468,106 Members | 1,536 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,106 developers. It's quick & easy.

3 dimension string array

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"} };
May 8 '06 #1
5 7270
Jackson wrote:
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.
May 8 '06 #2
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" wrote:
Jackson wrote:
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.

May 8 '06 #3
Jackson wrote:
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.
May 8 '06 #4
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" <Ja*****@discussions.microsoft.com> wrote in message
news:83**********************************@microsof t.com...
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"} };

May 8 '06 #5
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" wrote:
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" <Ja*****@discussions.microsoft.com> wrote in message
news:83**********************************@microsof t.com...
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"} };


May 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by psmith | last post: by
8 posts views Thread by Jeff Johnson | last post: by
2 posts views Thread by Ram Achar | last post: by
11 posts views Thread by Zordiac | last post: by
5 posts views Thread by Paulers | last post: by
8 posts views Thread by fAnSKyer/C# newbie | last post: by
2 posts views Thread by John | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.