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

Re: Hashtable Curiosity by Robert

Robert
Thu Oct 14 20:15:37 CDT 2004

Henry wrote:

> 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[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

Re: Hashtable Curiosity by Jay

Jay
Thu Oct 14 20:39:22 CDT 2004

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


"Henry" <Henry@discussions.microsoft.com> wrote in message
news:69F8D5A2-EFCE-40B5-986F-9A934A428155@microsoft.com...
> 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
>
>



Re: Hashtable Curiosity by Imran

Imran
Thu Oct 14 20:40:55 CDT 2004


"Henry" <Henry@discussions.microsoft.com> wrote in message
news:69F8D5A2-EFCE-40B5-986F-9A934A428155@microsoft.com...
> 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[i] = new Hastable();

> for (int j = 0; j < 10; j++) {
> obj_Hashtable[i].Add(j.ToString(), j);
> }
> }
> }
>


hope that helps..
Imran.



RE: Hashtable Curiosity by NoSpamMgbworld

NoSpamMgbworld
Fri Oct 15 13:07:14 CDT 2004

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

"Henry" wrote:

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