Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

differences in implementing an interface on Windows, MonoForAndroid, and MonoTouch

I am porting some old code that I have on Windows Mobile. The code compiles fine for .NET 3.5 CE. I created a project in MonoForAndroid and referenced the same code (.cs files refereneced as Links) and it builds fine for Android.

However, when I created a project for MonoTouch and referenced the same C# code files, it cannot build throwing an error saying that a method is not implemented.

Here is what I have:

public interface IJobController
{
   IUser User {get; }
}

public class BaseController 
{
   public virtual IUser User {get;set;}
}


public class BaseJobController : BaseController
{
   public new User User {get;set;}
}

public class JobController : BaseJobController, IJobController
{
  // some code here that uses the User property  
}

The error says that JobController does not implement method IUser User { get;} The candidate is the one in BaseJobController but the type does not match IUser against User.

This same code has been working for many years on Windows Mobile .NET 3.5 CE and the same code compiles fine on MonoForAndroid. However, it cannot compile on MonoTouch.

My concern is that there are differences in the way inheritance and Interface-Implementation implemented in MonoTouch, I have lots of work ahead (lots of code to port), so should I expect more surprises like this??

Any help would be much appreciated.

like image 435
Has AlTaiar Avatar asked Mar 28 '26 16:03

Has AlTaiar


1 Answers

Well, it's clearly a bug since your code does compile on these other platforms. I recommend you file a bug with them. Also clearly, the compiler is getting confused by you using the new keyword against a virtual method. This is a shame.

That being said, it's easy to workaround, at least until there's a fix. Just use explicit interface implementations:

public class JobController : BaseJobController, IJobController
{
    IUser IJobController.User
    {
        get { return User; }
    }
}

This should suffice until Xamarin produces a fix.

like image 57
Kirk Woll Avatar answered Mar 31 '26 06:03

Kirk Woll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!