I am missing something here with this topic, can someone please explain it?
I have a base class
class BaseClass
{
public void Test()
{
MessageBox.Show("Test");
}
}
Two sub classes
class Class1 : BaseClass
{
public void TestOnlyInSubClass1()
{
MessageBox.Show("TestOnlyInSubClass1");
}
}
class Class2 : BaseClass
{
public void TestOnlyInSubClass2()
{
MessageBox.Show("TestOnlyInSubClass2");
}
}
I instantiate an object depending on user input and try to make the instantiating change depending on user input but it fails as MyObject can only use members in the base class and not the sub class.
BaseClass MyObject = new BaseClass();
string UserInput = "1";
if (UserInput == "1")
{
MyObject = new Class1();
}
else
{
MyObject = new Class2();
}
MyObject.TestOnlyInSubClass1() // this fails
Next thing I tried was not declaring it at the top but this didn’t work as the object was then out of scope.
//BaseClass MyObject = new BaseClass();
string UserInput = "1";
if (UserInput == "1")
{
Class1 MyObject = new Class1();
}
else
{
Class1 MyObject = new Class1();
}
MyObject.TestOnlyInSubClass1()
Can someone explain what I should be doing here I’m obviously getting the whole concept wrong.
I have a base class
class BaseClass
{
public void Test()
{
MessageBox.Show("Test");
}
}
Two sub classes
class Class1 : BaseClass
{
public void TestOnlyInSubClass1()
{
MessageBox.Show("TestOnlyInSubClass1");
}
}
class Class2 : BaseClass
{
public void TestOnlyInSubClass2()
{
MessageBox.Show("TestOnlyInSubClass2");
}
}
I instantiate an object depending on user input and try to make the instantiating change depending on user input but it fails as MyObject can only use members in the base class and not the sub class.
BaseClass MyObject = new BaseClass();
string UserInput = "1";
if (UserInput == "1")
{
MyObject = new Class1();
}
else
{
MyObject = new Class2();
}
MyObject.TestOnlyInSubClass1() // this fails
Next thing I tried was not declaring it at the top but this didn’t work as the object was then out of scope.
//BaseClass MyObject = new BaseClass();
string UserInput = "1";
if (UserInput == "1")
{
Class1 MyObject = new Class1();
}
else
{
Class1 MyObject = new Class1();
}
MyObject.TestOnlyInSubClass1()
Can someone explain what I should be doing here I’m obviously getting the whole concept wrong.