mutidemensional array question

  • Thread starter Thread starter Shawn B.
  • Start date Start date
S

Shawn B.

Greets,

I am saying

int y = 0;
byte[,] x;
x = new byte[y, 65535];

And this work until

x[y, 300] = 0xFF;

Then I get an out of bounds.

Looking closer, it seems that if the number is too large it won't actually
create the array.

How do I make this work?

Thanks,
Shawn
 
Shawn B. said:
Greets,

I am saying

int y = 0;
byte[,] x;
x = new byte[y, 65535];

And this work until

x[y, 300] = 0xFF;

Then I get an out of bounds.

Looking closer, it seems that if the number is too large it won't actually
create the array.

How do I make this work?

Thanks,
Shawn

You're creating an array with a dimension of size 0... this means that
by default, anything you try to access inside of it will be out of bounds.

Also, keep in mind that arrays in C# are zero-based, so even if you
initialize your variable y to a number besides zero, your code will still
generate an exception.

Erik
 
When I say

byte[,] x;
x = new byte[10,10];

it works fine. I get 100 bytes and I can reference it.

when I say

byte[,] x;
x = new byte[0,65535];

It does not work. I do not get 65535 bytes allocated. What must I do to
make it work the way I want?


Thanks,
Shawn


Erik Frey said:
Shawn B. said:
Greets,

I am saying

int y = 0;
byte[,] x;
x = new byte[y, 65535];

And this work until

x[y, 300] = 0xFF;

Then I get an out of bounds.

Looking closer, it seems that if the number is too large it won't actually
create the array.

How do I make this work?

Thanks,
Shawn

You're creating an array with a dimension of size 0... this means that
by default, anything you try to access inside of it will be out of bounds.

Also, keep in mind that arrays in C# are zero-based, so even if you
initialize your variable y to a number besides zero, your code will still
generate an exception.

Erik
 
Shawn B. said:
When I say

byte[,] x;
x = new byte[10,10];

it works fine. I get 100 bytes and I can reference it.

when I say

byte[,] x;
x = new byte[0,65535];

It does not work. I do not get 65535 bytes allocated. What must I do to
make it work the way I want?


Thanks,
Shawn

10 x 10 = 100

0 x 65535 = 0

Therefore ->

x = new byte[1,65535];

1 x 65535 = 65535

Hope this answers your question.

Erik
 
Thanks,

I already knew that the 0 would initialize it to zero. For some reason, I
didn't "know" that. It's still a holiday today =))





Thanks,
Shawn



Erik Frey said:
Shawn B. said:
When I say

byte[,] x;
x = new byte[10,10];

it works fine. I get 100 bytes and I can reference it.

when I say

byte[,] x;
x = new byte[0,65535];

It does not work. I do not get 65535 bytes allocated. What must I do to
make it work the way I want?


Thanks,
Shawn

10 x 10 = 100

0 x 65535 = 0

Therefore ->

x = new byte[1,65535];

1 x 65535 = 65535

Hope this answers your question.

Erik
 
Back
Top