I have a form that contains an object “TextBox1” (TextBox1 control)
In the code section I’ve initialized a new TextBox object that is not on the form like this:
Dim aa As New TextBox
aa = TextBox1 'THE CONTROL ON THE FORM
aa.Text = "hi how are you?"
The TextBox1 on the form is now changed it wrote “hi how are you?”
shouldn’t be the “aa” object and the “TextBox1” be separate one from another? means that changing one object wouldn’t affect the other?
Why this happens?
And how to prevent this?
Means Separating the objects one from another.
Writing the code at this form
Public Sub blah(ByVal aa As TextBox)
aa.Text = "hi how are you?"
End Sub
And then calling the sub by
blah(TextBox1)
Doesn’t solve the problem.
.Net objects are passed by reference.
aaandTextBox1both refer to the sameTextBoxinstance.You can manually create a copy of an instance by copying over its properties to a different instance.