Problem understanding refactoring

void OnUserInput(string input)
{
if (input == “menu”) //this donkey works
{
ShowMainMenu(“Welcome Back Starlord”);
}
else if (currentScreen == Screen.MainMenu)
RunMainMenu(input);
}

void RunMainMenu(string input)

So the problem I’m having is that I can’t quite wrap my head around why we have to declare the (string input) in the RunMainMenu function. Because the ìnput parameter has already been declared in the OnUserInput function and that’s where the information is coming from for all input related statements.
And also while we call the RunMainMenu function we just use input in the parentheses.
So my question is, is this because how functions are meant to be written(declaring the parameter type everytime you use a parameter EVEN THOUGH it’s been declared as a parameter before, in another function) or is there something else I do not understand.

Sorry for the messy writing :smiley:

I’ll try to help… Try to think of the functions existing independent from the rest of the code; this lends to their modularity. C# allows us to “overload” our functions: for example if have a “function” called X, we can overload it to handle any number of “prototypes” like so

void X(int n) {} // X function for integers
void X(float n) {} // X function for floats
void X(bool torf) {} // X function for boolean

So the usual rule of thumb is to hand a function only what you need to. If your function RunMainMenu() requires a string input, this is the way to do it. Keeping the variable ‘input’ as a member variable is discouraged unless there is a good reason for it.

I hope this helps some.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms