Conditional Logic - If Statements :VB Net

Started by sukishan, Jul 11, 2009, 06:23 PM

Previous topic - Next topic

sukishan

Conditional Logic - If Statements

What is conditional logic? Well, it's something you use in your daily life all the time, without realising you're doing it. Suppose that there is a really delicious cream cake in front of you, just begging to be eaten. But you are on a diet. The cake is clearly asking for it. So what do you do, eat the cake and ruin your diet? Or stick to your diet and let somebody else have that delicious treat? You might even be saying this to yourself:

If I eat the cake Then my diet will be ruined

If I don't eat the cake Then I will be on course for a slimmer figure
Note the words If and Then in the above sentences. You're using conditional logic with those two words: "I will eat the cake on condition that my diet is ruined". Conditional logic is all about that little If word. You can even add Else to it.

If I eat the cake Then my diet will be ruined

Else

If I don't eat the cake Then I will be on course for a slimmer figure

And that is what conditional Logic is all about - saying what happens if one condition is met, and what happens if the condition is not met. Visual Basic uses those same words - If, Then, Else for conditional Logic. Let's try it out.

Start a new project. Give it any name you like. In the design environment, add a Button to the new form. Double click the button and add the following code to it:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click

Dim firstname As String

firstname = "Bill"
If firstname = "Bill" Then MsgBox("firstname is Bill")

End Sub

Run the programme and see what happens. You should get a Message Box with the words "firstname is Bill" in it.

What we did was to set up a string variable and put the name "Bill" into it. When then used conditional logic to test what was in the variable. In fact, we used an If statement. We said:

If the variable called firstname holds the value "Bill" Then display a Message Box

We can tidy that up a bit, because a single line of code can get very long with If statements. We can use this format instead.

If firstname = "Bill" Then
MsgBox "firstname is Bill"
End If

That's a lot tidier. Note that we start a new line after the word Then.

The first line contains our condition: "If the following condition is met".
The second line is what we want to do if the condition is indeed met.
The third line tells Visual Basic that the If statement ends right here.
Try this. Delete the two quotation marks around the word Bill in the If Statement. Your code should now be this:

Dim firstname as String

firstname = "Bill"

If firstname = Bill Then
MsgBox "firstname is Bill"
End If

VB.NET puts a blue wiggly line under Bill. If you try to start your programme, you'll get a message box telling you that there were Build Errors, and asking if you want to continue.

Say No to return to the design environment. The reason for the blue wiggly line is that VB insists on you using double quotes to surround you text. No double quotes and VB insists it's not a string of text.

Change you code to this.

firstname = "Phil"

If firstname = "Bill" Then
MsgBox "firstname is Bill"
Else
MsgBox "firstname is not Bill"
End If
Now run the programme and see what happens when you click the button.


A good beginning makes a good ending

redroses

I was having this same problem until today. I thought that was the issue, then I found this post - sure enough it works now. :clown
simulation credit immobilier