2 D Array?

bungpeng

Senior Contributor
Joined
Sep 10, 2002
Messages
906
Location
Malaysia
Array class like ArrayList is a great class because we no need to define the size of array and it will automatic adjust, but how about if I want to use 2D array? can I still use class like ArrayList? how to do?
 
I would try creating a class and use the two indexes as parameters in a default Item property.

ie:

public class WidgetArray

private myWidgets(iCol*iRow) as widget
inherits ArrayList 'override the methods you wish to use
public default property Items (Byval x as Integer, ByVal y as Integer) as Widget

get
return mywidgets(x,y)
end get

set (byval value as widget)
mywidgets(x,y) = value
end set


'override add, remove and whatever else you need
 
Visual Basic:
Dim alMain As ArrayList = New ArrayList
Dim iMain() As Integer = {1, 2, 3, 4, 5}
alMain.Add(iMain)

If you needed ArrayList-like functionability for the 2nd-dimension change the array into an ArrayList and add that instead. I would not recommend this method for large lists or processor intensive code blocks, nor is this a viable solution for all cases.
 
there is almost never a need to do that

just Dim it once... and then make new objects like so...

Dim myThing as Object
Dim myArray as ArrayList
mything = new object(parameters)

myArray.Add(mything)

mything = new object(paramenters)

(in reality you'd probably use a Sub and use the parameters from that Sub to instantiate your new object, that way you'd only have one statement..... 'x = new x(a,b,c setting abc when you call the sub)
(obviously this is just a sample, so your needs will vary, but it's a shot)

always tryin to help
 
Back
Top