Arrays Again Only This Time 2D

MRRX8

Newcomer
Joined
Jan 29, 2006
Messages
7
Hi

I have been sitting here for hours trying to find some information on 2D arrays in VB.NET. Basically i would like to store data like this

STRING INTEGER
SIZE 24 10

then display it is a grid

This array has to be dynamic as i dont know how many elements there will be. I would like to know is this possible? Will a 2d array do this if so where do i start from, ive tried coding some examples for myself but cannot manage to get it working.

I look forward to a response thanks experts
 
A very accurate answer depends on what exactly you are doing, and I don't quite understand what exactly you are trying to achieve. Are you looking to create a database-like functionality? Something more along the lines of a spreadsheet?

To declare a variable for a two-dimensional array in vb use the following syntax:
Visual Basic:
Dim MyVariable As Integer(,)
To create an array and store it in a variable use the following syntax:
Visual Basic:
MyVariable = New Integer(23, 9) {} 'Yields a 24 x 10 array of integers
Arrays can only support a single data type, unless you declare an array of objects (this uses boxing and late binding, which are generally looked down upon). This would be similar to a spreadsheet. If you want an array that holds multiple data types, for example, a list of names with accompanying dates of birth, and annual income, you should use an array of structures. This somewhat resembles a database.
Visual Basic:
'This represents one row in a "database"
Structure PersonsInfo
    Dim Name As String
    Dim Income As Decimal
    Dim DOB As DateTime
End Structure
 
Dim MyArray As New PersonsInfo(23) {}
'Creates an array of 24 persons info
'or a "database" that has 24 rows
 
Public Sub AccessPeronalInfo()
    MessageBox.Show("Person # 14's name:")
    MessageBox.Show(MyArray(14).Name)
 
    MessageBox.Show("Setting Person # 8's annual income to $100.")
    MyArray(8).Income = 100.0
End Sub
If you want to be dynamic, you have a few options. When you need more space, you can create a new, larger array and copy the contents of the old array to the new one. You can also use ArrayLists or the generic List class in .Net 2.0, which automatically resize and copy the arrays, making your life easier.

Jagged arrays offer more flexibility, better memory management options, and are better optimized, but are more complicated and not CLS-compliant. You may wish to research these in MSDN. You can also use ArrayLists (or List<T>) in a manner similar to that of a Jagged array.
 
Last edited:
Thank you for the help. Basically what i wanted to store in the array was the persons name and age then display it on a grid. Hoever i have never used multidimensional arrays and havn't the slightest idea how to code what it is i want.
 
wouldnt i need to create 2 single dimensional array's for this? How would i store both values in the single array?

Hope to hear from you
 
Back
Top