When working with string values, most of the time you’ll use the Equal To or Not Equal To operators. Is intNumber greater than or equal to 49? You can ask questions such as these, all of which have yes/no answers. In fact, If is far more flexible than this.
Visual basic not equal how to#
You know how to check whether a particular variable is equal to some value and execute code if this is the case. If intX = 3 Then MessageBox.Show(“intX = 3”) : intX = intX + 1 : Total += intX All statements must be on the same line and must be separated by colons, as in the following example: Multiple statements can also be executed within a single line If. You don’t need an End If at the end of a single-line If. If intX = 3 Then MessageBox.Show(“intX = 3”) Else MessageBox.Show(“intX is not 3”) However, it doesn’t provide the structure and flexibility of the multiline form and is usually harder to read: The single-line form is typically used for short, simple tests, and it saves space in the text editor. So try to keep the nesting of If statements to a minimum if you can. However, the more levels of nesting you have, the harder it is to follow what’s happening in your code. There’s no real limit to how far you can nest your If statements. It’s possible to nest an If statement inside another: Alternatively, you should use a Select Case block, which you will be looking at later in the chapter. Try to put the statements in the order they are most likely to be executed, with the most common one at the top. This slows your program if you have a lot of conditions to be tested. However, bear in mind that each ElseIf statement is executed as Visual Basic 2005 attempts to discover whether the condition is true. You can add as many ElseIf statements as you need to test for conditions. Note that you can still include the Else statement at the end to catch instances where intNumber is neither, but something else entirely:ĮlseIf intNumber = 27 Then MessageBox.Show(“‘intNumber’ is 27!”, “Simple If”) This time the code in the ElseIf statement ran because intNumber met the condition intNumber = 27. Run the code and you’ll see the message box shown in Figure 4-3. MessageBox.Show(“‘intNumber’ is neither 1000 nor 27!”, “Simple If”) End IfĢ. MessageBox.Show(“‘intNumber’ is 27!”, “Simple If”) Now take your Simple If program as an example to see how you can test for the value of intNumber being. If you want to test for more than one condition, you need to make use of the ElseIf statement. In this case, the value of intNumber is 27, but the condition being tested for is intNumber = 1000, so the code after the Else statement is run:Īllowing Multiple Alternatives with ElseIf The code following the Else statement runs if the condition in the If statement is not met. Run the code and you’ll see the message box shown in Figure 4-2. MessageBox.Show(“‘intNumber’ is not 1000!”, “Simple If”)Ģ. MessageBox.Show(“‘intNumber’ is, indeed, 1000!”, “Simple If”) ‘Here’s where you make a decision, ‘and tell the user what happened If intNumber = 1000 Then ‘Declare and set a variable Dim intNumber As Integer = 27 Private Sub btnAnotherIf_Click(ByVal sender As System.Object, _īyVal e As System.EventArgs) Handles btnAnotherIf.Click Change the code in the btnAnotherIf_Click procedure so that it looks like this: Expand on the previous Try It Out to see how it works.ġ. If you want to run one piece of code if the condition is true and another piece if the condition is false, you use the Else statement. In effect, the “true” code block is skipped. However, in this instance the statement was false, so the next line to be executed was the first line directly following the End If line (which is End Sub). If the statement were true, the line between the If and End If lines would have executed. The code block executes only if the statement is true, so it’s skipped. In this case, the question “Is intNumber equal to 1000?” comes out false.