3 dimension string array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have something that is stumping me. I am trying to initialize a 3
dimensional string array with the code below, but it wont compile. Can anyone
explain what Im doing wrong?????????????? Im going crazy with this one! Ive
tried a 2 dimensional array minus the "test3" and it works fine.

public string[,,] screens;

screens = new string[,,]{ {"test1","test2","test3"} };
 
Jackson said:
I am trying to initialize a 3 dimensional string array
with the code below, but it wont compile. [...]
Ive tried a 2 dimensional array minus the "test3" and
it works fine.

public string[,,] screens;
screens = new string[,,]{ {"test1","test2","test3"} };

I think you are misunderstanding what a 3D array is, structurally.
Your "test3" is only adding one extra element to the second dimension
of a 2D string array. The dimensionality is *not* the same as the
number of elements, just as a square (2D shape) can have sides of any
length (any number of elements) and still be only a 2D square.

Eq.
 
So is what I am coding actually doing like below?

screens["test1",,]
screens["test2",,]
screens["test3",,]

I want it to be like

screens["test1","test2","test3"]
screens["test2-1","test2-2","test2-3"]

How would I make my initializer to do like the bottom?


Paul E Collins said:
Jackson said:
I am trying to initialize a 3 dimensional string array
with the code below, but it wont compile. [...]
Ive tried a 2 dimensional array minus the "test3" and
it works fine.

public string[,,] screens;
screens = new string[,,]{ {"test1","test2","test3"} };

I think you are misunderstanding what a 3D array is, structurally.
Your "test3" is only adding one extra element to the second dimension
of a 2D string array. The dimensionality is *not* the same as the
number of elements, just as a square (2D shape) can have sides of any
length (any number of elements) and still be only a 2D square.

Eq.
 
Jackson said:
I want it to be like
screens["test1","test2","test3"]
screens["test2-1","test2-2","test2-3"]
How would I make my initializer to do like the bottom?

string[,,] s = new string[3,3,3];

for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
s[i,j,k] = i + "," + j + "," + k;

This creates a 3 x 3 x 3 array (27 strings in total: you can imagine
them laid out like the 27 sub-cubes of a Rubik's Cube).

But I'm not sure what you're hoping to do with your array. Certainly
you can't address it by string indexes: you'd have to use numbers,
e.g. s[2, 2, 2] for the last element (since the three are counted from
0 to 2). If that doesn't help, explain what you're trying to do with
your program, and why you want this particular structure.

Eq.
 
screens = new string[,,]{ {"test1",test2","test3"},
{"test4",test5","test6"},
{"test7",test8","test9"}, {"test10",test11","test12"} };

This creates a 3-dimensional array, and initializes each dimension to three
values:

Dimension 1: "test1", "test4", "test7", "test10"
Dimension 2: "test2", "test5", "test8", "test11"
Dimension 3: "test3", "test6", "test9", "test12"

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Thanks Kevin,

I see my err. I was thinking

screens = new string[,,]{ {"test1",test2","test3"},
{"test4",test5","test6"},
{"test7",test8","test9"}, {"test10",test11","test12"} };

would create

Dimension 1: "test1", "test2", "test3"
Dimension 2: "test4", "test5", "test6"
Dimension 3: "test7", "test8", "test9"
Dimension 3: "test10", "test11", "test12"

Kevin Spencer said:
screens = new string[,,]{ {"test1",test2","test3"},
{"test4",test5","test6"},
{"test7",test8","test9"}, {"test10",test11","test12"} };

This creates a 3-dimensional array, and initializes each dimension to three
values:

Dimension 1: "test1", "test4", "test7", "test10"
Dimension 2: "test2", "test5", "test8", "test11"
Dimension 3: "test3", "test6", "test9", "test12"

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

Jackson said:
I have something that is stumping me. I am trying to initialize a 3
dimensional string array with the code below, but it wont compile. Can
anyone
explain what Im doing wrong?????????????? Im going crazy with this one!
Ive
tried a 2 dimensional array minus the "test3" and it works fine.

public string[,,] screens;

screens = new string[,,]{ {"test1","test2","test3"} };
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top