I am making a text based adventure game to learn C# and when the user types something, I want to pass this to a InterpretCommand method that finds a method based on their input. So if they type “say Hello”, then the InterpretCommand method passes that input to another method called CommandSay and passes “hello” in as the argument.


I found this information but I’m not sure if that would work since I’d need all the commands to be under one class and I don’t know if I can structure all the code like that in the future. I might even be looking at this the wrong way so any advice is welcome.
Start with a switch statement.
Switch (input) { Case “Hello”: SaySomething(); Break; Case ….
Break your input into command then the rest is args for that command. “Hello Pejo” would grab hello as command and Pejo can be the args for say something.
I know but I’m planning to have more than a few commands so I don’t want to have to edit a switch statement repeatedly for every command.
I don’t understand how that answers the question. So if the user passes in the input “say” and I assign it to x, you’re saying I can just call the method just using x? How does it just convert a string to a method? User inputs “say”, which is processed by InterpretCommand
C# devs
null reference exceptions

source