Reading Items in an ArrayList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart

    #1

    Reading Items in an ArrayList

    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("Primar yKey").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.P ickList: {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.

  • pamelafluente@libero.it

    #2
    Re: Reading Items in an ArrayList

    Hi Stuat,

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

    a "bidimensio nal" 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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) 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(aSys tem(0),
    PickList)
    With FirstPickList
    Dim id As String = .ID
    Dim PrimaryKey As Integer = .PrimaryKey
    End With


    '2.Sort Items
    aSystem.Sort(Ne w ComparerForPick List)

    End Sub

    Class ComparerForPick List
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object)
    As Integer Implements System.Collecti ons.IComparer.C ompare

    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:
    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("Primar yKey").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.P ickList: {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.

    Comment

    • tommaso.gastaldi@uniroma1.it

      #3
      Re: Reading Items in an ArrayList

      Hi Stuart,

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

      a "bidimensio nal" 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(B yVal sender As System.Object, ByVal e As

      System.EventArg s) 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(aSys tem(0),
      PickList)
      With FirstPickList
      Dim id As String = .ID
      Dim PrimaryKey As Integer = .PrimaryKey
      End With


      '2.Sort Items
      aSystem.Sort(Ne w ComparerForPick List)


      End Sub


      Class ComparerForPick List
      Implements IComparer

      Public Function Compare(ByVal x As Object, ByVal y As Object)
      As Integer Implements System.Collecti ons.IComparer.C ompare

      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:
      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("Primar yKey").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.P ickList: {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.

      Comment

      • Stuart

        #4
        Re: Reading Items in an ArrayList

        Thanks Pamela.

        Wow! That was quick!

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

        Stuart

        "pamelafluente@ libero.it" wrote:
        Hi Stuat,
        >
        I think you are mislead by the idea that there exists such a thing like
        >
        a "bidimensio nal" 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(B yVal sender As System.Object, ByVal e As
        System.EventArg s) 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(aSys tem(0),
        PickList)
        With FirstPickList
        Dim id As String = .ID
        Dim PrimaryKey As Integer = .PrimaryKey
        End With
        >
        >
        '2.Sort Items
        aSystem.Sort(Ne w ComparerForPick List)
        >
        End Sub
        >
        Class ComparerForPick List
        Implements IComparer
        >
        Public Function Compare(ByVal x As Object, ByVal y As Object)
        As Integer Implements System.Collecti ons.IComparer.C ompare
        >
        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:
        >
        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("Primar yKey").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.P ickList: {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.
        >
        >

        Comment

        Working...