Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm writing a program for the VB class I'm taking, and I've 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 I'm 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. Here's 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 can't figure is if I move the lblDrivingRange.text line into the same procedure as the counter, it keeps a running total as I'm 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 I'm 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 you're thinking, there are probably many VB professors out there who are better than the one I got, but I'm stuck with him. Ergo, I'm stuck being told to use only local variables.

  • *Experts*
Posted

How about setting a variable in an IncreaseValue Function?

 


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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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, I'm am a newbie at VB and I don't know exactly how to apply the idea here.

 

groberts1980

  • *Experts*
Posted

I don't know...works for me

 

   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....

 


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.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

Assuming the buttons on your form are named btnDrivingRange and btnResults, the code DiverDan is proposing would look something like this:

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...