Forum Xamarin.Forms

Operator '==' cannot be applied to operands of type 'LayoutOptions' and 'LayoutOptions'

mshwfmshwf EGMember ✭✭✭
edited December 2017 in Xamarin.Forms

I'm trying to make a loop statement on the HorizontalOptions of a CustomGrid, first I used switch:

switch (HorizontalOptions)
                    case LayoutOptions.Start:
                        HorizontalOptions = LayoutOptions.End;

but it says a constant value is expected (I feel I'm missing a very basic rule of C#!)

then I tried with if statement:

if (HorizontalOptions == LayoutOptions.Start)


The error now is:

Operator '==' cannot be applied to operands of type 'LayoutOptions' and 'LayoutOptions'


  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @mshwf "HorizontalOptions" is property that describes an Object. It is not a variable or an Object by itself. Say if you are trying to check the value to which HorizontalOptions of a TextBox named say "TextBox1" then try as below:

    if (TextBoz1.HorizontalOptions == LayoutOptions.Start) { }

    Also you can use it in switch too:

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited December 2017

    The type LayoutOptions is a struct, not an enum. LayoutOptions.Start, etc., are static properties (like DateTime.Now), not enum values. That is why you cannot put it in a switch statement. Also, the == operator must be overloaded by the struct to work, which Xamarin did not do.

    Instead, you can switch on its Alignment property which is an enum.

Sign In or Register to comment.