?
Solved

object lifespan

Posted on 2006-10-26
7
Medium Priority
?
367 Views
Last Modified: 2010-04-16
Hi have a question about an objects lifespan.

I have a static method in a class that returns an object to a null object.

MyClass myclass;

myclass = ClassServices.GetClass(1);

What happens to the object that got created in the GetClass method? Does it get destoyed after being assigned to 'myclass'?




0
Comment
Question by:tmueller18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 17815728
No. Your method will return a reference to the object which will be stored in the variable myclass. The object will not be destroyed until sometime after the last reference to it goes out of scope. So in your case it won't be destroyed until at least the time that myclass goes out of scope. Unless of course you are returning a clone of the object rather than a simple reference in which case the original cloned object will drop out of scope at the end of the GetClass method (assuming it isn't a static object and isn't referenced from anywhere else).
0
 

Author Comment

by:tmueller18
ID: 17815776
the GetClass static method likes like this

public static PersonClass GetClass(int ID)
{
      PersonClass result= new  PersonClass();
      return result;
}

---------------------------
calling page:
----------------------------
PersonClass myPersonClass;
myPersonClass = PersonServices.GetClass(3);


what happens to 'result'?

From what you said the result object doesnt live outside the scope of the method. Is this correct?

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17815817
"result" isn't actually an object, it is a variable that holds a reference to an object.

"result" will drop out of scope at the end of the method, but the object you are returning will not because a reference to the object is still being held by myPersonClass outside of the GetClass method. The actual object being referenced will not drop out of scope until at least the time that the "myPersonClass" variable drops out of scope.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tmueller18
ID: 17815846
"result" isn't actually an object, it is a variable that holds a reference to an object.

'result'  is an object since it was instantiated.

'result' is passed to the variable 'myPersonClass'

result drops out of scope now?



0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17815886
"result" isn't an object, "result" simply points to an object.

Think of your object as a balloon; "result" is a piece of string through which you can get to the balloon, when you return from your method you are tying another piece of string (the "myPersonClass" variable) to the balloon. When the method exits the "result" piece of string is cut off and destroyed, the balloon stays exactly where it was and is still being referenced by the "myPersonClass" variable.

So, the "result" variable drops out of scope when the method exits, but the actual object you created still remains in memory and is now referenced by the "myPersonClass" variable.
0
 

Author Comment

by:tmueller18
ID: 17815971
so when i create an object like this:

Foo f = new Foo();

f isnt an object but a pointer to an object thats created in memory?

i understand the baloon part. thanks for explaing the scope of the variable

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17818109
Yes, basically.

f is a variable that holds a reference to an object of type Foo. then the "new Foo()" part creates an object in memory somewhere and stores the address for that block of memory in the variable f.
0

Featured Post

Technology Partners: 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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