Computing.Net > Forums > Programming > Visual Basic IF help

Visual Basic IF help

Reply to Message Icon

Original Message
Name: beastathon
Date: March 17, 2007 at 06:07:14 Pacific
Subject: Visual Basic IF help
OS: XP
CPU/Ram: 2.4ghz / 512ram
Comment:

Hello all, I'm currently trying to create a text based adventure in Visual Basic 6.

Although when I am comparing my IF statements to see which turn it is, it runs fine, but it also reads an IF statement that I don't want it to. Let me show you the code,

+++++++++++++++++++++++++++++++++++++++++++++

Private Sub go_Click()

' Room 3

If turn = 2 Then
If inp.Text = "go north" Then
desc.Caption = "A rusty sign reads 'Block E-7'. You are in a huge blank room with pale white walls. You glance over at the corner, there sits an overflowing bin"
loct.Caption = "Block E-7"
turn = 3
End If
Else
cant.Caption = "Cannot " + inp.Text
End If

If turn = 2 Then
If inp.Text = "open door" Then
cant.Caption = "Door is locked."
turn = 2
End If
End If

' Room 2
If turn = 1 Then
If inp.Text = "go north" Then
desc.Caption = "You are in a narrow corridor, the light continues north. You notice a small door to the right of you."
loct.Caption = "Narrow Corridor"
cant.Caption = ""
turn = 2
End If
Else
cant.Caption = "Cannot " + inp.Text
End If

inp.Text = ""

End Sub

++++++++++++++++++++++++++++++++++++++++++++

When I am at Room 3, it correctly does it but it also does the code at Room 2:

If inp.Text = "go north" Then

When it is under If turn = 1.
Basically I want it to stop reading the If statements under it, or make sure that ONLY IF turn = 1 do that block, none other.

I'm sorry if i sound confusing. Any questions please ask.

Thank you in advance.


Report Offensive Message For Removal


Response Number 1
Name: Sci-Guy
Date: March 17, 2007 at 14:54:42 Pacific
Reply: (edit)

You should use AND to tell your program not to execute a block of code unless both conditions are true.

+++++++++++++++++++++++++++++++++++++++++++++++++

Private Sub go_Click()

' Room 3

If turn = 2 And inp.Text = "go north" Then
desc.Caption = "A rusty sign reads 'Block E-7'. You are in a huge blank room with pale white walls. You glance over at the corner, there sits an overflowing bin"
loct.Caption = "Block E-7"
cant.Caption = ""
turn = 3
Else
cant.Caption = "Cannot " + inp.Text
End If

If turn = 2 And inp.Text = "open door" Then
cant.Caption = "Door is locked."
turn = 2
End If

' Room 2
If turn = 1 And inp.Text = "go north" Then
desc.Caption = "You are in a narrow corridor, the light continues north. You notice a small door to the right of you."
loct.Caption = "Narrow Corridor"
cant.Caption = ""
turn = 2
Else
cant.Caption = "Cannot " + inp.Text
End If

inp.Text = ""

End Sub

+++++++++++++++++++++++++++++++++++++++++++++++++

This may be able to be optimised further, but I just corrected what was obvious to me at a glance.

Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal

Response Number 2
Name: beastathon
Date: March 17, 2007 at 20:03:24 Pacific
Reply: (edit)

Thanks, but now that I look further, I think the problem is that the variable in the IF block ISNT setting...


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Visual Basic IF help

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge