Solved

Casting problem in .Net Compact Framework

Posted on 2010-08-16
12
599 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:karakav
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 3

Expert Comment

by:with
ID: 33448311
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

0
 
LVL 3

Expert Comment

by:with
ID: 33448495
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

0
 
LVL 4

Author Comment

by:karakav
ID: 33451986
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.
0
 
LVL 24

Assisted Solution

by:alexey_gusev
alexey_gusev earned 250 total points
ID: 33548238
what's the error exactly, and what's cf.net version?

the following snippet compiles perfectly in cf.net 3.5, please correct it if it is not what you are doing.
using System;
using System.Collections.Generic;
using System.Text;

namespace interfaceCast
{
    class Program
    {
        static void Main(string[] args)
        {
            AA aa = new AA();
            A a = (A)foo(aa);
            test(aa);
        }

        static B foo(B param)
        {
            return param;
        }

        static void test(A a)
        {
            a = (A)foo(a);
        }
    }

    interface B
    {
        int if1();
    }

    interface A : B
    {
        bool if2();
    }

    class AA : A
    {
        public AA() { }

        public int if1() { return 0;  }
        public bool if2() { return false; }
    }
}

Open in new window

0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33548244
tried it in cf.net 2.0 too
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Assisted Solution

by:fromer
fromer earned 125 total points
ID: 33548879
public InterfaceA : InterfaceB
{
}

public static class SomeClass
{
  public static InterfaceB MethodA(InterfaceB parameter)
  {
    return parameter;
  }  
}

public ObjectA : InterfaceA
{
}

public ObjectB : InterfaceB
{
}

InterfaceA returnValue =  (InterfaceA)SomeClass.MethodA(new ObjectA());

you get no error here, because ObjectA implements InterfaceA...

InterfaceA returnValue = (InterfaceA)SomeClass.MethodA(new ObjectB());
you get error here, because ObjectB doesn't implement InterfaceA...

is that the case of you?

0
 
LVL 4

Author Comment

by:karakav
ID: 33566766
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

0
 
LVL 24

Assisted Solution

by:alexey_gusev
alexey_gusev earned 250 total points
ID: 33566894
huh? unless I'm missing something,

BB implements B
A is NOT a base class for B, but its derived interface

so it all ends up in the following:

- you created the instance of the class BB in foo()
- you're trying to cast it to A, which is nowhere in the inheritance tree for BB, ie there are no conversion operators defined
0
 
LVL 3

Accepted Solution

by:
with earned 125 total points
ID: 33567595
Try this in Compact .NET.  It's the same sample you posted, with more obvious names to help follow it through.

You should be able to get this to run by choosing the "return param" route of the MakeShorter (aka "foo") method.

In your original question, you were returning the param, not making a new class, right?  Which do you need to do?
class Program
{
	interface IHaveHeight
	{
		int height { get; set; }
	}

	interface IThink : IHaveHeight
	{
		bool isthinkingaboutpizza { get; set; }
	}

	class Mammal : IHaveHeight
	{
		public int height { get; set; }
		public Mammal()
		{
			height = 10;
		}
	}

	class Human : Mammal, IThink
	{
		public bool isthinkingaboutpizza { get; set; }
		public Human()
		{
			isthinkingaboutpizza = true;
		}
	}

	static void Main(string[] args)
	{
		IThink joe = new Human();
		joe = (IThink)MakeShorter(joe);
		ThinkingAboutPizzaMakesYouShorter(joe);
	}

	static IHaveHeight MakeShorter(IHaveHeight param)
	{
		param.height--;

		//If you do this, what gets returned is a new simple Mammal that cannot think. 
		//So it cannot be cast to Human or IThink
		return new Mammal(); 

		//If you do this, joe remains intact and can be cast to anything else
		return param; 
	}

	static void ThinkingAboutPizzaMakesYouShorter(IThink thinker)
	{
		if (thinker.isthinkingaboutpizza)
		{
			//Assignment is pointless here
			thinker = (IThink)MakeShorter(thinker); 

			//This would do this same thing:
			MakeShorter(thinker);
		}
	}
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:karakav
ID: 34281897
Thanks guys. And sorry for the delay.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now