1
var summoner = api.GetSummoner(Region.euw, "AlexK");    

I want to change the euw into a variable. Like this

Console.WriteLine("Type your server") //and I type for example one of the following (eune,euw,na)
string server = Console.ReadLine();
var summoner = api.GetSummoner(Region.server, "AlexK");

Another example would be

Console.WriteLine("Type if you want to Read or Write");
string preference = Console.ReadLine();
Console.preferenceLine() //There is no practical use for this one just giving an example of where I want to place the variable.

The C# wrapper I am using is this one https://github.com/BenFradet/RiotSharp.
I am quite new to C# so ELI5 or send some links.
Thanks in advance.

  • No, there is no way to do that and I would honestly not know why it would be useful if it was possbile. Maybe you want to look into what an Array is or an Dictionary as my blind guess is that those concepts might be the Y in your XY problem. – rene Jun 17 '17 at 15:21
  • No [Monkey Patching](https://en.m.wikipedia.org/wiki/Monkey_patch) in C#. – CodingYoshi Jun 17 '17 at 15:24
  • In the first example, the only real way to change the value EUW is to do it in code. You can't get user input. store it in a variable and change it based on his answer. – Alexander Andrew Jun 17 '17 at 15:28

1 Answers1

0

Depending on what comes after the dot operator, this must be handled differently.

If you want to call a method based on an input, you can use reflection. However, unless you're dealing with an unknown type (when method names are only known at runtime), it will be better to just branch your code using an if statement.

if (method == "read")
{
    string input = Console.ReadLine();
    var summoner = api.GetSummoner(server, "AlexK");
    // TODO: do something with the summoner
}
if (method == "write")
{
    Console.WriteLine("Printing data:")
    // TODO: print data
}

This is in contrast to enums, which are a completely different language construct and can be easily parsed from text.

string input = Console.ReadLine();
Region region = Enum.Parse(typeof(Region), input);
var summoner = api.GetSummoner(region, "AlexK");
apk
  • 1,550
  • 1
  • 20
  • 28