473,544 Members | 713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting a referenced object to null

Joe
I might be overworked so please excuse this stupid question...

Say I do the following:

DataTable table = new DataTable();

myDataAdaptor.F ill(table);

dataGrid1.DataS ource = table;

table = null;

Why does dataGrid1.DataS ource still point to a table?

How can I set an object to null and have all references to that object now
be null?

Thanks,
Joe
Feb 20 '06 #1
12 16739
Joe,
dataGrid1.DataS ource = table;

table = null;

Why does dataGrid1.DataS ource still point to a table?
Because DataSource has its own copy of the reference to the table.
How can I set an object to null and have all references to that object now
be null?


You can't. I frankly non of the languages that I know can do that unless the
classes are specially designed to use some kind of indirect reference where
they reference an object that holds the real reference. In this case
modifying the reference in this "proxy" class will cut off all the
references to the real object.
--

Stoitcho Goutsev (100)
Feb 20 '06 #2
"Joe" <jb*******@noem ail.noemail> a écrit dans le message de news:
Oz************* *@TK2MSFTNGP12. phx.gbl...

|I might be overworked so please excuse this stupid question...
|
| Say I do the following:
|
| DataTable table = new DataTable();
|
| myDataAdaptor.F ill(table);
|
| dataGrid1.DataS ource = table;
|
| table = null;
|
| Why does dataGrid1.DataS ource still point to a table?
|
| How can I set an object to null and have all references to that object now
| be null?

You can't. You can only set references to objects to null.

If you know C tthen you would remember that object references hold the
addresses of objects and you would have to dereference that pointer to get
at the object. You could "delete" the object and then the pointer would be
invalid and would have to be nulled to avoid AVs.

In C#, all objects are garbage collected, you can't "delete" them. When the
last reference to a given object falls out of scope, the object is liable to
collection. You can certainly null as many references as you can find, but
the object will remain alive as long as *any* reference still holds that
object.

In your example, the DataGris's DataSource is holding a reference to the
table, so even though you null the original reference, 'table', the object
that 'table' was pointing to is kept alive by the DataSource property.

Why would you want to null a property to which you have only just assigned a
valid object ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 20 '06 #3
Joe
Hi,

Your answer is what I thought. I just wanted to make sure I wasn't loosing
my mind...

BTW - The example I wrote was just to show a really small and quick example
of what I meant. I don't think anyone would ever do as my example displayed.

-Joe

"Joanna Carter [TeamB]" <jo****@not.for .spam> wrote in message
news:uC******** *****@TK2MSFTNG P12.phx.gbl...
"Joe" <jb*******@noem ail.noemail> a écrit dans le message de news:
Oz************* *@TK2MSFTNGP12. phx.gbl...

|I might be overworked so please excuse this stupid question...
|
| Say I do the following:
|
| DataTable table = new DataTable();
|
| myDataAdaptor.F ill(table);
|
| dataGrid1.DataS ource = table;
|
| table = null;
|
| Why does dataGrid1.DataS ource still point to a table?
|
| How can I set an object to null and have all references to that object
now
| be null?

You can't. You can only set references to objects to null.

If you know C tthen you would remember that object references hold the
addresses of objects and you would have to dereference that pointer to get
at the object. You could "delete" the object and then the pointer would be
invalid and would have to be nulled to avoid AVs.

In C#, all objects are garbage collected, you can't "delete" them. When
the
last reference to a given object falls out of scope, the object is liable
to
collection. You can certainly null as many references as you can find, but
the object will remain alive as long as *any* reference still holds that
object.

In your example, the DataGris's DataSource is holding a reference to the
table, so even though you null the original reference, 'table', the object
that 'table' was pointing to is kept alive by the DataSource property.

Why would you want to null a property to which you have only just assigned
a
valid object ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Feb 20 '06 #4
Joe,

The statement:

DataTable table = new DataTable();

creates a new DataTable object in the heap, and stores a reference to this
object in the variable 'table'.

The statement:

dataGrid1.DataS ource = table;

COPIES the reference stored in the 'table' var to dataGrid1.DataS ource.

The statement:

table = null;

assigns null to 'table', so 'table' does not point anymore to the
dynamically created object; but dataGrid1.DataS ource still points at it.

There is no direct way to know from what different places an object might be
referenced. The CLR keeps track of that, and will automatically reclaim the
memory used by the object when no references points at it.

Regards - Octavio
"Joe" <jb*******@noem ail.noemail> escribió en el mensaje
news:Oz******** ******@TK2MSFTN GP12.phx.gbl...
I might be overworked so please excuse this stupid question...

Say I do the following:

DataTable table = new DataTable();

myDataAdaptor.F ill(table);

dataGrid1.DataS ource = table;

table = null;

Why does dataGrid1.DataS ource still point to a table?

How can I set an object to null and have all references to that object now
be null?

Thanks,
Joe

