Stable Release of XS with Support iOS Compiler Warning Bug

KevinMullinsKevinMullins USMember, Beta ✭✭

Before the stable release this chunk of code used to compile fine:

    /// <summary>
    /// Returns the bounds for the device's <c>MainScreen</c> adjusted to fit the current <c>UIInterfaceOrientation</c>
    /// </summary>
    /// <value>The rotated screen bounds.</value>
    internal static RectangleF RotatedScreenBounds{
        get {
            RectangleF bounds;

            //Adjust view to match current interface orientation
            switch (currentDeviceOrientation) {
            case UIInterfaceOrientation.LandscapeLeft:
            case UIInterfaceOrientation.LandscapeRight:
                bounds = new RectangleF (0, 0, UIScreen.MainScreen.Bounds.Height, UIScreen.MainScreen.Bounds.Width);
            case UIInterfaceOrientation.Portrait:
            case UIInterfaceOrientation.PortraitUpsideDown:
                bounds = new RectangleF (0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);

            //Return results
            return bounds;

Now I get an error at return bounds; that say Use of unassigned local variable 'bounds' even though all paths through the case statement set a value to bounds.

Note that this code worked correctly before.


  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    The compiler doesn't know that all possible cases of currentDeviceOrientation are covered. You need to initialize it to something when you declare it or add a default case. Perhaps some enhanced checking has been enabled in the stable release that wasn't there in whatever you were using before.

  • KevinMullinsKevinMullins USMember, Beta ✭✭

    This stuff has been there for a very long time... more than a year and it is valid C# so there's something wrong with the new version an switch statements.

    Even if I added a default case it was still failing. For the time being I just initialized it to an "empty" rect... but not the point, I shouldn't have to :)

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Nothing is "failing". The compiler is telling you that it believes it is possible for the return statement to be reached without ever assigning anything to the bounds variable. As I said, it doesn't know that every possible value has been accounted for in your switch statement. Your code would work fine, but the compiler can't know that at compile time. There is nothing wrong with the "new version of switch statements".

    It may be annoying, but it's not a bug. It's a valid compiler error.

    Have a look at
    or google for the error message. You will see that this is a common "complaint" in C#.

    Adding a default that throws an exception or initializing the variable at declaration is the correct way to deal with this. Whether it worked that way in Xamarin in the past or not, that is how (most) C# compilers have always worked.

Sign In or Register to comment.