|
-
February 13th, 2003, 10:46 AM
#1
Multi-Dimension array initializers
It's me again.... I know I'm troublesome.
First I have this:
protected int[,] array = new int[15,10];
then I do this:
map = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,1,0,0,0,0,0,1,1,1,0,0,0,1,
8,0,0,0,0,0,0,0,1,1,1,0,0,0,1,
1,0,0,0,1,1,1,0,0,1,0,0,0,0,1,
1,0,0,0,1,1,1,0,0,0,0,0,1,0,1,
1,1,0,0,1,1,1,0,0,0,0,0,1,0,1,
1,0,0,0,0,1,0,0,0,0,1,1,1,0,1,
1,0,1,1,0,0,0,1,0,0,0,0,0,0,5,
1,0,1,1,0,0,0,1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
And the compiler says this:
CMap.cs(31,43): error CS1525: Invalid expression term ','
CMap.cs(31,44): error CS1002: ; expected
CMap.cs(31,45): error CS1002: ; expected
Multiple times. And to my surprise,
neither the books "Inside C# second edition" nor "C# language specification" actually talks about that.
MSDN is a maze to me.
Question: How to I initialize a multiple dimensional array like this?
end------------------------------
Programmers aren't born, but are made from hardwork, effort and time.
To be a good one, requires more effort and hardwork.
Therefore N quality programmer = (N*hardwork)+(N*effort)+(N*time)
-
February 13th, 2003, 12:50 PM
#2
You can initialize the array upon declaration as shown in the following example:
int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}};
You can also initialize the array without specifying the rank:
int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};
If you choose to declare an array variable without initialization, you must use the new operator to assign an array to the variable. For example:
int[,] myArray;
myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; // OK
myArray = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
You can also assign a value to an array element, for example:
myArray[2,1] = 25;
thanx
Paresh
-
February 13th, 2003, 07:40 PM
#3
end------------------------------
Programmers aren't born, but are made from hardwork, effort and time.
To be a good one, requires more effort and hardwork.
Therefore N quality programmer = (N*hardwork)+(N*effort)+(N*time)
-
February 14th, 2003, 12:30 PM
#4
I know the declaration is odd than C++ but that's the way they made so...
Infact in C++ you can declare like this where as C# can't
for example
C++/C
1) int [10]a = {0};
will automatically initialize all mem. to 0
and in C# you can't
so there are mysterys.........
anyway ..
thanx
Paresh
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|