Avatar of 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
***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
            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?


Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon
Nasir Razzaq

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.

Ok but how exactly do I do this? MailMessage has no .clone method
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.