[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Tag and Object

Posted on 2012-08-30
6
Medium Priority
?
367 Views
Last Modified: 2012-10-26
Experts,

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?
0
Comment
Question by:San24
6 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 500 total points
ID: 38351983
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?
0
 

Author Comment

by:San24
ID: 38352040
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.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 38352422
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.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 38353169
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.
0
 

Author Comment

by:San24
ID: 38354011
@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.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 38354204
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

834 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