Hashtable : how to add a reference ?

C

cybertof

Hi !

I have a class instance named MyObject (instance of CObject class).

I would like to add a reference (not a copy) to it as an hashtable
value.

I use this code :

MyHashTable.Add("key1", MyObject)


If I do the following :

MyHashTable["key1"] = null

only the object in the hashtable is 'nulled'.


Is there a special syntax to add the reference and not a copy of the
object ?


Regards;
Cybertof.
 
G

Guest

Hi cybertof,
Hi !

I have a class instance named MyObject (instance of CObject class).

I would like to add a reference (not a copy) to it as an hashtable
value.

I use this code :

MyHashTable.Add("key1", MyObject)

This is what you want to do.
Your hashtable contains reference *MyObject* with key: "key1".
If I do the following :

MyHashTable["key1"] = null

only the object in the hashtable is 'nulled'.

This means that your *MyObject* reference was removed from
position marked by "key1".
Is there a special syntax to add the reference and not a copy of the
object ?

Your object *MyObject* is not copied in your example.

Regards

Marcin
 
C

cybertof

Yes it is, because when i 'null' it, the 'value' in the hashtable is
effectively null, but the original MyObject still exists.

Any Idea ?

Regards,
Cybertof.
 
J

Jon Skeet [C# MVP]

cybertof said:
Yes it is, because when i 'null' it, the 'value' in the hashtable is
effectively null, but the original MyObject still exists.

What is in the hashtable is a reference to an *object*, not a reference
to a *variable*. Changing the value of the variable won't change the
hashtable, but any changes you make to the object itself are visible
through the reference stored in the hashtable.
 
C

cybertof

Which 'variable' are you talking about ?


What is in the hashtable is a reference to an *object*, not a reference
to a *variable*. Changing the value of the variable won't change the
hashtable, but any changes you make to the object itself are visible
through the reference stored in the hashtable.
 
J

Jon Skeet [C# MVP]

cybertof said:
Which 'variable' are you talking about ?

The variable called MyObject, as used in the line of code:

MyHashTable.Add("key1", MyObject);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top