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

I know, it just doesn't want to set the variable to the value I want.

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.lblGreen1 = New System.Windows.Forms.Label
        Me.lblGreen2 = New System.Windows.Forms.Label
        Me.lblOrange1 = New System.Windows.Forms.Label
        Me.lblOrange2 = New System.Windows.Forms.Label
        Me.lblRed1 = New System.Windows.Forms.Label
        Me.cmdExit = New System.Windows.Forms.Button
        Me.cmdDouble = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.cmdCalc = New System.Windows.Forms.Button
        Me.lblCost = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.cmdNewBottle = New System.Windows.Forms.Button
        Me.lblDisplay = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'lblGreen1
        '
        Me.lblGreen1.BackColor = System.Drawing.Color.Lime
        Me.lblGreen1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblGreen1.Location = New System.Drawing.Point(327, 69)
        Me.lblGreen1.Name = "lblGreen1"
        Me.lblGreen1.Size = New System.Drawing.Size(100, 23)
        Me.lblGreen1.TabIndex = 0
        '
        'lblGreen2
        '
        Me.lblGreen2.BackColor = System.Drawing.Color.Lime
        Me.lblGreen2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblGreen2.Location = New System.Drawing.Point(327, 102)
        Me.lblGreen2.Name = "lblGreen2"
        Me.lblGreen2.Size = New System.Drawing.Size(100, 23)
        Me.lblGreen2.TabIndex = 1
        '
        'lblOrange1
        '
        Me.lblOrange1.BackColor = System.Drawing.Color.DarkOrange
        Me.lblOrange1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblOrange1.Location = New System.Drawing.Point(327, 135)
        Me.lblOrange1.Name = "lblOrange1"
        Me.lblOrange1.Size = New System.Drawing.Size(100, 23)
        Me.lblOrange1.TabIndex = 2
        '
        'lblOrange2
        '
        Me.lblOrange2.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(128, Byte), Integer), CType(CType(0, Byte), Integer))
        Me.lblOrange2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblOrange2.Location = New System.Drawing.Point(327, 168)
        Me.lblOrange2.Name = "lblOrange2"
        Me.lblOrange2.Size = New System.Drawing.Size(100, 23)
        Me.lblOrange2.TabIndex = 3
        '
        'lblRed1
        '
        Me.lblRed1.BackColor = System.Drawing.Color.Red
        Me.lblRed1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblRed1.Location = New System.Drawing.Point(327, 200)
        Me.lblRed1.Name = "lblRed1"
        Me.lblRed1.Size = New System.Drawing.Size(100, 23)
        Me.lblRed1.TabIndex = 4
        '
        'cmdExit
        '
        Me.cmdExit.BackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer))
        Me.cmdExit.Font = New System.Drawing.Font("Arial Black", 12.0!, System.Drawing.FontStyle.Bold)
        Me.cmdExit.Location = New System.Drawing.Point(12, 296)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.Size = New System.Drawing.Size(127, 56)
        Me.cmdExit.TabIndex = 4
        Me.cmdExit.Text = "E&xit"
        Me.cmdExit.UseVisualStyleBackColor = False
        '
        'cmdDouble
        '
        Me.cmdDouble.BackColor = System.Drawing.Color.Aqua
        Me.cmdDouble.Font = New System.Drawing.Font("Arial Black", 12.0!, System.Drawing.FontStyle.Bold)
        Me.cmdDouble.Location = New System.Drawing.Point(156, 235)
        Me.cmdDouble.Name = "cmdDouble"
        Me.cmdDouble.Size = New System.Drawing.Size(127, 45)
        Me.cmdDouble.TabIndex = 1
        Me.cmdDouble.Text = "&Double"
        Me.cmdDouble.UseVisualStyleBackColor = False
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("Calibri", 30.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(50, 9)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(325, 49)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "Cheers Tot System"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'cmdCalc
        '
        Me.cmdCalc.BackColor = System.Drawing.Color.Fuchsia
        Me.cmdCalc.Font = New System.Drawing.Font("Arial Black", 12.0!, System.Drawing.FontStyle.Bold)
        Me.cmdCalc.Location = New System.Drawing.Point(300, 235)
        Me.cmdCalc.Name = "cmdCalc"
        Me.cmdCalc.Size = New System.Drawing.Size(127, 45)
        Me.cmdCalc.TabIndex = 2
        Me.cmdCalc.Text = "&Calculate"
        Me.cmdCalc.UseVisualStyleBackColor = False
        '
        'lblCost
        '
        Me.lblCost.BackColor = System.Drawing.Color.White
        Me.lblCost.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblCost.Font = New System.Drawing.Font("Franklin Gothic Medium", 27.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblCost.Location = New System.Drawing.Point(300, 296)
        Me.lblCost.Name = "lblCost"
        Me.lblCost.Size = New System.Drawing.Size(127, 57)
        Me.lblCost.TabIndex = 12
        Me.lblCost.TextAlign = System.Drawing.ContentAlignment.BottomRight
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Font = New System.Drawing.Font("Calibri", 20.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(155, 305)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(128, 33)
        Me.Label2.TabIndex = 13
        Me.Label2.Text = "Total Due:"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(283, 69)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(38, 13)
        Me.Label3.TabIndex = 14
        Me.Label3.Text = "750 ml"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(274, 210)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(47, 13)
        Me.Label4.TabIndex = 15
        Me.Label4.Text = "< 150 ml"
        '
        'cmdNewBottle
        '
        Me.cmdNewBottle.BackColor = System.Drawing.Color.Gold
        Me.cmdNewBottle.Font = New System.Drawing.Font("Arial Black", 12.0!, System.Drawing.FontStyle.Bold)
        Me.cmdNewBottle.Location = New System.Drawing.Point(12, 235)
        Me.cmdNewBottle.Name = "cmdNewBottle"
        Me.cmdNewBottle.Size = New System.Drawing.Size(127, 45)
        Me.cmdNewBottle.TabIndex = 3
        Me.cmdNewBottle.Text = "&New Bottle"
        Me.cmdNewBottle.UseVisualStyleBackColor = False
        '
        'lblDisplay
        '
        Me.lblDisplay.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblDisplay.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblDisplay.Location = New System.Drawing.Point(12, 69)
        Me.lblDisplay.Name = "lblDisplay"
        Me.lblDisplay.Size = New System.Drawing.Size(265, 122)
        Me.lblDisplay.TabIndex = 17
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(441, 367)
        Me.Controls.Add(Me.lblDisplay)
        Me.Controls.Add(Me.cmdNewBottle)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.lblCost)
        Me.Controls.Add(Me.cmdCalc)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.cmdDouble)
        Me.Controls.Add(Me.cmdExit)
        Me.Controls.Add(Me.lblRed1)
        Me.Controls.Add(Me.lblOrange2)
        Me.Controls.Add(Me.lblOrange1)
        Me.Controls.Add(Me.lblGreen2)
        Me.Controls.Add(Me.lblGreen1)
        Me.Name = "Form1"
        Me.Text = "Cheers Tot System"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents lblGreen1 As System.Windows.Forms.Label
    Friend WithEvents lblGreen2 As System.Windows.Forms.Label
    Friend WithEvents lblOrange1 As System.Windows.Forms.Label
    Friend WithEvents lblOrange2 As System.Windows.Forms.Label
    Friend WithEvents lblRed1 As System.Windows.Forms.Label
    Friend WithEvents cmdExit As System.Windows.Forms.Button
    Friend WithEvents cmdDouble As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents cmdCalc As System.Windows.Forms.Button
    Friend WithEvents lblCost As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents cmdNewBottle As System.Windows.Forms.Button
    Friend WithEvents lblDisplay As System.Windows.Forms.Label

End Class

I don't either spot any logical error in the code.

Have you tried

Dim iBottle As Integer

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  '
  iBottle = 750

End Sub

or just

Dim iBottle As Integer

if the user hits cmdNewBottle as the first button.

adatapost>gareth wants to initialize all variables.

Public Sub cmdNewBottle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNewBottle.Click
         iBottle = 750
         iTots= 0
         icost=0
         iNumberOfDrinks= 0
         iTotalDrinksLeft = 15
         iOverOrder= 0
         iNewBottle=0

        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

adatapost>gareth wants to initialize all variables.

No he doesn't, only iBottle:

the "cmdNewBottle_Click" subroutine should reset "iBottle" to its original value. But It's not doing it.

Take a closer look at the first post :)

