Null out original reference?

  • Thread starter Thread starter RMD
  • Start date Start date
R

RMD

I need to be able to keep a list of object references, and null them out one
by one at a later point in time. I realize this can be dangerous, but I have
my reasons.

I can't figure out, however, how to keep what is essentially a reference to
a reference. I want to store my objects in an ArrayList, then loop through
the array list and null out the original reference I was given when I added
it to my Array List.

Below is some example code:

//Here is some example usage
object nullMe;
Nuller nuller = new Nuller();

nullMe = new object();
nuller.AddToNullList(ref nullMe);
nuller.NullItOut();

//Here is my "Nuller" class:
public class Nuller
{
private ArrayList m_NullList = new ArrayList();

public void AddToNullList(ref object nullMe)
{
m_NullList.Add(nullMe);
}

public unsafe void NullItOut()
{
for(int i = 0; i < m_NullList.Count; i++)
{
//I want this to null out the original reference... the "nullMe"
variable in the usage example code.
m_NullList = null;
}
}
}

How do I accomplish this?

RMD
 
RMD said:
I need to be able to keep a list of object references, and null them out one
by one at a later point in time. I realize this can be dangerous, but I have
my reasons.

I can't figure out, however, how to keep what is essentially a reference to
a reference. I want to store my objects in an ArrayList, then loop through
the array list and null out the original reference I was given when I added
it to my Array List.

You can't, basically. Don't forget that the original variable may not
even exist any more - it could have been a local variable, or an
instance variable in an instance which has since been garbage
collected.
 
Basically, this is part of a O/R mapping project. I wanted to be able to
have a base class handle the management of all member variables of a domain
object. If you commit the domain object, it persists all the data for your
object as well as calling commit for any members that are also domain
objects.

So say one of those member domain objects was marked for deletion and was
deleted due to the commit. I wanted to detect that (I have an "OnCommit"
event that the parent domain object listens for from all its domain object
members), and null out the member variable. I can't expose properties for
each of these member variables (for a bunch of reasons), so I wanted to be
able to store a pointer to the original member and null out that memory
location when the event is fired.

Currently, I have the domain object listen for the events, and null out
their own members. This works fine, but I really wanted to get as much of
this generic code out of the domain object and into a base class. I want to
minimize the work somebody has to do to implement a domain object.

RMD
 
Back
Top