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. Creating an arraylist of an object

Creating an arraylist of an object

Scheduled Pinned Locked Moved Visual Basic
question
8 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

    Sorry if this is elsewhere but I look and could not find. Can I / How do I create an arrarylist of an object. For instance for the below object Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class Cheers Geoff

    G N G 4 Replies Last reply
    0
    • G Geoff_3001

      Sorry if this is elsewhere but I look and could not find. Can I / How do I create an arrarylist of an object. For instance for the below object Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class Cheers Geoff

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Add a method that creates the ArrayList:

      Public Class Posts
      Public Name As String
      Public ID As Integer
      Public Selected As Boolean

      Public Function MakeList() as ArrayList
      	Dim list as New ArrayList();
      	list.Add(Name);
      	list.Add(ID);
      	list.Add(Selected);
      	Return list;
      End Function
      

      End Class

      I don't really know if this is the solution you want, as you haven't told anything about what you are trying to accomplish. If it's not, you have to surrender a bit more information...

      --- Year happy = new Year(2007);

      G 1 Reply Last reply
      0
      • G Guffa

        Add a method that creates the ArrayList:

        Public Class Posts
        Public Name As String
        Public ID As Integer
        Public Selected As Boolean

        Public Function MakeList() as ArrayList
        	Dim list as New ArrayList();
        	list.Add(Name);
        	list.Add(ID);
        	list.Add(Selected);
        	Return list;
        End Function
        

        End Class

        I don't really know if this is the solution you want, as you haven't told anything about what you are trying to accomplish. If it's not, you have to surrender a bit more information...

        --- Year happy = new Year(2007);

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

        Sorry I was not tring to be difficult, This looks like what I am after I want to create an arrary of the Posts objects (only using this as an example) and the dynamically add to the arrarylist. How would I use the above to create the list of object and add to them. Thanks I am strating to see the answer

        1 Reply Last reply
        0
        • G Geoff_3001

          Sorry if this is elsewhere but I look and could not find. Can I / How do I create an arrarylist of an object. For instance for the below object Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class Cheers Geoff

          N Offline
          N Offline
          nlarson11
          wrote on last edited by
          #4

          Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class dim oArr as new arraylist ------Populate the arraylist ------ dim oPost as new Posts oPost.Name = "me" oPost.ID = 1 oPost.Selected=False oArr.Add (oPost) oPost = Nothing oPost = new Posts oPost.Name = "you" oPost.ID = 2 oPost.Selected=true oArr.Add (oPost) oPost = Nothing . . . ------example to get values out ------ For Each oPost in oArr Msgbox(oPost.name) Next

          G 2 Replies Last reply
          0
          • N nlarson11

            Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class dim oArr as new arraylist ------Populate the arraylist ------ dim oPost as new Posts oPost.Name = "me" oPost.ID = 1 oPost.Selected=False oArr.Add (oPost) oPost = Nothing oPost = new Posts oPost.Name = "you" oPost.ID = 2 oPost.Selected=true oArr.Add (oPost) oPost = Nothing . . . ------example to get values out ------ For Each oPost in oArr Msgbox(oPost.name) Next

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

            Aah Clear concise and I even understand Many Thanks will give it a go. Cheers

            1 Reply Last reply
            0
            • G Geoff_3001

              Sorry if this is elsewhere but I look and could not find. Can I / How do I create an arrarylist of an object. For instance for the below object Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class Cheers Geoff

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

              Thanks That works fine. The bit I was having trouble with was I thought you had to link the obeject to the arraylist somehow. I see now that arrarylist will allow you to add to it with whatever you like there is no need to "preset" it to do so. Thanks again

              1 Reply Last reply
              0
              • N nlarson11

                Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class dim oArr as new arraylist ------Populate the arraylist ------ dim oPost as new Posts oPost.Name = "me" oPost.ID = 1 oPost.Selected=False oArr.Add (oPost) oPost = Nothing oPost = new Posts oPost.Name = "you" oPost.ID = 2 oPost.Selected=true oArr.Add (oPost) oPost = Nothing . . . ------example to get values out ------ For Each oPost in oArr Msgbox(oPost.name) Next

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

                Ok, Iam stuck again I cobbled together a sort metod in by posts class (see below). When I run the code it works ok if my posts have names C,B, and A but if they have C, B and C no sorting occurs. Any Body got any clues pls Public Class Form1 Inherits System.Windows.Forms.Form Public Class Posts Implements IComparable Public Name As String Public ID As Integer Public Selected As Boolean Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim temp As Posts = CType(obj, Posts) Dim vMe As String = Name Dim iMe As Integer = ID Dim mestxt mestxt = "Name=" & vMe & ", Id=" & ID & " Temp name=" & temp.Name & " Temp_ID=" & temp.ID MessageBox.Show(mestxt, "Compare") If vMe < temp.Name Then MessageBox.Show("1") Return 1 End If If vMe > temp.Name Then Return 0 End If End Function End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim oArr As New ArrayList Dim mestxt As String Dim oPost As New Posts oPost.Name = "C" oPost.ID = 1 oPost.Selected = False oArr.Add(oPost) oPost = Nothing oPost = New Posts oPost.Name = "B" oPost.ID = 2 oPost.Selected = True oArr.Add(oPost) oPost = Nothing oPost = New Posts oPost.Name = "C" oPost.ID = 3 oPost.Selected = True oArr.Add(oPost) oPost = Nothing mestxt = "Original -> " For Each oPost In oArr mestxt = mestxt + oPost.Name & ", " Next MessageBox.Show(mestxt) oArr.Sort() oPost = Nothing oPost = New Posts mestxt = mestxt & "Sorted -> " For Each oPost In oArr mestxt = mestxt + oPost.Name & ", " Next MessageBox.Show(mestxt) End Sub End Class

                1 Reply Last reply
                0
                • G Geoff_3001

                  Sorry if this is elsewhere but I look and could not find. Can I / How do I create an arrarylist of an object. For instance for the below object Public Class Posts Public Name As String Public ID As Integer Public Selected As Boolean End Class Cheers Geoff

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

                  Hey I solved my own question. The problem with my code was I was returning 1 for greater than an zero for <= . The compare function should return 1 for greater than 0 for equal to and -1 for less than. Phew on to the next stage. Public Class Posts Implements IComparable Public Name As String Public ID As Integer Public Selected As Boolean Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim temp As Posts = CType(obj, Posts) Dim mestxt If Name > temp.Name Then Return 1 ElseIf Name = temp.Name Then Return 0 Else Return -1 End If End Function End Class

                  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