Question Dynamicly creating two-dimensional arrays

Mynx

New member
Joined
Mar 6, 2011
Messages
2
Programming Experience
3-5
Hello eveyone,
I have the followig problem; i want to make a class with a private field as a 2-dimensional array, where it's dimension is set in the constructor of the class.
Contained data does not matter at this moment
I've looked a bit around but couldn't find a direct solution to my problem, is this possible in VB.NET?

VB.NET:
Private twoDimArray(,) As Integer

Public Sub New(ByVal width As Integer, ByVal height as Integer)
   ?
   ?
End Sub

Thanks
 
If you need to do this one thing I'd suggest you take a look at is the Redim command.

VB.NET:
Private twoDimArray(,) As Integer

Public Sub New(ByVal width As Integer, ByVal height as Integer)
Your Code
ReDim twoDimArray(width, height)
Your Code
End Sub
Note that if you already have data in an array you need to use the "preserve" keyword to avoid wiping it out. Also be aware that this can cause quite a performance hit, especially if you try to preserve. If that's what you need then that's what it's there for, but try to avoid it where possible.

According to that article that warns about performance, you can use the Arraylist object that assigns itself more space as needed rather than using arrays and the redim command. However as far as I can tell (and according to microsoft) that is strictly one dimensional.
 
Last edited:
Exactly what i needed!
Since the table i want to resize is completely empty i don't need the preserve keyword, thanks for mentioning it though. Interesting article too!
Thanks for the help!

Edit: I thought about using an arraylist
Using them twodimensional is possible (arraylists in an arraylist ), but it's just not practical since you can't adress the elements directly, you'll need a double Get to get to an element, annoying!
 
Last edited:
Back
Top