Hi, this is a program that I'm coding for a project. The code is a bit messy, but I'm having trouble with resetting the variable to the original value.
The variable "iBottle" is declared at the start of the program. It then gets subtracted by within the IF...ELSE structure in the "cmdCalc_Click" subroutine.
Now, the "cmdNewBottle_Click" subroutine should reset "iBottle" to its original value. But It's not doing it. I even made all the subroutines public, wasn't sure if I had scope problems. But that didn't fix it.
I really need to be able reset the variable, so that it can be re-used.
Here's the code:
Imports System.IO
Public Class Form1
Private Const Path As String = "..\..\Bottles_Used.txt"
Const cDoubleCost As Integer = 15
Const cDoubleFluid As Integer = 50
Dim iBottle As Integer = 750
Dim iTots As Integer = 0
Dim icost As Integer = 0
Dim iNumberOfDrinks As Integer = 0
Dim iTotalDrinksLeft As Integer = 15
Dim iOverOrder As Integer = 0
Dim iNewBottle As Integer
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Close()
End Sub
Public Sub cmdNewBottle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNewBottle.Click
iBottle = 750
lblGreen1.BackColor = Color.Lime
lblGreen2.BackColor = Color.Lime
lblOrange1.BackColor = Color.DarkOrange
lblOrange2.BackColor = Color.DarkOrange
lblRed1.BackColor = Color.Red
Dim textOut As New StreamWriter(New FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write))
textOut.Write(iNewBottle)
textOut.Close()
End Sub
Private Sub cmdDouble_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDouble.Click
lblCost.Text = ""
icost += cDoubleCost
iTots += cDoubleFluid
iNumberOfDrinks += 1
lblDisplay.Text = "Total tots ordered: " & iNumberOfDrinks.ToString & ControlChars.NewLine
End Sub
Public Sub cmdCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalc.Click
Dim textIn As New StreamReader(New FileStream(Path, FileMode.OpenOrCreate, FileAccess.Read))
If iNumberOfDrinks > iTotalDrinksLeft Then
iOverOrder = iNumberOfDrinks - iTotalDrinksLeft
MsgBox("Too many drinks!" & ControlChars.NewLine _
& "You ordered " & iOverOrder.ToString & "" _
& " Too many", MsgBoxStyle.Information)
iTots = 0
icost = 0
lblDisplay.Text = ""
lblCost.Text = ""
Else
iTotalDrinksLeft -= iNumberOfDrinks
lblDisplay.Text &= "Tots still left in this bottle: " & iTotalDrinksLeft.ToString
End If
If iTots < iBottle Then
iBottle -= iTots
If iBottle < 750 And iBottle >= 600 Then
lblGreen1.BackColor = Color.White
End If
If iBottle < 600 And iBottle >= 450 Then
lblGreen1.BackColor = Color.White
lblGreen2.BackColor = Color.White
End If
If iBottle < 450 And iBottle >= 300 Then
lblGreen1.BackColor = Color.White
lblGreen2.BackColor = Color.White
lblOrange1.BackColor = Color.White
End If
If iBottle < 300 And iBottle >= 150 Then
lblGreen1.BackColor = Color.White
lblGreen2.BackColor = Color.White
lblOrange1.BackColor = Color.White
lblOrange2.BackColor = Color.White
End If
If iBottle < 50 Then
lblGreen1.BackColor = Color.White
lblGreen2.BackColor = Color.White
lblOrange1.BackColor = Color.White
lblOrange2.BackColor = Color.White
lblRed1.BackColor = Color.White
End If
Else
iNewBottle = CInt(textIn.ReadLine)
iNewBottle += 1
MsgBox("Bottle empty!", MsgBoxStyle.Information)
End If
iTots = 0
lblCost.Text = icost.ToString
icost = 0
iNumberOfDrinks = 0
textIn.Close()
End Sub
End Class