Link to home
Avatar of Matb396
Matb396

asked on

MailMessages linking...

Hi All

I have a vb.net program that has the following code:
 

  Public Sub sendmailto(ByVal toemail As MailAddress)
Dim reMail As New MailMessage
        reMail = Me.email
        reMail.To.Add(toemail)
***then a load of other stuff***
End Sub

Open in new window


me.email is a property within this class:

    Private_mail As New MailMessage
    Public Property email() As MailMessage
        Get
            Return _mail
        End Get
        Set(ByVal value As MailMessage)
            _mail = value
        End Set
    End Property

Open in new window


why is that when i change something on reMail (e.g.reMail.To.Add(toemail)) the me.email updates aswell?

Anyideas?

Cheers
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Because its a Reference type not value type. With reference type, when you do something like

reMail = Me.email

You are actually copying memory address of one object from one variable to the other but object remains the same (single).

Some types implement IClone interface so you can call the .Clone() method to create a real copy of the object.
Avatar of Matb396
Matb396

ASKER

Ok but how exactly do I do this? MailMessage has no .clone method
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial