CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    36

    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;
    }

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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)
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    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

  4. #4
    Join Date
    Feb 2010
    Posts
    36

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured