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[i].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[i] = obj_Temp_Hashtable;
}
}
}
}
Thoughts or documentation is appreciated.
Henry
Robert Jordan - 15 Oct 2004 02:15 GMT
> 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
[quoted text clipped - 16 lines]
> {
> for (int i = 0; i < 2; i++) {
obj_Hashtable[i] = new Hashtable();
> for (int j = 0; j < 10; j++) {
> obj_Hashtable[i].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
Jay B. Harlow [MVP - Outlook] - 15 Oct 2004 02:39 GMT
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[i] = new Hashtable();
> for (int j = 0; j < 10; j++) {
> obj_Hashtable[i].Add(j.ToString(), j);
> }
> }
> }
Hope this helps
Jay
> Can someone explain to me why The load method compiles but throws and
> error
[quoted text clipped - 45 lines]
>
> Henry
Imran Koradia - 15 Oct 2004 02:40 GMT
> Can someone explain to me why The load method compiles but throws and
> error
[quoted text clipped - 12 lines]
> {
> 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[i] = new Hastable();
> for (int j = 0; j < 10; j++) {
> obj_Hashtable[i].Add(j.ToString(), j);
> }
> }
> }
hope that helps..
Imran.
Cowboy (Gregory A. Beamer) - MVP - 15 Oct 2004 19:07 GMT
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!
***************************
> 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
[quoted text clipped - 45 lines]
>
> Henry