Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Arraylist.Index(Object) help please. [modified]

Arraylist.Index(Object) help please. [modified]

Scheduled Pinned Locked Moved Visual Basic
helpdatabasetutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geoff_3001
    wrote on last edited by
    #1

    I am trying to get the index of an object in an Arraylist. Please see my example. When run I get -1 for the values of Indexof. I am sure this is a rookie error but any help would be appreciated. Cheers Public Class Plateobj Public Name As String Public Order As Integer End Class Private Sub Go() Dim Plate As New Plateobj Dim List As New ArrayList 'Populate arraylist Plate.Name = "Geoff" Plate.Order = 3 List.Add(Plate) Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 2 List.Add(Plate) Plate.Name = "Paul" Plate.Order = 1 List.Add(Plate) 'Locate the index of each of the following MessageBox.Show(List.IndexOf(Plate), "Paul") Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 1 MessageBox.Show(List.IndexOf(Plate), "Dan") Plate = New Plateobj Plate.Name = "Geoff" Plate.Order = 3 MessageBox.Show(List.IndexOf(Plate), "Geoff") End Sub

    modified on Friday, November 27, 2009 6:36 AM

    L T 2 Replies Last reply
    0
    • G Geoff_3001

      I am trying to get the index of an object in an Arraylist. Please see my example. When run I get -1 for the values of Indexof. I am sure this is a rookie error but any help would be appreciated. Cheers Public Class Plateobj Public Name As String Public Order As Integer End Class Private Sub Go() Dim Plate As New Plateobj Dim List As New ArrayList 'Populate arraylist Plate.Name = "Geoff" Plate.Order = 3 List.Add(Plate) Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 2 List.Add(Plate) Plate.Name = "Paul" Plate.Order = 1 List.Add(Plate) 'Locate the index of each of the following MessageBox.Show(List.IndexOf(Plate), "Paul") Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 1 MessageBox.Show(List.IndexOf(Plate), "Dan") Plate = New Plateobj Plate.Name = "Geoff" Plate.Order = 3 MessageBox.Show(List.IndexOf(Plate), "Geoff") End Sub

      modified on Friday, November 27, 2009 6:36 AM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, You create 5 instances of Plateobj and add 3 of them to the list. when you reach the first MessageBox.Show statetement, the list contains: Geoff, Dan, Paul. Nothing changes afterwards, as you don't add the later Plateobj objects. So the output should be: 2, -1, -1 BTW: I suggest you improve your naming conventions; normally variables use lower-case (plate), and class names don't include "class" or "obj" or such. FWIW: the list order is independent of the Order member inside Plateobj (until you come up with some code to sort the list based on the Order member of course). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      1 Reply Last reply
      0
      • G Geoff_3001

        I am trying to get the index of an object in an Arraylist. Please see my example. When run I get -1 for the values of Indexof. I am sure this is a rookie error but any help would be appreciated. Cheers Public Class Plateobj Public Name As String Public Order As Integer End Class Private Sub Go() Dim Plate As New Plateobj Dim List As New ArrayList 'Populate arraylist Plate.Name = "Geoff" Plate.Order = 3 List.Add(Plate) Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 2 List.Add(Plate) Plate.Name = "Paul" Plate.Order = 1 List.Add(Plate) 'Locate the index of each of the following MessageBox.Show(List.IndexOf(Plate), "Paul") Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 1 MessageBox.Show(List.IndexOf(Plate), "Dan") Plate = New Plateobj Plate.Name = "Geoff" Plate.Order = 3 MessageBox.Show(List.IndexOf(Plate), "Geoff") End Sub

        modified on Friday, November 27, 2009 6:36 AM

        T Offline
        T Offline
        The Man from U N C L E
        wrote on last edited by
        #3

        You are generating new plate objects for comparison, however IndexOf does an object comparison. The newly created objects will never match anything in your list. What Indexof is doing is iterating through the list calling Equals to compare each object in turn. The only way your comparison can work is to override the Equals method on your PlateObj. E.g.

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim plate As Plateobj = TryCast(obj, Plateobj)

        ' Return False if the obj is not a plate
        If plate Is Nothing Then Return False

        ' Must have same name
        If plate.Name <> Me.Name Then Return False

        ' Must have same order
        If plate.Order <> Me.Order Then Return False

        ' We have a match
        Return True
        End Function

        If only the names need to match then only compare the names.

        If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

        G 1 Reply Last reply
        0
        • T The Man from U N C L E

          You are generating new plate objects for comparison, however IndexOf does an object comparison. The newly created objects will never match anything in your list. What Indexof is doing is iterating through the list calling Equals to compare each object in turn. The only way your comparison can work is to override the Equals method on your PlateObj. E.g.

          Public Overrides Function Equals(ByVal obj As Object) As Boolean
          Dim plate As Plateobj = TryCast(obj, Plateobj)

          ' Return False if the obj is not a plate
          If plate Is Nothing Then Return False

          ' Must have same name
          If plate.Name <> Me.Name Then Return False

          ' Must have same order
          If plate.Order <> Me.Order Then Return False

          ' We have a match
          Return True
          End Function

          If only the names need to match then only compare the names.

          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

          G Offline
          G Offline
          Geoff_3001
          wrote on last edited by
          #4

          Mr Kuryakin Thanks for the answer this makes sense I did try before posting a compareto function in the plateobj class, which I have successfully used for sorting. Ok so how to use the code you posted. 1) I assume I add this to the plateobj class ? 2) I get an error that suggests instead of using Overides I should use Overloads 3) Dim plate As Plateobj = TryCast(obj, Plateobj)gives two errors - TryCast not declared - 'Plateobj' is a type and cannot be used as an expression. Could I ask you to insert your code into my example and post back. Many Thanks Geoff

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups