Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2004

Tip: Looking for answers? Try searching our database.

Hashtable Curiosity

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henry - 15 Oct 2004 02:01 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
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.