MailMessages linking...

Matb396
Matb396 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
Ok but how exactly do I do this? MailMessage has no .clone method
Most Valuable Expert 2012
Top Expert 2014
Commented:
Then you have to manually copy properties from one object to the other

reMail.subject = me.email.subject
etc

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial