Hashtable Curiosity

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
 
R

Robert Jordan

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
I

Imran Koradia

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.
 
G

Guest

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!
***************************
 

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