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
@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:
Also you can use it in switch too:
The type
LayoutOptions
is a struct, not an enum.LayoutOptions.Start
, etc., are static properties (likeDateTime.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.