Hi I have a little problem with a list of custom objects i wrote. When I use the list.add(xxx) method it doesn’t simply append the xxx object to my list, but it turns each items to xxx and I don’t know how to fix it.
Here is the declaration of my custom class:
Public Class User
Private Shared n As String
Public Shared Property Name() As String
Get
Return n
End Get
Set(ByVal value As String)
n = value
End Set
End Property
Sub New(ByVal name As String)
User.Name = name
End Sub
End Class
And here’s where I call the list.add method
Public Class Form1
Private Sub subname
Dim temp As New User
Dim data As New List(Of User)
For Each item As String In ListBox1.Items
data.Add(New User(item))
Next
End Sub
End Sub
P.S. Yes, I’ve already read some posts about people having the same problem but did not understand how to apply their solution to my project.
Your problem is the
Sharedproperty and private member. ASharedmember is shared among all instances of the class. If you set it in one instance it will be the same for all instances.Remove the keyword
Sharedand it should work as expected.