Syed Nazir Hussain
Syed Nazir Hussain

Reputation: 441

How i loop through my class object add in hashtable object

// this is my class object
Friend f1 = new Friend("Nazir",24);
Friend f2 = new Friend("Hamza", 24);
Friend f3 = new Friend("Abdullah", 23);

Hashtable myhash = new Hashtable();

// add this object in hashtable object
myhash.Add("13b-049-bs",f1);
myhash.Add("13b-034-bs", f1);
myhash.Add("13b-038-bs", f1);

foreach (Friend item in myhash)
{
    Console.WriteLine("Key {0}\tName {1}\tAge {2}",item.Name,item.Age,myhash.Keys);
}

I got this error:

An unhandled exception of type 'System.InvalidCastException' occurred in HashTableDemo.exe

Upvotes: 0

Views: 230

Answers (3)

Patrick Artner
Patrick Artner

Reputation: 51643

Use strongly typed Dictionary (using System.Collections.Generic needed):

using System.Collections.Generic;

public class Program
{
    class Friend
    {
        public string Name {get;set;}    
        public int Age {get;set;}

        public Friend(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }

    public static void Main()
    {
        // this is my class object
        Friend f1 = new Friend("Nazir", 24);
        Friend f2 = new Friend("Hamza", 24);
        Friend f3 = new Friend("Abdullah", 23);
        var myDict = new Dictionary<string, Friend>();
        // add this object in hashtable object
        myDict["13b-049-bs"] = f1;
        myDict["13b-034-bs"] = f1;
        myDict["13b-038-bs"] = f1;
        foreach (KeyValuePair<string, Friend> item in myDict)
        {
            Console.WriteLine("Key {0}\tName {1}\tAge {2}", item.Key, item.Value.Name, item.Value.Age);
        }
    }
}

Strongly typed has the advantage that you never will put an Enemy (or some other Type inside this dict - unless it derives from Friend.

Upvotes: 0

Arash Motamedi
Arash Motamedi

Reputation: 10682

The iterator (enumerator) of Hashtable returns objects of type DictionaryEntry. Therefore, in your foreach loop, each item will have two properties: Key and Value, both of type object. You can individually cast the Key and Value properties on each item to your desired type. For example:

foreach (DictionaryEntry item in myhash)
{
    // Cast item.Key to string
    string key = (string)item.Key;

    // And cast item.Value to Friend
    Friend friend = (Friend)item.Value;

    Console.WriteLine("Key {0}\tName {1}\tAge {2}", key, friend.Name, friend.Age);
}

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

try out , as data store in hash tbale is not type of Friend object youare getting error, if you dont know type than make use of var that will resolve error

  foreach (var  item in myhash)
        {
           string key = de.Key.ToString();
           Friend val  = (Friend) item.Value;
           Console.WriteLine("Key {0}\tName {1}\tAge {2}", key, val.Name, val.Age );
        }

or you can do like this

foreach(DictionaryEntry de in myHashtable)
{
      string key = de.Key.ToString();
   Friend val  = (Friend) de.Value;
   Console.WriteLine("Key {0}\tName {1}\tAge {2}", key, val.Name, val.Age );
}

Read out : Hashtable Class

Upvotes: 1

Related Questions