Hashtable : how to add a reference ?

  • Thread starter Thread starter cybertof
  • Start date Start date
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.
 
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
 
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.
 
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.
 
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.
 
cybertof said:
Which 'variable' are you talking about ?

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

MyHashTable.Add("key1", MyObject);
 
Back
Top