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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Visual Basic IF help

Reply to Message Icon

Name: beastathon
Date: March 17, 2007 at 06:07:14 Pacific
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.



Sponsored Link
Ads by Google

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

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.


0

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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Java Tic Tac Toe help on creating DOS Batc...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Visual Basic IF help

Visual Basic.......help! www.computing.net/answers/programming/visual-basichelp/889.html

Visual Basic Project Help Needed... www.computing.net/answers/programming/visual-basic-project-help-needed/17187.html

Visual Basic: Menues www.computing.net/answers/programming/visual-basic-menues/15501.html