HashTables and scoping

  • Thread starter Thread starter Roger Webb
  • Start date Start date
R

Roger Webb

Hey All,

I cant VERIFY this, but it makes sense so I thought I'd trow up my theory
and see if it made sense to any of you... and then ask how to get around it.
hehe

I've got a Class1[] that I'm wanting to load into a hash table, so, I
basically (at this point) run through a loop and do a
Hashtable.Add(Class1[index].key,Class1[index])

However, later when I try to get out the value out of the HashTable... The
value is null.

I'm thinking this is because the array that initially had the values has
gone out of scope .. and the hashtable only stored a reference to the
object.. so now, the refence is null. If this is true, how do I get
around that? If it is not true, does anyone know why I'm not getting my
values out?

- Roger
 
Roger,

This is not the case. When you assign Class1[index] to the hashtable,
the hashtable holds a reference to the object in the array. If the array
goes out of scope, as long as the hashtable is in scope, then the references
are still valid.

It is something else that is causing the values in the hashtable to be
null.

The only things I can think of are that the value at Class1[index] is
already null, and it's null when it goes in, or there is an explicit setting
of the value for that key to null.

Can you provide a sample project which shows the error?
 
I know that the values going in are non-null, mainly becasue the key value
is a property of the class being added. So I will investigate that it is
beign set null somewhere that I am unaware of.

Thanks for the reply.

- Roger


Nicholas Paldino said:
Roger,

This is not the case. When you assign Class1[index] to the hashtable,
the hashtable holds a reference to the object in the array. If the array
goes out of scope, as long as the hashtable is in scope, then the references
are still valid.

It is something else that is causing the values in the hashtable to be
null.

The only things I can think of are that the value at Class1[index] is
already null, and it's null when it goes in, or there is an explicit setting
of the value for that key to null.

Can you provide a sample project which shows the error?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Roger Webb said:
Hey All,

I cant VERIFY this, but it makes sense so I thought I'd trow up my theory
and see if it made sense to any of you... and then ask how to get around
it.
hehe

I've got a Class1[] that I'm wanting to load into a hash table, so, I
basically (at this point) run through a loop and do a
Hashtable.Add(Class1[index].key,Class1[index])

However, later when I try to get out the value out of the HashTable... The
value is null.

I'm thinking this is because the array that initially had the values has
gone out of scope .. and the hashtable only stored a reference to the
object.. so now, the refence is null. If this is true, how do I get
around that? If it is not true, does anyone know why I'm not getting my
values out?

- Roger
 
Back
Top