How to create 2 dimensional dynamic array (vb.net or c#)

  • Thread starter Thread starter singleb
  • Start date Start date
S

singleb

I've tryed to something lik this and got "Object reference not set to
an instance of an object."

Dim myarr(,) As String
myarr(0, 0) = "aaaa"
myarr(0, 1) = "bbbb"
myarr(1, 0) = "cccc"
myarr(1, 1) = "dddd"

Thanks.
 
I've tryed to something lik this and got "Object reference not set to
an instance of an object."

Dim myarr(,) As String
myarr(0, 0) = "aaaa"
myarr(0, 1) = "bbbb"
myarr(1, 0) = "cccc"
myarr(1, 1) = "dddd"

Thanks.


That's because you've only declared the variable, not made it refer to
an actual array object. Since you posted to the C# group, here's the
C# syntax

string[,] myarr = new string[2,2];

And if you use C#, the compiler will catch this error since you're not
allowed to use unassigned local variables.


Mattias
 
Hi,

array dimensions can't be changed, after the array is created. If you do not
know the exact size at the time you need to create the array, use ArrayList
instead.

In your case, using two dimensions, create a struct or class for the data
(time, value...) and add objects of it to an ArrayList.


Regards,
Husam Al-a'araj
 
I've tryed to something lik this and got "Object reference not set to
an instance of an object."
Dim myarr(,) As String
myarr(0, 0) = "aaaa"
myarr(0, 1) = "bbbb"
myarr(1, 0) = "cccc"
myarr(1, 1) = "dddd"

That's because you've only declared the variable, not made it refer to
an actual array object. Since you posted to the C# group, here's the
C# syntax

string[,] myarr = new string[2,2];

And if you use C#, the compiler will catch this error since you're not
allowed to use unassigned local variables.

Mattias

The problem I dont know how long will be this array, I cant define its
size.
I guess it cant be done with arrays, is there some control or type
that will do this job ?
 
The problem I dont know how long will be this array, I cant define its
size.
I guess it cant be done with arrays, is there some control or type
that will do this job ?

No problems with dynamic arrays.

Open source of strings (text file), get array dimensions m and n; then
create array:

string[,] myarr = new string[m, n];

And read strings to myarr.
 

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