How can I use Logical OR(||) in a conditional statement where operands are string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aritramahanty
    New Member
    • May 2010
    • 1

    How can I use Logical OR(||) in a conditional statement where operands are string

    the method :
    Code:
        protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
        {
            string str = e.Item.Text;
            if ((str == "scrap") || (str = "Friend"))
            {
                e.Item.Parent.ChildItems.Remove(e.Item);
            }
    
        }
    it throws an error:
    Error 1: Operator '||' cannot be applied to operands of type 'bool' and 'string'

    I want to check the string with that 2 value("scrap"," Friend")

    Thanks.....
    Last edited by Frinavale; May 4 '10, 02:26 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    On line 4 in the above posted code, the second condition of your if statement is this:
    str = "Friend"

    Notice how you only have one equal sign?
    This means that you are trying to assign str the value "Friend" but what you want to do is compare str to "Friend".

    To compare a value to another value you use the comparison operator. The comparison operator is two equal signs.

    So you should have:

    str == "Friend"

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      That one gets a lot of people. Two equal signs can blend together depending on your font.

      I love the proggy fonts and have Visual Studio using "Crisp". I like being able to see slashes in my zeros and find it much easier on the eyes and easier to distinguish things like == from =

      When first learning C++ I would remember this as I typed by pronouning them in my head as I typed
      == Is Equal {to}
      = Equals

      1 sign, 1 words, "Equals" a declarative to say A equals 4
      2 signs, 2 words, "Is equal" a question to say if (a is equal to 4 )

      Just one of those silly mnemonics we create along life.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I'm on the default of Courier New as a font and its been good about spacing. The 0 vs O is a little close though

        Comment

        Working...