Two dimension Array

  • Thread starter Thread starter Agnes
  • Start date Start date
A

Agnes

I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
.....ETC
From the help , i know I can use arItem.setvalue("APPLE",0,0)... ETC
However, I don't know how to declare such array
Please help ~
 
Agnes,
Have you tried:

Dim arItem(9,1) As Object
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
...
arItem(9,0)= "Banana"
arItem(9,1)=30

However seeing as each row appears to be an object. I would consider
creating an array of Objects instead.

Something like:
Public Class Item
Private Readonly m_name As String
Private m_ount As Integer
Public Sub New(name As String, count As Integer)
m_name = name
m_count = count
End Sub
Public ReadOnly Property Name As string
Get
Return m_Name
End Get
End Property
Public Property Count As Integer
Get
Return m_count
End Get
Set(value As Integer)
m_count = value
End Set
End Property
...
End Class

Dim arItem(9) As Item
arItem(0) = New Item("Apple", 30)
arItem(1) = New Item("Orange", 16)
...
arItem(9) = New Item("Banana", 30)

Note, because Item is defined to be a class you need to initialize each row
of the array as I have done above. Once each row is initialized, you can use
the properties of Item to get at its values:

' sell an apple
arItem(0).Count -= 1

Hope this helps
Jay
 
Agnes,

You have so often worked with a datatable. When an array get more dimensions
than that one is in my opinion one of the easiest one.

\\\
dim dt as new datatable
dt.columns.add("Fruit", gettype(system.string))
dt.columns.add("Value", gettype(system.Int32))
dim dr as datarow = dt.newrow
dr("Fruit") = "APPLE"
dr("Value")=30
dt.rows.add(dr)
dt.rows.add(dt.newrow)
dt.rows(1)(0) = "ORANGE"
dt.rows(1)(1) = 16
dt.rows.add(dt.newrow)
dt.rows(2).itemarray = new object() {"Citrus",20}
///
Etc in the way you like

I hope this helps?

Cor
 
Agnes said:
I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
....ETC
From the help , i know I can use arItem.setvalue("APPLE",0,0)... ETC
However, I don't know how to declare such array


Instead of using a 2-dimensional array here, I would use a 1-dimensional
array of instances of a class/structure:

\\\
Public Structure Item
Public Name As String
Public Number As Integer
End Structure
..
..
..
Dim a(90) As Item
For i As Integer = 0 To a.Length - 1
a(i).Name = ...
a(i).Number = ...
Next i
///

Notice that this is a very basic sample, if you are storing more data for
each object, consider using a class, and add properties instead of providing
direct access to the fields holding the attributes' values.
 
Thanks All.
Dear Herfried K. Wanger,
I never know I can use 'structure' to do that , I learn one more new
thing. Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top