Multi-dimensional array of different types?

jrvbdeveloper

Member
Joined
Jul 15, 2007
Messages
17
Programming Experience
Beginner
Hi,

Is it possible to have a multi-dimensional array of different types?

Lets say I want the following in a multi-dimensional array:
Joe 25
Tom 30
Jeff 55
Mike 42

The names are strings but the ages are integers. How would I go about doing this?

Thanks in advance.
 
You need two parallel arrays

One string array would hold the names, the other integer array would hold the numbers. The index numbers would match up the name with the number.

In a database, each array represents a field and each index in the parallel arrays represents a record.

Example:
myname(0) = "Joe"
mynumber(0) = 25

myname(1) = "Tom"
mynumber(1) = 30

etc.
 
You could create a class to reflect the data record, use a collection to hold instances...
VB.NET:
    <Serializable()> Class person
        Public name As String, age As Integer
    End Class

    Sub somepersons()
        Dim persons As New List(Of person)
        Dim p As New person
        p.name = "Ally"
        p.age = 33
        persons.Add(p)
    End Sub
To save and load such data you could use serialization (some examples).
 
Hi,

Is it possible to have a multi-dimensional array of different types?

It already exists and is called a DATATABLE

The names are strings but the ages are integers. How would I go about doing this?

Right click your project and Add New Item
Choose a DataSet
The dataset designer appear
Right click the surface and Add Table
Right click the table and Add Column
Set the type to String and the name to Name

etc..


Use like this:

Dim x as New XYZDataTable
x.AddXYZRow("John" 46)


Save it to disk with the WriteXml, read it with the ReadXml

You dont have to use this with a database, but if you do, check out the DW2 link in my sig. Start with Creating a Simple Data App
 
Back
Top