karakav
asked on
Casting problem in .Net Compact Framework
Hi there,
I have a problem with a cast two objects in .Net Compact framework but the same problem doesn't occur in .net. I have an Interface InterfaceA that inherits an interface InterfaceB. Later I have a method that get Interface InterfaceB and returns Interface InterfaceB.
When I pass in an object of an implementation of the InterfaceA interface to InterfaceB, I guet the values passed in without problem. However when I cannot get the values returned from the methods because I get a casting error.
Can some one help with this?
I have a problem with a cast two objects in .Net Compact framework but the same problem doesn't occur in .net. I have an Interface InterfaceA that inherits an interface InterfaceB. Later I have a method that get Interface InterfaceB and returns Interface InterfaceB.
When I pass in an object of an implementation of the InterfaceA interface to InterfaceB, I guet the values passed in without problem. However when I cannot get the values returned from the methods because I get a casting error.
Can some one help with this?
public InterfaceB methodA (InterfaceB param)
{
//do some work
return param;
}
InterfaceA object1 = (Interface)methodA(object1);
Whoops, correction: you would also want "param" to be of type T in the method signature...
public T methodA<T>(T param) where T : InterfaceB
{
//do some work
return param;
}
InterfaceA object1 = methodA<InterfaceA>(object1);
ASKER
Yeah, I understand I can use generic, but in my case it is some thing I have already used in .net and I wanted to know if there is a limitation in .net compact framework.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
tried it in cf.net 2.0 too
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi guys,
Sorry for the late answer.
I changely changed the solution of alexey_gusev to reflect the problem I am having.
Please not that in the foo method, when you return an implementation of B (BB in this case), you get a runtime error.
Sorry for the late answer.
I changely changed the solution of alexey_gusev to reflect the problem I am having.
Please not that in the foo method, when you return an implementation of B (BB in this case), you get a runtime error.
class Program
{
static void Main(string[] args)
{
A a = new AA();
a = (A)foo(a);
test(a);
}
static B foo(B param)
{
return new BB();
//return param;
}
static void test(A a)
{
a = (A)foo(a);
}
}
interface B
{
int if1();
}
interface A : B
{
bool if2();
}
class AA : BB, A
{
public bool if2() { return false; }
}
class BB : B
{
public int if1()
{
return 10;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys. And sorry for the delay.
I think that because it is possible to create an object that is only InterfaceB (in other words, it is possible to create an object that implements only InterfaceB and not InterfaceA), this means that methodA must treat the return value as a "pure" InterfaceB. It's not always guaranteed that what gets returned by methodA could necessarily be cast into an InterfaceA, because the object might not implement that interface anymore than it might implement InterfaceQ or InterfaceZ.
If your version of the compact framework supports it, this may be a good scenario for using generics. This way you can pass in "anything, so long as it implements InterfaceB" and the return type will be whatever that "anything" is.
Open in new window