Is tag property ByRef or ByVal?

  • Thread starter Thread starter martin.zugec
  • Start date Start date
M

martin.zugec

I have project where I am assigning forms to Tag of different form
(therefore creating some kind of relationship)...

My problem is quite simple - when one of forms is disposed, tag still
contains that object.

So my question is quite simple - is Tag ByRef or ByVal???

Thanks,
Martin
 
I have project where I am assigning forms to Tag of different form
(therefore creating some kind of relationship)...

My problem is quite simple - when one of forms is disposed, tag still
contains that object.

So my question is quite simple - is Tag ByRef or ByVal???

It depends on the type of the object you assign. If it is an instance of a
value type, the object is lost, if it is in instance of a reference type, it
still exists.
 
I have project where I am assigning forms to Tag of different form
(therefore creating some kind of relationship)...

My problem is quite simple - when one of forms is disposed, tag still
contains that object.

So my question is quite simple - is Tag ByRef or ByVal???

Well, there are two things here - the behaviour of properties, and what
"ByRef" and "ByVal" really mean.

Properties are always effectively "ByVal" - but if you set a property
value to be a reference, then the value really is just the reference,
so modifications to the object it refers to will be visible however you
refer to it.

It's probably worth having a look at this page about parameter passing
- it's in C#, but the concepts apply to VB as well.

http://pobox.com/~skeet/csharp/parameters.html
 
I have project where I am assigning forms to Tag of different form
(therefore creating some kind of relationship)...

My problem is quite simple - when one of forms is disposed, tag still
contains that object.

So my question is quite simple - is Tag ByRef or ByVal???

When you assign a form (or any object) reference to a Tag property,
the Tag property then contains a reference to the object. Neither the
form nor any other entity has any knowledge that the Tag property
contains a reference to the form (except the garbage collector, who
will notice the reference and not garbage collect the form), and
nothing in .NET will clear the Tag property when the form is disposed.

When you dispose of the object that the Tag references, the object is
disposed but will not be available for garbage collection because of
the outstanding reference. If you try to use the reference after the
object is disposed, you will get errors. You must clear the Tag
property to allow the object to be garbage collected.
 
When you assign a form (or any object) reference to a Tag property,
the Tag property then contains a reference to the object.  Neither the
form nor any other entity has any knowledge that the Tag property
contains a reference to the form (except the garbage collector, who
will notice the reference and not garbage collect the form), and
nothing in .NET will clear the Tag property when the form is disposed.

When you dispose of the object that the Tag references, the object is
disposed but will not be available for garbage collection because of
the outstanding reference.  If you try to use the reference after the
object is disposed, you will get errors.  You must clear the Tag
property to allow the object to be garbage collected.

Hi Jack,

thank for reply, that is little bit surprising behavior, but
definitely something I was looking for...

Is that behavior for all tags???
 
Hi Jack,

thank for reply, that is little bit surprising behavior, but
definitely something I was looking for...

Is that behavior for all tags???

It is the behavior for all properties. There is nothing special about
Tag.
 
Back
Top