How to add multiple instance of Data class to another class

I have 2 classes similar to the 2 below where a user can have multiple emails, so how would i do this ? Would it be an array like "Private _Email As email()" or a List( of email)
and how to i add new email to the user class ?

Public Class user

Private _Fname As String = ""
Private _Mname As String = ""
Private _Lname As String = ""
Private _Email As ?? 


Public Property Fname As String
Get
Return _Fname
End Get
Set(ByVal Value As String)
_Fname = Value
End Set
End Property

Public Property Mname As String
Get
Return _Mname
End Get
Set(ByVal Value As String)
_Mname = Value
End Set
End Property

Public Property Lname As String
Get
Return _Lname
End Get
Set(ByVal Value As String)
_Lname = Value
End Set
End Property

Public Property Email As String
Get
Return _Email
End Get
Set(ByVal Value As String)
_Email = Value
End Set
End Property


End Class

Open in new window


Public Class email

Private _Nickname As String = ""
Private _Emailaddress As String = ""
Private _Default As Int32 = "0"


Public Property Nickname As String
Get
Return _Nickname
End Get
Set(ByVal Value As String)
_Nickname = Value
End Set
End Property

Public Property Emailaddress As String
Get
Return _Emailaddress
End Get
Set(ByVal Value As String)
_Emailaddress = Value
End Set
End Property

Public Property Default As Int32
Get
Return _Default
End Get
Set(ByVal Value As Int32)
_Default = Value
End Set
End Property


End Class

Open in new window

Confused CoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Set your User.Email property as a collection, for example List<Email>. You should really instantiate this property to an empty List, and make it readonly. This way, the consumer of your class can only add and remove from the list, but not overwrite it with a completely new list.

Public Class user
    Public Property FName As String
    Public Readonly Property Email As List(Of Email) = New List(Of Email)
End Class

Public Class email
    Public Property NickName As String
    Public Property EmailAddress As String   
End Class

Open in new window

And then to use it you create a new Email instance and add it to the Email property:

Dim user As New user
user.FName = "Chris"

user.Email.Add( New Email() With { . NickName = "Chris", .EmailAddress = "chris@example.com" } )
	
Dim anotherEmail = New Email()
anotherEmail.NickName = "John"
anotherEmail.EmailAddress = "john@example.com"
	
user.Email.Add(anotherEmail)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Confused CoderAuthor Commented:
Thanks, that did the job. I had it setup that way but was missing the " = New List(Of Email)" so i got an error every time i tried to add a new object.
0
Chris StanyonWebDevCommented:
Good stuff.

If you don't instantiate the list, then it will be Null, so trying to add to Null with give your errors.

Glad you got it sorted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.