Create array of sqlParameters when you dont know the siz a

M Kerr 21 Reputation points
2022-01-07T19:17:41.907+00:00

I want to create an array (only way to pass parameters to our data access component) but i dont know how many params i will need.

I have a bunch of bool variables and will add parameters if the bool is true

    SqlParameter[] p = new SqlParameter[5];
    if (IsCall)
        p[0] = new SqlParameter("@IsCall", IsCall);
    if (Given)
        p[p.count] = new SqlParameter("@Given", Given);

Thanks !

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,205 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.8K Reputation points
    2022-01-08T00:34:50.897+00:00

    If you cannot use a list, then consider this approach:

    var list = new List<SqlParameter>( );
    if( IsCall)
       list.Add( new SqlParameter("@IsCall", IsCall));
    if( Given)
       list.Add( new SqlParameter("@Given", Given));
    . . .
    SqlParameter[ ] p = list.ToArray( );
    
    0 comments No comments