Tag and Object

Posted on 2012-08-30
Last Modified: 2012-10-26

I`m using C# .Net 3.5 VS 2008

Lets say I assign the Tag property of an object to a custom object, if I destroy the object the custom object should also be destroyed right?

For example : I assign a Custom Object - CarObj to a Treeview Node.

TreeNode.Tag = CarObj;

If I remove the TreeNode, the CarObj should also be removed right? From what I see CarObj still exists, How do I solve this issue?
Question by:San24
    LVL 20

    Assisted Solution

    The CarObj should not be removed.

    Consider this.

    You have a datatable and you assign it as a datasource to the gridview.

    Now if you set the datasource of gridview to null or delete a row from gridview it won't affect the datatable. right?

    Author Comment

    Thanks infomaniac. The issue is resolved.

    I got confused, I have assigned objects to items in dictionary before and when i remove the entry the object ceases to exist.
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    The CarObj should not be removed.
    I have to disagree slightly. If the Tag property is the only reference to the CarObj, then the CarObj would be eligible for garbage collection once the TreeNode is destroyed.
    LVL 40

    Assisted Solution

    by:Jacques Bourgeois (James Burger)
    The object will be destroyed when all references are destroyed. That means that you need to get rid of both the Tag property (or the Node or the TreeView), and the variable CarObj, as well as any other reference that could have been made to that object.

    If any reference "lives", then the object won't be destroyed.

    Author Comment

    @Kaufmed - Doesn`t Node.Remove() destroy the TreeNode?

    Getting rid of the TreeView is not an option. But I resolved this issue by casting the Tag as an object and removing it. Let me know if you are interested in the solution.
    LVL 40

    Accepted Solution

    One thing that everybody forgot here, including me, is that objects are not actually "destroyed" when we get rid of them. The reference (variable, pointer) is destroyed, but the memory is not reclaimed.

    .NET memory cleaning is managed by the garbage controller, an important piece of the framework. Setting an object to Nothing simply breaks the link between the variable and any reference. When all those links have been broken, the garbage collector knows that the object is not used any more and that the memory it takes is available if needed.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now