Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2008

Tip: Looking for answers? Try searching our database.

Is tag property ByRef or ByVal?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
martin.zugec@gmail.com - 20 Jan 2008 16:10 GMT
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
Herfried K. Wagner [MVP] - 20 Jan 2008 16:40 GMT
<martin.zugec@gmail.com> schrieb:
>I have project where I am assigning forms to Tag of different form
> (therefore creating some kind of relationship)...
[quoted text clipped - 3 lines]
>
> 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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jon Skeet [C# MVP] - 20 Jan 2008 19:08 GMT
> I have project where I am assigning forms to Tag of different form
> (therefore creating some kind of relationship)...
[quoted text clipped - 3 lines]
>
> 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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jack Jackson - 20 Jan 2008 19:22 GMT
>I have project where I am assigning forms to Tag of different form
>(therefore creating some kind of relationship)...
[quoted text clipped - 3 lines]
>
>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.
martin.zugec@gmail.com - 21 Jan 2008 16:58 GMT
> On Sun, 20 Jan 2008 08:10:51 -0800 (PST), martin.zu...@gmail.com
> wrote:
[quoted text clipped - 19 lines]
> 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???
Jon Skeet [C# MVP] - 21 Jan 2008 19:02 GMT
> thank for reply, that is little bit surprising behavior, but
> definitely something I was looking for...
>
> Is that behavior for all tags???

No, it's for everything. It's really important that you understand how
reference types work.

See http://pobox.com/~skeet/csharp/references.html

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jack Jackson - 21 Jan 2008 19:46 GMT
>> On Sun, 20 Jan 2008 08:10:51 -0800 (PST), martin.zu...@gmail.com
>> wrote:
[quoted text clipped - 26 lines]
>
>Is that behavior for all tags???

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.