Link to home
Start Free TrialLog in
Avatar of karakav
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?
public InterfaceB methodA (InterfaceB param)
{
//do some work
return param;
}

InterfaceA object1 = (Interface)methodA(object1);

Open in new window

Avatar of with
with

Hi karakav,

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.

public T methodA<T>(InterfaceB param) where T : InterfaceB
{
	//do some work
	return (T)param;
}

InterfaceA object1 = methodA<InterfaceA>(object1);

Open in new window

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);

Open in new window

Avatar of karakav

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
Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tried it in cf.net 2.0 too
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karakav

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.
    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;
        }
    }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karakav

ASKER

Thanks guys. And sorry for the delay.