locked
if loop otmization RRS feed

  • Question

  • User-87306394 posted

    please have a look at following simple code

      if (usercmb.SelectedItem.Value != "" )
                    {
                        if (usercmb.SelectedItem.Value != "0")
                        {
                            cnt2.UserName = Convert.ToString(usercmb.SelectedItem.Value.ToString());
                         }
                    }

    i want to merge both the first lines logic i.e can i do like this

    if(usercmb.SelectedItem.Value!==""  ||  usercmb.SelectedItem.Value!==0)

    please help

    Thursday, August 14, 2014 9:55 AM

Answers

  • User281315223 posted

    You could absolutely do that :

    if (usercmb.SelectedItem.Value != "" && usercmb.SelectedItem.Value != "0")
    {
        cnt2.UserName = usercmb.SelectedItem.Value;
    }
    

    An even safer approach might be to use the SelectedValue property, which is the same as the SelectedItem.Value property along with the String.IsNullOrEmpty() method to check for empty or null values :

    if (!String.IsNullOrEmpty(usercmb.SelectedValue) && usercmb.SelectedItemValue != "0")
    {
         cnt2.UserName = usercmb.SelectedValue;
    }

    And just for fun a one liner :

    cnt2.UserName = (!String.IsNullOrEmpty(usercmb.SelectedValue) && usercmb.SelectedItemValue != "0") ?  usercmb.SelectedValue : cnt2.UserName;
    

    You didn't need to previously include all of the Convert.ToString() methods and other .ToString() method calls as the SelectedValue property is already a string, so you don't need to worry about attempting to convert it again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 14, 2014 10:01 AM

All replies

  • User281315223 posted

    You could absolutely do that :

    if (usercmb.SelectedItem.Value != "" && usercmb.SelectedItem.Value != "0")
    {
        cnt2.UserName = usercmb.SelectedItem.Value;
    }
    

    An even safer approach might be to use the SelectedValue property, which is the same as the SelectedItem.Value property along with the String.IsNullOrEmpty() method to check for empty or null values :

    if (!String.IsNullOrEmpty(usercmb.SelectedValue) && usercmb.SelectedItemValue != "0")
    {
         cnt2.UserName = usercmb.SelectedValue;
    }

    And just for fun a one liner :

    cnt2.UserName = (!String.IsNullOrEmpty(usercmb.SelectedValue) && usercmb.SelectedItemValue != "0") ?  usercmb.SelectedValue : cnt2.UserName;
    

    You didn't need to previously include all of the Convert.ToString() methods and other .ToString() method calls as the SelectedValue property is already a string, so you don't need to worry about attempting to convert it again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 14, 2014 10:01 AM
  • User-389597101 posted

    try this

    cnt2.UserName = usercmb.SelectedItem.Value != "" ? (usercmb.SelectedItem.Value != "0" ? test : "") : "";

     

    Thursday, August 14, 2014 10:03 AM
  • User-1360095595 posted

    I would write it like this:

    string selectedValue = usercmb.SelectedValue ;
    if (selectedValue != "" && selectedValue != "0")
    {
        cnt2.UserName = selectedValue;
    }
    

    Thursday, August 14, 2014 10:10 AM
  • User-1956403626 posted

    if too many sentinels gets added kinda cryptic to read. I would do

    using System.Linq;
    
    var invalidSelections = new[]{null, string.Empty, "0"}; //add other invalid inputs here as per need
            if(!invalidSelections.Contains(a))
            {
                //do stuffs
            }

    Friday, August 15, 2014 4:13 PM