Creating an arraylist of an object
-
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
-
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
Add a method that creates the ArrayList:
Public Class Posts
Public Name As String
Public ID As Integer
Public Selected As BooleanPublic 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);
-
Add a method that creates the ArrayList:
Public Class Posts
Public Name As String
Public ID As Integer
Public Selected As BooleanPublic 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);
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
-
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
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
-
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
Aah Clear concise and I even understand Many Thanks will give it a go. Cheers
-
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
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
-
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
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
-
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
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