In VB.NET I would like to create a complicated data structure with multiple types of data stored in an array like format (see below).
I am trying to create a data structure that would look something like this:
[Name; xLoc; yLoc; zLoc; [Jagged Array]]
Note: Name needs to be dimensioned as a string, xLoc and so forth as integers.
The Jagged Array would look like this:
[Array1;Array2;...]
Each Array within would look like:
[xVel; yVel; zVel]
Note: Each value in the above vector would be an integer.
An example of this data structure with data in it might look like this:
[Ship1; 15; 16; 25; [[2;3;0];[5;0;7];[6;2;1]]]
Essentially I want to create a jagged array with multiple types of data stored in the same structure. Does VB.NET support this kind of structure in any way? I am not very experienced with VB.NET so there might very well be a much more efficient way to structure the data that would do the same thing in VB. Please feel free to comment with any ideas or critiques.
You could use the
System.Collections.Generic.List(Of Object), but you need to keep track of what the types are when you retrieve the values from the list.Out of curiosity, why do you want to store different types together in the same list?
To use
System.Collections.Generic.ListDim x as new List(of Object)() x.Add(element) 'given an element, all objects inherit from Object x.Add(new Int32({1,2,3,4,5}) x.AddRange(array) 'append an array's contents Dim retrievedValue as int32() = x(1) ' here, I needed to remember what I stored at what indice.To make things easier, I would generally not do this kind of thing. You lose type safety and need to implicitly cast the value retrieved, but if you want to it can be done in the language.
I would think about how I could write a class that would represent the data I am working with. If a class is too heavyweight, perhaps a few strongly-typed Dictionary() objects that share keys to store say int32() and strings in a
string_mapandint_array_maprespectively.For instance, a Dictionary stores elements in KeyValuePairs, which can be strongly typed.
Dim dict as new Dictionary(of string, List(of int32))() dict.Add('myname', new List(of Int32)({1,2,4,5,6,7})So
dict('myname')retrieves a strongly typed list of Int32.I’m really just stabbing in the dark here, but you can see there are several useful collections classes in .NET. I’d recommend reading into the System.Collections.Generic namespace.