Problem with integers

groberts1980

New member
Joined
Feb 7, 2004
Messages
2
Im writing a program for the VB class Im taking, and Ive run into a problem. Basically, I need the program to count how many times a button is clicked, and display the results when another button is pressed. Here is the code I have for the Driving Range button, the one to be counted:

Static intDrivingRange As Integer
intDrivingRange += 1

My professor requires that all our declerations be local, which is why Im using static instead of a dim statement in the declerations section. These lines should add 1 to the integer DrivingRange each time the button is clicked. My problem is getting it to display the results when the Results button is clicked. Heres my code for the Results Button:

Static intDrivingRange As Integer
lblDrivingRange.Text = "Driving Range: " & (intDrivingRange)

When I click the Driving Range button a few times, then click the Results button, it always says "Driving Range: 0"

The part I cant figure is if I move the lblDrivingRange.text line into the same procedure as the counter, it keeps a running total as Im clicking Driving Range button and works fine. But for the project I need it to simply keep count until I hit the Results button. Only then should it display the results.

Is my problem with the counter or the way Im trying to display the results?

- groberts1980

P.S. Using class-level variables does work for this program. My problem is, at this stage, my professor wants us to use only local variables. There are 6 buttons I need counters for, and when I Dim all 6 variables at the class level, the program works fine. But like I said I have to use local variables. Is there any way to create the variable locally in the DrivingRange button procedure, and have the Results button use that variable and its value? If I have to turn in the program using class level variables I will, but I would prefer to have it work using only local variables. I know what youre thinking, there are probably many VB professors out there who are better than the one I got, but Im stuck with him. Ergo, Im stuck being told to use only local variables.
 
If the variable is declared local to the DrivingRange buttons click event then it cannot be seen from another sub or function in the class, static or not.
Cant think of any other way than using class level variables myself.
 
How about setting a variable in an IncreaseValue Function?

Code:
Private Function IncreaseValue(By Val value as Integer) As Integer
Return value += 1
End Function

Then you can simply pass the last local vairable into this function and it will return the variable + 1
 
Two things. First, how do I pass the local variable into this function? And second, when I typed the function into the code, it gave me an error with (By Val value As Integer) it says "Comma, or ) expected." But I typed it exactly as you posted it.

Sorry, Im am a newbie at VB and I dont know exactly how to apply the idea here.

groberts1980
 
I dont know...works for me

Code:
    Private Function IncreaseNumber(ByVal value As Integer) As Integer
        Return value + 1
    End Function

to use the function, first establish a variable in your local event then call the function with the variable....

Code:
a = 1
a = IncreaseNumber(a)

the returned value is 2. Its the same as increasing the variable except that it can be called from anywhere in your program.
 
Assuming the buttons on your form are named btnDrivingRange and btnResults, the code DiverDan is proposing would look something like this:

Code:
Private Sub btnDrivingRange_Click(sender as Object, e as EventArgs) Handles btnDrivingRange.Click
    GetValue(True)
End Sub

Private Sub btnResults_Click(sender as Object, e as EventArgs) Handles btnResults.Click
    lblDrivingRange.Text = "Driving Range: " & GetValue(False)
End Sub

Private Function GetValue(increment as Boolean) As Integer
    Static iValue as Integer

    If increment Then
        iValue += 1
    End If

    Return iValue
End Function
 
Back
Top