none
If, and, or statements problem. RRS feed

  • Question

  • I am making a command checker thing on my computer. I'll show you a piece of the code:

    cmd = cmdBox.Text

            'Split cmd at each space and put every part in to an array called sepWords (separate words)
            Dim sepWords As String() = cmd.Split(" "c, "."c)



            'Open the default web browser and search for google.com

            If sepWords.Contains("start") Or sepWords.Contains("launch") Or sepWords.Contains("open") And sepWords.Contains("internet") Or sepWords.Contains("browser") Or sepWords.Contains("web") Then

                Process.Start("http://www.google.com")

            End If

    I think the code explains itself pretty well. 

    I have a textbox called cmdBox. In that box you can write for example "start browser".

    Then i click a buton and this code runs. I want the code to check if any of the words: start, launch or open exists together with any of the words: internet, browser or web. Then the Process.Start should run. Notice that in the middle of the If statement there's an AND placed out. This didn't work out as planned. It's enough with me writing "browser" for the Process.Start to run. It's like it's ignoring my AND and treats it like OR..... Any tips? 


    How do i write my signature on the computer?

    Thursday, June 20, 2013 11:58 AM

Answers

  • Try separating your if statement into two - that'll make the precedence of the AND/OR operators explicit. So instead of:

    If sepWords.Contains("start") Or sepWords.Contains("launch") Or sepWords.Contains("open") And sepWords.Contains("internet") Or sepWords.Contains("browser") Or sepWords.Contains("web") Then

    'Do stuff here

    end if

    Try using this:

    If sepWords.Contains("start") Or sepWords.Contains("launch") Or sepWords.Contains("open") Then If sepWords.Contains("internet") Or sepWords.Contains("browser") Or sepWords.Contains("web") Then

    'Do Stuff here

    end if end if


    • Marked as answer by OptiGE Sunday, June 23, 2013 7:52 PM
    Thursday, June 20, 2013 12:13 PM