2 Dimensional Arrays

gonegaming12

Member
Joined
Jul 10, 2007
Messages
5
Programming Experience
3-5
So today I was doing some coding where I need a 2+ dimensional array for my game I'm making and I realized that I have never needed to use one...so could someone help me with this.

VB6 was like this
VB.NET:
Public Class Tile
public something as integer
End Class

Dim tiles() as tile

But if I try to use access something it is null and i get an error. And since .net decides to now let you use

VB.NET:
Dim tiles() as new tile

I need to know if there is a way to setup 2 dimensional arrays without have to every time you add a new object to it do this

VB.NET:
tiles(i) = new tile
 
VB.NET:
Public Class Tile
public something as integer
End Class

Dim tiles() as tile
That's a 1D array


I need to know if there is a way to setup 2 dimensional arrays without have to every time you add a new object to it do this

No. Think about it:

When you declare ana rray you are simply making a construct in memory that is capable of holding N references to N object sof the same type

Its like writing:

Dim a0 as Thing
Dim a1 as Thing
Dim a2 as Thing
Dim a3 as Thing
Dim a4 as Thing

Five references, all capable of pointing to a Thing, but not pointing to anything right now



Dim a(4) as New Thing

Five references, all capable of pointing to a Thing, but not pointing to anything right now


You made a new array, like a new ice cube tray, but you didnt fill it with anything. VB doesnt fill it automatically, because how can it know what to fill it with? Ice cube trays take any kind of liquid.. You'd be mad if you bought a new ice cube tray and it auto filled itself with water, but you wanted to put orange juice in there for a certain cocktail this evening..

Remember, that VB.NET is OO, and supports inheritance.. An array of type X can hold any object under type X in the hierarchy too:

Dim x(3) as Control
x(0) = New TextBox
x(1) = New Button
x(2) = New Label

...
 
im sorry i forgot to add the things to the tile() its like tile(max_mapwidth, max_mapheight). Also I forgot to put this in, i didn't know if it made a differance. but the class as a new method in it that sets all the variables to 0 or -1.
 
im sorry i forgot to add the things to the tile() its like tile(max_mapwidth, max_mapheight). Also I forgot to put this in, i didn't know if it made a differance. but the class as a new method in it that sets all the variables to 0 or -1.

Use two loops to fill your array with objects
 
wow I am good, thats exactly what I did (took forever cause i have like 50 classes., think i did something list this

VB.NET:
sub initarrays
dim x as integer=0
dim y as integer=0

Do until y = tiles.getupperbounds(1)
    Do until x = tiles.getupperbonds(0)
        tile(x,y) = new tile
         x=x+1
    Loop
      y=y+1
      x=0
Loop
y=0
x=0

end sub
 
I thought that VB6 did not use two-dimensional arrays. I thought this was a feature new to .NET for visual basic. Other than creating one using a UDT, if it was even possible through that. But in .NET you can just say:

VB.NET:
Dim str()() as string

and tah dah you have a two dimensional array.

Correct?
 
Thats actually a great link. I have been having to use C# a little bit lately and this will really come in handy for all those basic things that I dont know how to accomplish syntactically with C#.

But for all intensive purposes I was right about the 2-D array. Naturally I would have redim it, etc.

But what about the VB6 part. There are no 2-D arrays in VB6 unless you make an array of collections...that is the closest thing. Right?

How would you go about making a 2-D array using UDT in VB6?
 
But what about the VB6 part. There are no 2-D arrays in VB6 unless you make an array of collections...that is the closest thing. Right?

How would you go about making a 2-D array using UDT in VB6?
We don't have VB6 forums here, only VB.Net, but you'll plenty of them at the internet.

Dim str()() as string is not a two-dimensional array, it is a jagged array, ie array of arrays. Dim str(,) as string is two-dimensional.
 
I was also under the impression that you HAVE to supply the size of the top level array in a jagged array

Dim s()() as String 'fails?
Dim s(10)() as String 'works

I dont know how this works for 3 levels.. ?
 
But for all intensive purposes I was right about the 2-D array. Naturally I would have redim it, etc.

Small grammar note: "For all intents and purposes" ;)

Yep, a jagged array can/is 2D, also so is a rectangular one:
Dim (2,3) As String

Perhaps we should talk of jagged vs rectangular rather than 2D


Actually, if youre ever ReDImming an array maybe you should be using a better container in the first place!
 
Back
Top