-5

First of all sorry if this is a duplicate. I dont exactly know how to search for this.

I have a question about how to be able to use a saved string to change what type of method I call

MenuBar.Dock = Dockstyle.DockStyleString //DockStyleString is a string defined somewhere with either Top or Bottom
Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • 3
    Write code that does that. Use chained [if/else if/else](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else) statements, or a [switch statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch). You'll have to write this code yourself. Try something and if it doesn't work, we'll help you get it working. But we won't write the code for you. – 15ee8f99-57ff-4f92-890c-b56153 Sep 01 '17 at 14:07
  • 3
    Your question is insufficient. Please provide more information, detailing what you expect to be able to do, and an example of code that you have tried which does not work. – Sam Sep 01 '17 at 14:07
  • `if (menuBar.Dock == "Bottom") Dothis(); else DoSomeThingelse(); ` There´s not much we can do as you´re not providing enough information. So any answer you´ll get is vague. – MakePeaceGreatAgain Sep 01 '17 at 14:09
  • I don't understand your question. Can you give us more information about the problem? It's not easy to understand what you want to do. – TeoVr81 Sep 01 '17 at 14:12
  • 1
    Possible duplicate of [How should I convert a string to an enum in C#?](https://stackoverflow.com/questions/16100/how-should-i-convert-a-string-to-an-enum-in-c) – Sinatr Sep 01 '17 at 14:12
  • 1
    Stop hating on this question. Is a valid question from someone who is starting programming. – Pepito Fernandez Sep 01 '17 at 14:13

2 Answers2

2

So, according to your example you seem to be using an enumerator. Enum has utilities that would 'convert' a string into the right enum value. Also you can have an utility class that does that for you.

DockstyleUtils.FromString("DockStyleString");

This would return an enum Dockstyle.DockstyleString.

So, you can use it MenuBar.Dock = DockstyleUtils.FromString("DockStyleString");

I created this method you can use...

public DockStyle ConvertDockingStyleFromString(string dockingStyle)
        {
            return (DockStyle)Enum.Parse(typeof(DockStyle), dockingStyle);
        }

There you go.

Pepito Fernandez
  • 2,352
  • 6
  • 32
  • 47
0

Some of this depends on what you want to do with the string once you have it. You can use the code in @PepitoFernandez's answer to convert it to an enum. If you'd like to then use it to determine what method to call against an object, you have a few options.

The first is that if it's a known set of strings, you could use a switch statement:

switch (stringVariable) {
    case "stringA": methodA(); break;
    case "stringB": methodB(); break;
    ...
    // If you get a "bad" string, you WANT to throw an exception to make
    // debugging easier
    default: throw new ArgumentException("Method name not recognized");
}

Obviously, you can also replace this with enum values if you do the conversion first. (That's actually not a bad idea because if you get a "bad" string you

The other option (if you want to do it dynamically at runtime) is to do the call using reflection, like this:

public class SomeClass
    {
        public void MethodA()
        {
            Console.WriteLine("MethodA");
        }
    }

    static void Main(string[] args)
    {
        Type type = typeof(SomeClass);
        // Obviously, you'll want to replace the hardcode with your string
        MethodInfo method = type.GetMethod("MethodA");

        SomeClass cls = new SomeClass();

        // The second argument is the parameters; I pass null here because
        // there aren't any in this case
        method.Invoke(cls, null);
    }