WL_07

I coded a car race program and everything functions properly, except for the display. The program is supposed to display the number of correct and incorrect choices made by the player depending on his selection, but my program isn't displaying either one. Instead, it is displaying the number of times the program is run. Here is the piece I think is not functioning correctly:

Beginning...

Dim sChoice As String

Dim sResult As String

Static iCorrectNum As Integer

Static iIncorrectNum As Integer

sChoice = lbxResults.SelectedIndex.ToString (From a list box with selections)

...

(Area where I think the problem is)...

If sChoice <> "No Guess" Then

If sResult = sChoice Then

iCorrectNum = iCorrectNum + 1

Else

iIncorrectNum = iIncorrectNum + 1

End If

lblCorrectDisplay.Text = Convert.ToString(iCorrectNum)

lblIncorrectDisplay.Text = Convert.ToString(iIncorrectNum)

End If

Any help is appreciated.



Re: Visual Basic Express Edition Display Problem

Jeff Wharton

What does the variable 'sResult' contain and where is it being set More than likely this is never equal to 'sChoice' therefore the condition is always false resulting in 'ilncorrectNum' always being incremented

Also, you can use:

'iCorrectNum += 1' and 'iIncorrectNum += 1' to increment these variables instead of 'iCorrectNum = iCorrectNum + 1' and 'iIncorrectNum = iIncorrectNum + 1'

'lblCorrectDisplay.Text = Convert.ToString(iCorrectNum)' can also be written as 'lblCorrectDisplay.Text = iCorrectNum.ToString'

'lblIncorrectDisplay.Text = Convert.ToString(iIncorrectNum)' can also be written as 'lblIncorrectDisplay.Text = iIncorrectNum.ToString'






Re: Visual Basic Express Edition Display Problem

WL_07

Here's the whole program, if it'll help to clarify the problem:

Private Sub StartRaceTimer_Tick(ByVal Sender As Object, ByVal e As System.EventArgs) Handles StartRaceTimer.Tick

Dim randomnum As New Random

Dim sChoice As String

Dim sResult As String

Dim iWhiteCarPos As Integer

Dim iRedCarPos As Integer

Dim iFinishLine As Integer = txtFinishLine.Left + 1

Static iCorrectNum As Integer

Static iIncorrectNum As Integer

sChoice = lbxResults.SelectedIndex.ToString

lbxResults.Enabled = False

iWhiteCarPos = pbxWhiteCar.Right + randomnum.Next(0, 15)

If iWhiteCarPos > iFinishLine Then

iWhiteCarPos = iFinishLine

End If

iRedCarPos = pbxRedCar.Right + randomnum.Next(0, 15)

If iRedCarPos > iFinishLine Then

iRedCarPos = iFinishLine

End If

pbxWhiteCar.SetBounds(iWhiteCarPos - pbxWhiteCar.Width, 0, 0, 0, _

BoundsSpecified.X)

pbxRedCar.SetBounds(iRedCarPos - pbxRedCar.Width, 0, 0, 0, BoundsSpecified.X)

If pbxWhiteCar.Right = iFinishLine OrElse pbxRedCar.Right = iFinishLine Then

StartRaceTimer.Enabled = False

For x As Integer = 1 To 5

Console.Beep(100, 100)

Next x

If iWhiteCarPos = iRedCarPos Then

sResult = "Tie"

ElseIf iWhiteCarPos > iRedCarPos Then

sResult = "White Car Wins"

Else

sResult = "Red Car Wins"

End If

MessageBox.Show("Race Over!" & ControlChars.NewLine & sResult, "Car Race", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

pbxWhiteCar.SetBounds(1, 0, 0, 0, BoundsSpecified.X)

pbxRedCar.SetBounds(1, 0, 0, 0, BoundsSpecified.X)

If sChoice <> "No Guess" Then

If sResult = sChoice Then

iCorrectNum = iCorrectNum + 1

Else

iIncorrectNum = iIncorrectNum + 1

End If

lblCorrectDisplay.Text = Convert.ToString(iCorrectNum)

lblIncorrectDisplay.Text = Convert.ToString(iIncorrectNum)

End If

lbxResults.Enabled = True

End If

End Sub





Re: Visual Basic Express Edition Display Problem

Jeff Wharton

Change

sChoice = lbxResults.SelectedIndex.ToString

to

sChoice = lbxResults.SelectedItem

and it works.






Re: Visual Basic Express Edition Display Problem

WL_07

Dude, you rock!

Thanks for the help.