Anyway, if gareth has the same problem with some other variable, the same solution would work with that too.

Teme64, I appreciate your quick response. I run gareth's code and I found that he/she want to restart the all action once the click on newBottle Button.

Teme64, I appreciate your quick response.

Thanks :)

But the original question, why iBottle variable doesn't reset is still unsolved. Assuming gareth posted all the relevant code, it should reset it's value. There's just nothing wrong in the first code posted or in the designer code.

Maybe gareth should comment, if he still has this problem.

Hey guys, the iBottle should reset back to its original value after the user presses the New Bottle button. The other variables values are changed by the other buttons.
I'm going to try this:

1.
      Dim iBottle As Integer
   2.
       
   3.
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   4.
      '
   5.
      iBottle = 750
   6.
       
   7.
      End Sub

The iBottle is declared with all other variables at the start of the code. And then again its value set in a button, but it's like it's not recognising that part of the code.

I tried:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

iBottle = 750

End Sub

Epic fail, how would I use it? I tried putting that code in the New Bottle button, but it says you can't put it in a method argument, something like that.

I tried putting that code in the New Bottle button, but it says you can't put it in a method argument

That

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
iBottle = 750
End Sub

is form's load event handler i.e. a procedure of its own. You do not put that code inside the button's event handler. Just Copy/Paste that snippet (three lines of code) in the file, for example right before Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click line.

This article has been dead for over six months. Start a new discussion instead.