1

I read a data from excel and determine which event I am going to execute.

event are both class created by myself (login and logout)

if the value I read = 1 , execute login

if the value I read = 2 , execute logout

I use switch but my boss say I have to use something like hashmap in Java.

In Java, I can write the code like:

table.Add( "one", login.class );

so how can I add class into hashtable using c#?

and how can I read the value and call the class method in hashtable?

Edison
  • 79
  • 1
  • 13

3 Answers3

5

You can use delegates. For example, if you had these methods:

public void Login() {
    // ...
}

public void Logout() {
    // ...
}

You could use this Dictionary:

Dictionary<string, Action> actions = new Dictionary<string, Action>() {
    {"Login", Login},
    {"Logout", Logout}
};

Then call it like:

actions[myAction]();

Of course, you'll want to make sure the key exists. You can call delegates in pretty much the same way you call a regular method. If they have arguments or return values, just use the appropriate Action<T1, T2...> or Func<T1, T2... TOut>.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @Edison: Not really, you have to choose which method you want to call in the class. But the premise is the same: `{"Login", new Login().DoWhatever}` for example. Either that or store an instance of the class in the dictionary - this only works if they inherit or implement a common class or interface, respectively. – Ry- Apr 18 '12 at 01:29
  • @Edison: Will the class have static methods? If not, you will need to store instances. You should inherit all classes of such instances from a common `Interface`, such as `ICallable` or something similar, if you intend to call the same method. Then you can use a `Dictionary` – Robert Harvey Apr 18 '12 at 01:29
  • @RobertHarvey Can you give me some code as an example? I don't know how to use Interface in here. – Edison Apr 18 '12 at 02:48
  • @Edison: See my posted answer. – Robert Harvey Apr 18 '12 at 03:05
1

Actually, C# compiler will implement switch on strings as a hashtable, so it's unlikely you can gain anything by doing it manually.

See this post.

You could even tell your boss you already did it, and you wouldn't be lying ;)

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
0

The following code allows you to implement a DoSomething method in your objects, callable from a Dictionary index:

public interface ICallable
{
    void Execute();
}

public class Login : ICallable
{
    // Implement ICallable.Execute method
    public void Execute()
    {
        // Do something related to Login.
    }
}

public class Logout : ICallable
{
    // Implement ICallable.Execute method
    public void Execute()
    {
        // Do something related to Logout
    }
}

public class AD
{
    Dictionary<string, ICallable> Actions = new Dictionary<int, ICallable>
    {
        { "Login", new Login() }
        { "Logout", new Logout() }
    }

    public void Do(string command)
    {
        Actions[command].Execute();
    }
}

Example Usage

AD.Do("Login"); // Calls `Execute()` method in `Login` instance.
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • oh!! thank u!! BUT I write a class ActionDictionary to do the thing you write, how can I return the class ActionDictionary 's method? I just want to return Actions[1].Execute(); – Edison Apr 18 '12 at 03:31
  • What do you want to return from the `Execute()` method? Right now it's returning `void`. See the `interface` definition? – Robert Harvey Apr 18 '12 at 03:32
  • yes I know , so i am trying to modify. My main class read the excel and send the value(like:"Login") to let ActionDictionary class to get the Action class I want.(Like AD.do("Login");) – Edison Apr 18 '12 at 03:37