Null out original reference?

  • Thread starter Thread starter Juan Gabriel Del Cid
  • Start date Start date
J

Juan Gabriel Del Cid

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.

Well, it's really not that dangerous so much as it is completely useless.
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.

[SNIP]

How do I accomplish this?

When your variable (nullMe in your example) goes out of scope, it
automatically ceases to exist. As part of it's non-existance, it doesn't
reference your object anymore. This also means that you CANNOT null it
out... it's already gone!

Why exactly do you need to null references out anyway?

-JG
 
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
 
Actually, it's not completely useless. The code I pasted was an example, not
what I'm actually doing.

My scenario is far too complicated to explain completely, but the gist of it
is that I need to null out private member variables of a class from one of
it's base classes. An event occurs, and the base class handles the event.
Part of the handler is to loop through some "registered" variables
(variables which were passed to a function like the "AddToNullList" method
in my example by the class in question), and null them out. I need to null
the actual member variables, not simply my local stack-based reference.

RMD


Juan Gabriel Del Cid 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.

Well, it's really not that dangerous so much as it is completely useless.
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.

[SNIP]

How do I accomplish this?

When your variable (nullMe in your example) goes out of scope, it
automatically ceases to exist. As part of it's non-existance, it doesn't
reference your object anymore. This also means that you CANNOT null it
out... it's already gone!

Why exactly do you need to null references out anyway?

-JG
 
RMD said:
Actually, it's not completely useless. The code I pasted was an example, not
what I'm actually doing.

My scenario is far too complicated to explain completely, but the gist of it
is that I need to null out private member variables of a class from one of
it's base classes.

That sounds like a very strange design. What's the bigger picture here?

Given that you're just not going to be able to do it this way, if you
post some more information we may be able to suggest some better ways
of accomplishing the same grander aim.
 
Well, it wasn't really my intention to design it this way... it just kinda
happened. :-)

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
 
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.

Well, this is more understandable. This is quite simple to do, in fact. Have
the DomainObject define a (virtual) method that nulls out it's members (if
there are any). Then any class that inherits from DomainObject can
*override* this method to null out it's own members (and optionally null out
it's base class members too). It doesn't matter if the event is handled by
the base class or the subclass... that's the beauty of Polymorphism.

Here's an example of how it would work:

/////////////////////
/// IneritanceTest.cs
///
using System;

namespace Tests {
public class TestClass {
public static void Main(string []args) {
BaseClass b = new BaseClass();
SubClass s = new SubClass();

Console.WriteLine("--- called from a baseclass object ---");
nullThisOut(b);

Console.WriteLine("--- called from a subclass object ---");
nullThisOut(s);
}

public static void nullThisOut(BaseClass obj) {
obj.nullMeOut();
}
}

public class BaseClass {
private String baseClassMember = "something";

public virtual void nullMeOut() {
Console.WriteLine("Nulling out BaseClass Member: {0}",
baseClassMember);
this.baseClassMember = null;
}
}

public class SubClass : BaseClass {
private String subClassMember = "something else";

public override void nullMeOut() {
Console.WriteLine("Nulling out SubClass Member: {0}",
subClassMember);
this.subClassMember = null;

// null out the parent's members too
base.nullMeOut();
}
}
}

Hope that helps,
-JG
 
Yeah, this is basically the solution I've been using. I just wanted to have
it done without the inheriting domain object having to do anything.

Oh well. Close enough. :-)

Thanks,
RMD

Juan Gabriel Del Cid said:
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.

Well, this is more understandable. This is quite simple to do, in fact. Have
the DomainObject define a (virtual) method that nulls out it's members (if
there are any). Then any class that inherits from DomainObject can
*override* this method to null out it's own members (and optionally null out
it's base class members too). It doesn't matter if the event is handled by
the base class or the subclass... that's the beauty of Polymorphism.

Here's an example of how it would work:

/////////////////////
/// IneritanceTest.cs
///
using System;

namespace Tests {
public class TestClass {
public static void Main(string []args) {
BaseClass b = new BaseClass();
SubClass s = new SubClass();

Console.WriteLine("--- called from a baseclass object ---");
nullThisOut(b);

Console.WriteLine("--- called from a subclass object ---");
nullThisOut(s);
}

public static void nullThisOut(BaseClass obj) {
obj.nullMeOut();
}
}

public class BaseClass {
private String baseClassMember = "something";

public virtual void nullMeOut() {
Console.WriteLine("Nulling out BaseClass Member: {0}",
baseClassMember);
this.baseClassMember = null;
}
}

public class SubClass : BaseClass {
private String subClassMember = "something else";

public override void nullMeOut() {
Console.WriteLine("Nulling out SubClass Member: {0}",
subClassMember);
this.subClassMember = null;

// null out the parent's members too
base.nullMeOut();
}
}
}

Hope that helps,
-JG
 
Back
Top