groberts1980 Posted February 7, 2004 Posted February 7, 2004 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. Quote
Administrators PlausiblyDamp Posted February 7, 2004 Administrators Posted February 7, 2004 If the variable is declared local to the DrivingRange button's click event then it cannot be seen from another sub or function in the class, static or not. Can't think of any other way than using class level variables myself. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* DiverDan Posted February 7, 2004 *Experts* Posted February 7, 2004 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 Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
groberts1980 Posted February 8, 2004 Author Posted February 8, 2004 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 Quote
*Experts* DiverDan Posted February 8, 2004 *Experts* Posted February 8, 2004 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. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Tryster Posted February 9, 2004 Posted February 9, 2004 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.