Help with c# arrays, please.
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;
}
Re: Help with c# arrays, please.
In the following line:
Code:
public static int ExecNonQuerySP(string SPName, params List<SqlParameter> Params)
The 'params' bit is not required. Try:
Code:
public static int ExecNonQuerySP(string SPName, List<SqlParameter> Params)
Re: Help with c# arrays, please.
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:
Code:
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
Re: Help with c# arrays, please.
That did the trick, thanks for the help and explaination. Now I return you to your regularly schedualed programming.