Reading Items in an ArrayList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Visual Basic 2005.

I have created a two dimensional ArrayList named aSystem that is populated as
follows:-

aSystem.Add(New PickList(0, "Undefined"))
aSystem.Add(New PickList(-1, "Standard UTM"))
aSystem.Add(New PickList(-2, "UTM-NAD83"))
aSystem.Add(New PickList(-3, "UTM-NAD27"))

and then other items are added to the ArrayList from a sorted recordset as
follows:-

Do Until ssTemp.EOF
aSystem.Add(New PickList(ssTemp.Fields("PrimaryKey").Value, _
ssTemp.Fields("Name").Value))
ssTemp.MoveNext()
Loop

The structure of the PickList class is as follows:-

Public Structure PickList

Private mnPrimaryKey As Integer
Private msID As String

Public Sub New(ByVal PrimaryKey As Integer, ByVal ID As String)
mnPrimaryKey = PrimaryKey
msID = ID
End Sub

Public ReadOnly Property PrimaryKey() As Integer
Get
Return mnPrimaryKey
End Get
End Property

Public ReadOnly Property ID() As String
Get
Return msID
End Get
End Property

End Structure

I use the ArrayList to populate a ComboBox

With cboSystem
.DataSource = aSystem
.ValueMember = "PrimaryKey"
.DisplayMember = "ID"
End With

For the most part everything works fine; the ComboBox is populated with all
the data.

I have two problems...

1. I can't seem to read the items from the ArrayList directly. I would
expect to be
able to read the array something like this:

aSystem.Item(1)

but all I get back is:_

MagCalculator.PickList: {MagCalculator.PickList} where

where MagCalculator is the name of the application.

How do I read the individual items of the two-dimensional ArrayList?

2. I want to sort the ArrayList but although there is an abundance of
information in MSDN on how to sort a one-dimensional ArrayList, there is no
information on how to sort a two-dimensional ArrayList.

As mentioned already, I subsequently populate a ComboBox with the two
dimensional ArrayList, and this seems to work fine except of course the list
isn't sorted.

If anyone could point me in the right direction, I'd be grateful.
 
Hi Stuat,

I think you are mislead by the idea that there exists such a thing like

a "bidimensional" arraylist.

Here an example for your tasks. YOu can change the property being
compared
according to your needs.

-Tommaso




Public Class Form1
Inherits System.Windows.Forms.Form



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim aSystem As New ArrayList
aSystem.Add(New PickList(0, "Undefined"))
aSystem.Add(New PickList(-1, "Standard UTM"))
aSystem.Add(New PickList(-2, "UTM-NAD83"))
aSystem.Add(New PickList(-3, "UTM-NAD27"))

'1.Get items
Dim FirstPickList As PickList = DirectCast(aSystem(0),
PickList)
With FirstPickList
Dim id As String = .ID
Dim PrimaryKey As Integer = .PrimaryKey
End With


'2.Sort Items
aSystem.Sort(New ComparerForPickList)

End Sub

Class ComparerForPickList
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object)
As Integer Implements System.Collections.IComparer.Compare

Dim PickList1 As PickList = DirectCast(x, PickList)
Dim PickList2 As PickList = DirectCast(y, PickList)

Return String.Compare(PickList1.ID, PickList2.ID)

End Function

End Class

Public Class PickList

Private mnPrimaryKey As Integer
Private msID As String

Public Sub New(ByVal PrimaryKey As Integer, ByVal ID As String)
mnPrimaryKey = PrimaryKey
msID = ID
End Sub

Public ReadOnly Property PrimaryKey() As Integer
Get
Return mnPrimaryKey
End Get
End Property

Public ReadOnly Property ID() As String
Get
Return msID
End Get
End Property

End Class

End Class







Stuart ha scritto:
 
Hi Stuart,

I think you are mislead by the idea that there exists such a thing like

a "bidimensional" arraylist.

Here an example for your tasks. YOu can change the property being
compared according to your needs.

-Tommaso

Public Class Form1
Inherits System.Windows.Forms.Form


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles Button1.Click


Dim aSystem As New ArrayList
aSystem.Add(New PickList(0, "Undefined"))
aSystem.Add(New PickList(-1, "Standard UTM"))
aSystem.Add(New PickList(-2, "UTM-NAD83"))
aSystem.Add(New PickList(-3, "UTM-NAD27"))


'1.Get items
Dim FirstPickList As PickList = DirectCast(aSystem(0),
PickList)
With FirstPickList
Dim id As String = .ID
Dim PrimaryKey As Integer = .PrimaryKey
End With


'2.Sort Items
aSystem.Sort(New ComparerForPickList)


End Sub


Class ComparerForPickList
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object)
As Integer Implements System.Collections.IComparer.Compare

Dim PickList1 As PickList = DirectCast(x, PickList)
Dim PickList2 As PickList = DirectCast(y, PickList)

Return String.Compare(PickList1.ID, PickList2.ID)

End Function

End Class


Public Class PickList


Private mnPrimaryKey As Integer
Private msID As String

Public Sub New(ByVal PrimaryKey As Integer, ByVal ID As String)

mnPrimaryKey = PrimaryKey
msID = ID
End Sub


Public ReadOnly Property PrimaryKey() As Integer
Get
Return mnPrimaryKey
End Get
End Property


Public ReadOnly Property ID() As String
Get
Return msID
End Get
End Property


End Class


End Class


Stuart ha scritto:
 
Thanks Pamela.

Wow! That was quick!

....and it worked. I would never had figured it out myself. You're a star.

Stuart
 
Back
Top