Click to See Complete Forum and Search --> : Help with c# arrays, please.


Eagle_f90
March 2nd, 2010, 07:54 PM
I am familare with arrays from Perl but c# is a new ball game for me. With the help of a few users from this board I recently created an object to handle SQL calls. When I tested it today I got an array error of: CS0225 The params parameter must be a single dimensional array. The line of code is:

public static int ExecNonQuerySP(string SPName, params List<SqlParameter> Params)


Can anyone help me with this as I have no experiance with multi-dimensional arrays and need a bit of help with what is wrong on this. Full code of that object:

public static int ExecNonQuerySP(string SPName, params List<SqlParameter> Params)
{
int RetVal = 0;

using (SqlConnection con = GetCon())
{
SqlCommand cmd = new SqlCommand(SPName, con);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter Param in Params)
{
cmd.Parameters.Add(Param);
}
cmd.Connection.Open();
RetVal = cmd.ExecuteNonQuery();
}
return RetVal;
}

rliq
March 2nd, 2010, 09:43 PM
In the following line:

public static int ExecNonQuerySP(string SPName, params List<SqlParameter> Params)

The 'params' bit is not required. Try:

public static int ExecNonQuerySP(string SPName, List<SqlParameter> Params)

BigEd781
March 3rd, 2010, 02:29 AM
To elaborate, the 'params' keyword denotes a parameter that is seen from within the function as an array, but from the outside it is a variable length argument list. Example:

void Foo( params string[] words )
{
foreach( string word in words )
{
Console.WriteLine( word );
}
}

// call it like this

Foo( "one", "two", "three" );

// output
one
two
three

Eagle_f90
March 3rd, 2010, 05:30 AM
That did the trick, thanks for the help and explaination. Now I return you to your regularly schedualed programming.