Hashtable Curiosity

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone explain to me why The load method compiles but throws and error
when is called and why do I have write the code with a temp container like
the one in load2 method in order for it to work?

#region Using directives
using System;
using System.Collections;
using System.Text;
#endregion

namespace Testing_Cache
{
public class cls_test
{
private static Hashtable[] obj_Hashtable = new Hashtable[2];


public static void Load()
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 10; j++) {
obj_Hashtable.Add(j.ToString(), j);
}
}
}

public static void Load2()
{
Hashtable obj_Temp_Hashtable;

for (int i = 0; i < 2; i++)
{
obj_Temp_Hashtable = new Hashtable();

for (int j = 0; j < 10; j++)
{
obj_Temp_Hashtable.Add(j.ToString(), j);
}

obj_Hashtable = obj_Temp_Hashtable;
}
}

}
}



Thoughts or documentation is appreciated.

Henry
 
Henry said:
Can someone explain to me why The load method compiles but throws and error
when is called and why do I have write the code with a temp container like
the one in load2 method in order for it to work?

#region Using directives
using System;
using System.Collections;
using System.Text;
#endregion

namespace Testing_Cache
{
public class cls_test
{
private static Hashtable[] obj_Hashtable = new Hashtable[2];


public static void Load()
{
for (int i = 0; i < 2; i++) {
obj_Hashtable = new Hashtable();
for (int j = 0; j < 10; j++) {
obj_Hashtable.Add(j.ToString(), j);
}
}
}


The statement Hashtable[] obj_Hashtable = new Hashtable[2];
generates an array of length 2 which consists of 2 null
values. You always have to initialize a "new"ed array.

bye
Rob
 
Henry,
In addition to Robert's comment.

I would simply initialize each row when I reach it, something like:
public static void Load()
{
for (int i = 0; i < 2; i++) {

obj_Hashtable = new Hashtable();
for (int j = 0; j < 10; j++) {
obj_Hashtable.Add(j.ToString(), j);
}
}
}


Hope this helps
Jay


Henry said:
Can someone explain to me why The load method compiles but throws and
error
when is called and why do I have write the code with a temp container like
the one in load2 method in order for it to work?

#region Using directives
using System;
using System.Collections;
using System.Text;
#endregion

namespace Testing_Cache
{
public class cls_test
{
private static Hashtable[] obj_Hashtable = new Hashtable[2];


public static void Load()
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 10; j++) {
obj_Hashtable.Add(j.ToString(), j);
}
}
}

public static void Load2()
{
Hashtable obj_Temp_Hashtable;

for (int i = 0; i < 2; i++)
{
obj_Temp_Hashtable = new Hashtable();

for (int j = 0; j < 10; j++)
{
obj_Temp_Hashtable.Add(j.ToString(), j);
}

obj_Hashtable = obj_Temp_Hashtable;
}
}

}
}



Thoughts or documentation is appreciated.

Henry
 
Henry said:
Can someone explain to me why The load method compiles but throws and
error
when is called and why do I have write the code with a temp container like
the one in load2 method in order for it to work?

#region Using directives
using System;
using System.Collections;
using System.Text;
#endregion

namespace Testing_Cache
{
public class cls_test
{
private static Hashtable[] obj_Hashtable = new Hashtable[2];

As Robert mentioned, the statement above is only initializing your array -
not the objects within the array. (Ofcourse, value types are initialized to
their default values). So your hashtable objects are still null. Instead of
your code in Load2(), just add the line I added in your Load() method and
you should be good to go.
public static void Load()
{
for (int i = 0; i < 2; i++) {

// this should do it..
obj_Hashtable = new Hastable();
for (int j = 0; j < 10; j++) {
obj_Hashtable.Add(j.ToString(), j);
}
}
}



hope that helps..
Imran.
 
You are creating a static Hashtable and using instance methods to Add to it.
This is why you must create an instance first.
---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Back
Top