Feb 20 '06 #5
"Joe" <jb*******@noem ail.noemail> a écrit dans le message de news:
OT************* *@TK2MSFTNGP14. phx.gbl...

| BTW - The example I wrote was just to show a really small and quick
example
| of what I meant. I don't think anyone would ever do as my example
displayed.

Just checking :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 20 '06 #6
Joe <jb*******@noem ail.noemail> wrote:
I might be overworked so please excuse this stupid question...

Say I do the following:

DataTable table = new DataTable();

myDataAdaptor.F ill(table);

dataGrid1.DataS ource = table;

table = null;

Why does dataGrid1.DataS ource still point to a table?

How can I set an object to null and have all references to that object now
be null?


The important thing is that you're not setting an object to null.
You're not even setting a reference to null, really.

You're changing the variable's value to null. That doesn't affect
anything other than the value of that variable - so the fact that
dataGrid1.DataS ource happened to have a reference value which was the
same as the value of table is irrelevant.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #7
Question:

If I create an instance of a value type object (int for example), that
value is stored directly on the stack is it not? And my variable
points directly at the location in the stack where the integer value is
stored? Is that correct?

If I create an instance of a reference type, the object itself is
created on the heap correct? And the variable holds the address of
that object on the heap. Where is the value of the variable stored?
Is it stored on the heap as well? Or is it stored on the stack?

Feb 21 '06 #8
Chris Dunaway <du******@gmail .com> wrote:
Question:

If I create an instance of a value type object (int for example), that
value is stored directly on the stack is it not?
Not always - it depends on the type of the variable.
And my variable
points directly at the location in the stack where the integer value is
stored? Is that correct?
If it's a local variable, yes.
If I create an instance of a reference type, the object itself is
created on the heap correct?
Yes.
And the variable holds the address of that object on the heap.
Well, that's the value of the variable.
Where is the value of the variable stored?
Is it stored on the heap as well? Or is it stored on the stack?


It depends on whether the variable is a local variable, an instance
variable etc. See http://www.pobox.com/~skeet/csharp/memory.html

Where a variable's value is stored does *not* depend on the type of the
variable (in terms of whether it's a reference type or a value type).
It only depends on what kind of variable it is (local, instance, static
etc).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 21 '06 #9
You can force the garbageCollecto r to free up the memory.
But you HAVE to remove ANY refenrence to the table object. If the table
object has to references it is eligble for GarbageCollecto r.

GarbageCollecto r is a complicated matter, but you SELDOM need to call it
manually. It is tweaked at the best to release memory when needed.

Your answere is that it is doable, but the time and effort to do it is not
worth it.
"Joe" <jb*******@noem ail.noemail> wrote in message
news:Oz******** ******@TK2MSFTN GP12.phx.gbl...
I might be overworked so please excuse this stupid question...

Say I do the following:

DataTable table = new DataTable();

myDataAdaptor.F ill(table);

dataGrid1.DataS ource = table;

table = null;

Why does dataGrid1.DataS ource still point to a table?

How can I set an object to null and have all references to that object now
be null?

Thanks,
Joe

Feb 21 '06 #10

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

Similar topics

7
2463
by: Ricardo Luceac | last post by:
I'm geting a object null reference in this method SqlConnection cnn = new SqlConnection("server=(local);database=varired;trusted_connection=true") ; SqlCommand cmm = new SqlCommand(); cmm.Connection=cnn; cnn.Open(); cmm.CommandText="Delete from estados where id_estado="+Convert.ToInt32(lstEsts.SelectedItem.Value);
4
1907
by: Dixon | last post by:
wats the Diff Between Setting an object to NULL and calling the Dispose() method for that object?
5
3147
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the client/user code be responsible for doing it? On what basis should a decision be made favouring either case ?
1
1801
by: Sparky74 | last post by:
Hi Everybody. I have been searching for many hours for an answer to this problem. I hope somebody can help me. I have a C# .NET client application that connects to a TCP/IP C++ server application that I have maintained for a number of years. What I am finding is that Socket.Close() does not actually sever the client connection from my...
20
13289
by: Justin Rich | last post by:
so im trying to be good and not leave anything hanging open but i guess ive seen a variety of ways to kill objects.. is there like a recommended way? basically im looking for best practices for handling object destruction. Thanks Justin
27
2154
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I thought "void ** " should do the trick, but I kept getting the following warning message from gcc: warning: passing argument 1 of 'free_var' from...
6
1580
by: sandy | last post by:
Hello, My understanding about objects is that they are reference types and not value types but a simple code seems to defy my whole understanding. I wrote a simple console app: class Program {
3
8131
karthickkuchanur
by: karthickkuchanur | last post by:
Dear Experts, Please let me know what is the difference between difference between object !=null and null !=object,I already google it but i can't able to find the right answer
0
7376
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...
1
7395
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...
0
7722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5310
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4932
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...
0
3433
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...
0
3425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1851
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
0
679
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...

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.