Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

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;
                        break;
                    default:
                        break;
                }

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'

Answers

  • 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:

    switch(TxtBox1.HorizontaOptions)
    
  • 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.