I am using a library of code which has some classes defined like such:
Code:
public abstract class A
{
...stuff...
}
public class B : A
{
...stuff...
}
So we have a class A and a class B which inherits from A. Class A is also abstract.
Now, we then have some function somewhere in the code which declares an instance of class B, does some operations on that object, and then returns it to the caller. It, however, returns it as if it were an instance of class A. It is something like this:
Code:
public A ReturnInstanceOfA()
{
B myobj = new B();
return B;
}
Now, as a client to this library, I would like to call this function which instantiates an object of class B and returns it to me, and I want to see it as being a B and not an A. So I do this:
Code:
B myobj = ReturnInstanceOfA() as B;
Problem. I get this error: Cannot convert type 'A' to 'B' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
How can I solve this issue?