locked
Help with || RRS feed

  • Question

  • I'm translating field rules for an app thats being migrated from FoxPro to C# (.NET).

    Example:  

    Here is an expression in the old app that is for a required field (the field is required when this expression evaluates to true):

    IntegerFieldName1 = 1 OR IntegerFieldName1 = 2 

    First C# thought was:  fieldName1.Value = 1 || fieldName.Value = 2

    Is there a better approach ? Simplified ?  For instance something like:  filedName1.Value == 1 || 2 ? 
    I'm also thinking that the || operator can't be used with int or bool data types ?

    Thanks

    Tuesday, November 18, 2008 4:04 PM

Answers

  • Your first C# thought is probably your best bet. 

    Beware, however, your first C# thought is assigning values, not checking them for equality.  It should be:

    fieldName1.Value == 1 || fieldName1.Value == 2

    I'm assuming this was just a typo. 

    Unfortunately, I know of no good shortcuts to this.


    David Morton - http://blog.davemorton.net/
    Tuesday, November 18, 2008 4:11 PM
    Moderator
  • No, you have to do it longhand - unless the comparison values are constants, in which case you could use a switch statement.
    Another possibility is if you have the comparison values in an array; then you could loop through all the values in the array. It doesn't help you much though, because you'd still have to put those values in the array in the first place.
    Tuesday, November 18, 2008 4:59 PM
  • C# 3 gets close:-

        int x = 5;  
        var myValues = new int[] {1, 2, 3, 4, 5, 6 };  
        bool b = !Array.TrueForAll<int>(myValues, y => y != x);  
     

    Or as an extension method:-

        int x = 5;  
        bool b = x.OneOf(1, 2, 3);  
     
     
    ...  
     
        public static bool OneOf<T>(this T val, params T[] values)  
        {  
            return !Array.TrueForAll<T>(values, y => !y.Equals(val));  
        }  
     

    Anthony Jones - MVP ASP/ASP.NET
    Tuesday, November 18, 2008 5:50 PM

All replies

  • Your first C# thought is probably your best bet. 

    Beware, however, your first C# thought is assigning values, not checking them for equality.  It should be:

    fieldName1.Value == 1 || fieldName1.Value == 2

    I'm assuming this was just a typo. 

    Unfortunately, I know of no good shortcuts to this.


    David Morton - http://blog.davemorton.net/
    Tuesday, November 18, 2008 4:11 PM
    Moderator
  • Hi David,

    Yep, just a typo.  Sorry about that, but thanks for pointing it out.

    What about checking for 10 values in a field ?  Do I still have to go with:

     fieldName == value1 || fieldName == value2 || fieldName || value 3 || fieldName == value 4

    ?

    Is there some type of C# code that I can simplify with ?  For instance " If the value of this field is any of these ten value, then "  ?

    thnx

    Tuesday, November 18, 2008 4:50 PM
  • No, you have to do it longhand - unless the comparison values are constants, in which case you could use a switch statement.
    Another possibility is if you have the comparison values in an array; then you could loop through all the values in the array. It doesn't help you much though, because you'd still have to put those values in the array in the first place.
    Tuesday, November 18, 2008 4:59 PM
  • C# 3 gets close:-

        int x = 5;  
        var myValues = new int[] {1, 2, 3, 4, 5, 6 };  
        bool b = !Array.TrueForAll<int>(myValues, y => y != x);  
     

    Or as an extension method:-

        int x = 5;  
        bool b = x.OneOf(1, 2, 3);  
     
     
    ...  
     
        public static bool OneOf<T>(this T val, params T[] values)  
        {  
            return !Array.TrueForAll<T>(values, y => !y.Equals(val));  
        }  
     

    Anthony Jones - MVP ASP/ASP.NET
    Tuesday, November 18, 2008 5:50 PM
  • Thanks everyone.
    Tuesday, November 18, 2008 7:24 PM