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?

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

@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.
@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<string, ICallable>
@RobertHarvey Can you give me some code as an example? I don't know how to use Interface in here.
@Edison: See my posted answer.
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 ;)

Comments

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.

3 Comments

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();
What do you want to return from the Execute() method? Right now it's returning void. See the interface definition?
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");)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.