locked
If function with Area of Comprison

    Question

  • what is code replace this code ( if area is between 1 to 5)

     if (image52.Tag.ToString() == "1" || image52.Tag.ToString() == "2" || image52.Tag.ToString() == "3" || image52.Tag.ToString() == "4" || image52.Tag.ToString() == "5")

    TANX


    Amir Ameri

    Thursday, February 12, 2015 8:57 PM

Answers

  • Hi Amir,

    there's no "between"-operator in C#. But anyway, you could write your if-statement more readable like this:

    if (new[] { "1", "2", "3", "4", "5" }.Contains(image52.Tag.ToString()))
    {
                    
    }

    And if you don't like that approach, you could create an extension-method:

        public static class StringExtensions
        {
            public static bool IsIn(this string str, params string[] values)
            {
                return values != null && values.Contains(str);
            }
        }

    With this extension-method (your code needs a using-directive for the namespace containing this StringExtensions-class) you can write your statement like below:

    if (image52.Tag.ToString().IsIn("1", "2", "3", "4", "5"))
    {
    
    }


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-courses:
    XAML Layout in Depth
    Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com


    Thursday, February 12, 2015 9:58 PM