Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is the .Tag property a reference or an instance of an object?

Status
Not open for further replies.

joelwenzel

Programmer
Joined
Jun 28, 2002
Messages
448
Some controls have a Tag property that you can set to equal an object. Does this create a copy of the object of just a reference to the original?
 
The .Tag property is for saving extra information to a control. You can use it the way you would use a public variable, or as you want/need. It is an optional property and can be used for get/set at run-time.
 
Agree.
Tag is a member of an object's properties. Setting it's value will not create a copy of the object.
 
I think what joelwenzel is asking is - Does changing the value you set in Tag reflect on the original variable you used to set the Tag?

The answer is the value stored in Tag is just a copy - for a quick example:
Code:
Dim x As Integer = 5
Button1.Tag = x
Button1.Tag = 2
MsgBox(x)
While the tag does change - x does not and the MsgBox shows 5.
 
ok, I've been experimenting with the tags and I think they store references to the original object, not a copy...because if I change a setting use the object stored in the tag, the original changes as well.

Thanks everyone for your input.
 
I would say a copy of an object. Try Borvik's code to see that the message box shows 5. Then try mine to see that it shows 5 again:


Dim x As Integer = 5
TextBox1.Tag = x
x = 2
MessageBox.Show(TextBox1.Tag)
 
'Integer is a value type therefore it is a copy. If you used a reference you would see a 2

Public Class foo
Public num As Integer
End Class

Dim f As foo = New foo
f.num = 5
TextBox1.Tag = f
f.num = 2
MsgBox(f.num)
 
Thanks for pointing that out stsuing!

Although just to point out - your MsgBox is displaying the exact one you changed according to that code so of course that will work. You would want to display the TextBox1.Tag.num according to your code - which does indeed change by reference.
 
yes integer is a copy. I'm using objects though
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top