474,050 Members | 66,967 Online
Bytes | Software Development & Data Engineering Community
+ 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.AddToNul lList(ref nullMe);
nuller.NullItOu t();

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

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

public unsafe void NullItOut()
{
for(int i = 0; i < m_NullList.Coun t; 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
Nov 15 '05 #1
6 1794
> 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
Nov 15 '05 #2
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 "AddToNullL ist" 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" <jd*****@atrevi do.nospam.net> wrote in message
news:eP******** ******@tk2msftn gp13.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.


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

Nov 15 '05 #3
RMD <rm*@nospam.sor ry.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
RMD
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
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
RMD <rm*@nospam.sor ry.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
> 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.WriteLi ne("--- called from a baseclass object ---");
nullThisOut(b);

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

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

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

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

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

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

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

Hope that helps,
-JG
Nov 15 '05 #6
RMD
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" <jd*****@atrevi do.nospam.net> wrote in message
news:#A******** ******@tk2msftn gp13.phx.gbl...
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.WriteLi ne("--- called from a baseclass object ---");
nullThisOut(b);

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

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

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

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

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

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

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

Hope that helps,
-JG

Nov 15 '05 #7

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

Similar topics

2
1736
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, 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...
15
2269
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 k }
10
11528
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 { private Dummy(){} } public struct tInt {
7
1646
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 every time an event is raised: if (SomethingChanged != null) // wasted code { // more waste SomethingChanged(...)
6
10095
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) the calendar control in asp.net doesn't seem to have a selectedDate = null property
4
1940
by: Dixon | last post by:
wats the Diff Between Setting an object to NULL and calling the Dispose() method for that object?
12
16803
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;
6
5977
by: doncee | last post by:
I have a list box that is generated on a form by way of a Parameter Query. Problem is whenever I try to refer to the list box, i.e., to update the underlying table, I am getting a "null" value from any row or field I refer to. Have tried using "after update" events as well as a button click event to no avail. The debug window always shows "null" values when I bring it up while trying to examine my code\events. Is there a way to get back to...
4
2502
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 m_Token = null in order to destroy the session token when the user logs out. However, I'm finding that setting class instance to null results in no header being sent back to the client, with the result that the client actually remains with an...
19
2753
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 string. When we call x.ToString we are really calling a function like this: class Object
0
10557
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10362
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
12057
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7884
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6675
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5432
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3989
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.