473,480 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Null out original reference?

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[i] = null;
}
}
}

How do I accomplish this?

RMD
Jul 21 '05 #1
2 1693
You *may* be looking for System.WeakReference but I'm not sure exactly what
you're trying to achieve here.

To adapt your code slightly:

{
object nullMe;
nullMe = new object();
// If you never use the object pointed to by nullMe, it may be collected
from here on.
// Code not involving that object...
}

So where's the need?
"RMD" <rm*@nospam.sorry.com> wrote in message
news:Os**************@TK2MSFTNGP09.phx.gbl...
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[i] = null;
}
}
}

How do I accomplish this?

RMD

Jul 21 '05 #2
RMD
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

"Stu Smith" <st*****@remove.digita.com> wrote in message
news:Oy**************@tk2msftngp13.phx.gbl...
You *may* be looking for System.WeakReference but I'm not sure exactly what you're trying to achieve here.

To adapt your code slightly:

{
object nullMe;
nullMe = new object();
// If you never use the object pointed to by nullMe, it may be collected from here on.
// Code not involving that object...
}

So where's the need?
"RMD" <rm*@nospam.sorry.com> wrote in message
news:Os**************@TK2MSFTNGP09.phx.gbl...
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[i] = null;
}
}
}

How do I accomplish this?

RMD


Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2202
by: JKop | last post by:
Does that Standard explicitly forbid the initiation of a null reference? Is there anything wrong with the following code?: void Blah( std::string const &k ) { if ( !&k ) return; // work with...
10
11444
by: John | last post by:
Ok. I'm not sure whether this is cool or perverted, I need second opinion ;) I define two classes as follows: /********************************************/ public sealed class Dummy {...
7
1625
by: Kevin Cline | last post by:
Why, oh why is it necessary to test an event for null before raising it? Why isn't that case handled automatically, instead of forcing developers to write three lines of wasted boilerplate code...
6
10068
by: evolve | last post by:
why doesn't c# seem to support some kind of 'null date' for instance: i have an application where the user is not obliged to enter a date until an event happens (e.g. a bug was fixed on xyz) ...
2
267
by: RMD | last post by:
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,...
4
1898
by: Dixon | last post by:
wats the Diff Between Setting an object to NULL and calling the Dispose() method for that object?
12
16728
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
4
2458
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
19
2698
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason the behaviour couldn't be to return an empty...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.