Arrays
-
I am have problems adding records to an array. I am using a structure and an arraylist Code Snippet Public Persons As New ArrayList Public Structure Person Public First As String Public Last As String Public Home As String Public Work As String Public Mobile As String End Structure and the next block is to add a record to the list Code Snippet Dim p As New Person p.First = txtFirst2.Text p.Last = txtLast2.Text p.Home = txtHome2.Text p.Work = txtWork2.Text p.Mobile = txtMobile2.Text Persons.Add(p) lstPhone.Items.Add(p.First + " " + p.Last) The above code works but the trouble i'm having now is I cannot append to the array or save it. Can anyone help? Thanks all :((
Regards Zeldacat
-
I am have problems adding records to an array. I am using a structure and an arraylist Code Snippet Public Persons As New ArrayList Public Structure Person Public First As String Public Last As String Public Home As String Public Work As String Public Mobile As String End Structure and the next block is to add a record to the list Code Snippet Dim p As New Person p.First = txtFirst2.Text p.Last = txtLast2.Text p.Home = txtHome2.Text p.Work = txtWork2.Text p.Mobile = txtMobile2.Text Persons.Add(p) lstPhone.Items.Add(p.First + " " + p.Last) The above code works but the trouble i'm having now is I cannot append to the array or save it. Can anyone help? Thanks all :((
Regards Zeldacat
This code works just fine. I'm not sure what type of control lstPhone is, but this below code will work just fine. Question: Why are you using a Structure instead of a class? Question: Why are you using an ArrayList instead of a Generic List(Of Person)? This would provide a type safe collection.
Public Structure Person
Public First As String
Public Last As String
Public Home As String
Public Work As String
Public Mobile As String
End StructurePublic Class Class1
Public Persons As New ArrayList
Public Sub New()
Dim p As New Person
p.First = "hello"
p.Last = "There"
p.Home = "555-1212"
p.Work = "411"
p.Mobile = "900-555-1212"
Persons.Add(p)' this will work just fine 'I'm assuming that lstPhone is a Combo Box or List Box? 'lstPhone.Items.Add(p.First + " " + p.Last)
End Sub
End Class
Cheers, Karl
My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
-
This code works just fine. I'm not sure what type of control lstPhone is, but this below code will work just fine. Question: Why are you using a Structure instead of a class? Question: Why are you using an ArrayList instead of a Generic List(Of Person)? This would provide a type safe collection.
Public Structure Person
Public First As String
Public Last As String
Public Home As String
Public Work As String
Public Mobile As String
End StructurePublic Class Class1
Public Persons As New ArrayList
Public Sub New()
Dim p As New Person
p.First = "hello"
p.Last = "There"
p.Home = "555-1212"
p.Work = "411"
p.Mobile = "900-555-1212"
Persons.Add(p)' this will work just fine 'I'm assuming that lstPhone is a Combo Box or List Box? 'lstPhone.Items.Add(p.First + " " + p.Last)
End Sub
End Class
Cheers, Karl
My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
Sorry Karl I made a bit of a boo boo here. I forgot to add the fact, i'm using a .csv file to append the new persons data to. I might get a thread going properly one day. :confused:
Regards Zeldacat
-
Sorry Karl I made a bit of a boo boo here. I forgot to add the fact, i'm using a .csv file to append the new persons data to. I might get a thread going properly one day. :confused:
Regards Zeldacat
Cedrickdeorange wrote:
I forgot to add the fact, i'm using a .csv file to append the new persons data to.
How does this change anything? You can read your .csv file, record by record and still load a Generic Collection List(Of Person). Am I missing something?
Cheers, Karl
My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
-
Cedrickdeorange wrote:
I forgot to add the fact, i'm using a .csv file to append the new persons data to.
How does this change anything? You can read your .csv file, record by record and still load a Generic Collection List(Of Person). Am I missing something?
Cheers, Karl
My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
:confused: No. I know where you're going with this now. I had another look at the first post again. I wasn't thinking that way at the time. As i said it all makes sense now. Thanks.
Regards Zeldacat