problem adding object to hashtable

Posted by daemonkid on Stack Overflow See other posts from Stack Overflow or by daemonkid
Published on 2010-05-24T14:54:30Z Indexed on 2010/05/24 15:01 UTC
Read the original article Hit count: 248

Filed under:
|

I am trying to call a class method dynamically depending on a condition. This is how I am doing it

I have three classes implement a single interface

interface IReadFile
{
    string DoStuff();
}

The three classes A,B,C implement the interface above.

I am trying to add them to a hashtable with the code below

        _HashT.Add("a", new classA());
        _HashT.Add("b", new classB());
        _HashT.Add("c", new classC());

This compiles fine, but gives a runtime error.{Object reference not set to an instance of an object.}

I was planning to return the correct class to the interface type depending on a parameter that matches the key value. say if I send in a. ClassA is returned to the interface type and the method is called.

        IReadFile Obj = (IReadFile )_HashT["a"].GetType();
        obj.DoStuff();

How do I correct the part above where the objects need to be added to the hashtable? Or do I need to use a different approach? All the classes are in the same assembly and namespace.

Thanks for your time.

© Stack Overflow or respective owner

Related posts about c#

Related posts about hashtable