• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1456
  • Last Modified:

Creating a C# array

Hello,,

I'm trying to use the following to create a string array:

string [] strArray = new string [12]  {"hello","world"} ;

Accordng to my references, this is legal, but the compiler burps with the following message:

Invalid rank specifier: expected ',' or ']' (CS0178)

Creating the array and then assigning the text works as expected, however

Thanks.

0
qvee
Asked:
qvee
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you're giving it values to assign then I don't think you can specify the initial upper bound of 12.  It determines the size of the array based on the number of values between the braces {}:

        private void button1_Click(object sender, EventArgs e)
        {
            string[] strArray = new string[] { "hello", "world" };
            MessageBox.Show(strArray.Length.ToString());
        }
0
 
findsukeshCommented:
string [] strArray ;
strArray = = new string [2] {"Hello", "World"}

The size of array specified and the number of string literals need to match for this code to get compiled.
0
 
moe2sanCommented:
If you need the string array size to 12 but you want to set values to the first and second items of the string array, the following code will be ok.

                string[] strArray = new string[12];
      strArray[0] = "hello";
      strArray[1] = "world";
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jagadeesh_motamarriCommented:
>>string [] strArray = new string [12]  {"hello","world"} ;

The compiler expects to find the 12 elements in the array....when u just have 2 elements...it should be

string [] strArray = new string [2]  {"hello","world"} ;


if you are not sure on the number of elements your array contain...u might want to use this...
string[] strArray = new string[12];
strArray[0] = "hello";
strArray[1] = "world";

and the rest of the its items are automatically initialized to the default initial value for the array type if the array is not initialized at the time it is declared.
0
 
basavaraj_knCommented:
what findsukesh & moe2san are saying is correct however they failed explain why .NET behaves that way

when you are declaring and initilizing an array simoultaneously .NET assumes you are giving data to all its array elements so if it finds that you have not provided in that way, then it throws an compiler exception.

So if you want the array to be of size 12 but initilize only 2 array elements then what moe2san has given should do. or the below one will also do.

string [] strArray = new string [12]  {"hello","world", "", "", "", "", "", "","", "", "",} ;

Hope it solved your problem
~BAS
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Isn't that I said in the very first comment of this thread?...

   "It determines the size of the array based on the number of values between the braces {}:"

=)
0
 
yokkuiCommented:
I usually do the same as Idle_Mind as well and find no point specifying both size and members.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now