I want to call a method from my base class that also has a variable from the base class:
class BaseClass
{
public string BaseClassMethod()
{
if (CheckKeyboard(Keys.Up))
return "Up";
if (CheckKeyboard(Keys.Down))
return "Down";
if (CheckKeyboard(Keys.Enter) && keyboardOn == true) <-- keyboardOn is a variable from my BaseClass that i want to be able to use :/
{
counter = 0; <-- counter is also one of those variables
return "Enter";
}
return "";
}
}
class InheritFromBase : BaseClass
{
public string Update()
{
currentKeyboard = Keyboard.GetState();
currentMouse = Mouse.GetState();
if (BaseClassMethod() == "Up")
if (selected > 0)
selected--;
else
selected = buttonList.Count - 1;
if (BaseClassMethod() == "Down")
if (selected < buttonList.Count - 1)
selected++;
else
selected = 0;
if (BaseClassMethod() == "Enter")
return buttonList[selected];
previousKeyboard = currentKeyboard;
previousMouse = currentMouse;
return "";
}
}
and since i call the mothod from another class it seems like a cannot access he variables (values) and then change them.
please help 🙂 thank you
It’s generally bad practice to expose a local variable outside a class. You can do it through a protected access modifier, however I’d recommend you expose it through a
protectedProperty or Method.assuming
keyboardOnis your class-level variable in your base class:The above assumes you only want to
getthe status variablekeyboardOnfrom the base class. If you need to also set the value of the variable from the inheriting class you can add asetto the exposing property.