A
Andreas Schmitt
I have a problem here. I've read a book about C# already and got the
basics of how the language handles polymorphism I think but I ran into a
problem here that I simply never even thought of as a problem coming
from C++:
I wrote some code similar to this:
abtract public class Base
{
}
public class DerivedA : Base
{
}
public class DerivedB : Base
{
}
public class MyClass
{
public void HandleSomething(DerivedA whatever)
{}
public void HandleSomething(DerivedB whatever)
{}
}
public class MyClassB
{
public void HandleSomething(Base whatever)
{
myThing.HandleSomething(whatever);
}
private MyClass myThing;
}
Doing this I get the error that in MyClassB's method 'HandleSomething'
the parameter whatever cannot be converted to DerivedA or DerivedB
What's the logical reason for this error? The whole point of using a
common base class here was to enable me to handle all cases in one
method. Why does the compiler check this in this way? there will never
be a parameter that is just a "Base" because Base is abstract...
What's the correct way of doing this in C#?
basics of how the language handles polymorphism I think but I ran into a
problem here that I simply never even thought of as a problem coming
from C++:
I wrote some code similar to this:
abtract public class Base
{
}
public class DerivedA : Base
{
}
public class DerivedB : Base
{
}
public class MyClass
{
public void HandleSomething(DerivedA whatever)
{}
public void HandleSomething(DerivedB whatever)
{}
}
public class MyClassB
{
public void HandleSomething(Base whatever)
{
myThing.HandleSomething(whatever);
}
private MyClass myThing;
}
Doing this I get the error that in MyClassB's method 'HandleSomething'
the parameter whatever cannot be converted to DerivedA or DerivedB
What's the logical reason for this error? The whole point of using a
common base class here was to enable me to handle all cases in one
method. Why does the compiler check this in this way? there will never
be a parameter that is just a "Base" because Base is abstract...
What's the correct way of doing this in C#?