Counting

I want to make a program where you click a button and the textbox tells you how many times that button has been clicked. However it only counts the first click. What have i done wrong

Option Explicit On
Option Strict On

Public Class MainForm

Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub


Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
' displays the number of times the Count button is clicked

Dim numClicks As Integer

' add 1 to the numClicks variable each time the button is clicked

numClicks = 0
numClicks = numClicks + 1


' display message
xMessageLabel.Text = "You have clicked the Count button " & _
Convert.ToString(numClicks) & " time(s)."
End Sub
End Class


Answer this question

Counting

  • SweptSquash

    THat's because I forgot to delete something it should be:

    I get the "end statement expected" when I type numClicks += 1

    learn syntax.



  • gwestwater

    Shouldnt the line declaration line for numclicks be within a class declaration.

    Otherwise you'll generate a Statement is not valid in namespace compile error.

    Probably just a typo.


  • bluefocs

     

     

    Well, we're mixed agreement becuase in a way Tall Dude is right too. By using a static variables he is keeping it modular and self-contained. In that respect it's desireable. However I think syntactically they fall apart. And they do have a valuable function.

    Since they are allocated at the beginning or running of an assembly, I always thought there'd be a slight performance enhancement to use them. But a couple of weeks ago, I was running some crude benchmarks with lots of calls to a routine and they didn't make a difference in those tests.

    Also, I learned something else. Remember a long time ago when we had a discussion on how much memory is required by a byte and I said I'd read that it actually required 12 bytes I just found out, during that benchmark and reading some really obscure stuff that I was right. Heap, has a representatiin or descriptor of every variable and the minimum size of the representation is 12 bytes. I believe that for 64 bits that's either going to double again or be 12+4. I think it's going to double though.



  • erinselena

    Probably or you could infer ignorance but that would be your inference.



  • kangalert

    This is also a good time to use a 'static' type counter.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    Static i As Integer = 1

    MsgBox("You have clicked this button " _

    & i.ToString & " time(s)")

    i += 1

    End Sub

    End Class



  • Jenise

    Hmmm, a good point: when should static variables be used, if at all

    I agree with you renee, if you aren't paying attention, you can overlook the fact that a variable is declared static. Even if you are paying attention, sometimes you just don't see it. Hence, I'd avoid them ordinarily.

    However, I was actually suprised that static declarations were still a part of the .NET framework (don't know why, just was). I use them in very few circumstances:

    • The routine is short (less than a screens worth).
    • as a flag to prevent reentrancy on a function.
    • in timers, if I need to 'slow down' a firing event (a counter).

    I think this is a coding preference: it's great to encapsulate a function. In this particular instance, either style could be used - as long as only the routine needs it (the variable), Tall Dudes answer is a very good one (although I'd call it something other than 'i', which is historically used as a loop variable, but again - coding style).



  • Osmose1000

    Tall dude,

    I didn't forget static variables. I don't like the syntax of the intialization of the static variable because it looks like each time the procedure is executed, the value is set to the initial value even when it's not.



  • 23

    Three more reasons to use them:

    1. Diagnostic purposes. Say an exception shows up several minutes into a program in a function that is called many times. I want to start debugging prior to that point. So I put in a static counter at the top of the function, run it to crash again, and then set a breakpoint on the resulting hit count.

    2. Caching constant values or objects. Say when you enter a function, you need to create some kind of object, but that object can stay the same for each invocation of the function. You first set the pointer/ref to null, and then when you enter you create the object, set the static variable to it, and it's now cached. In one of my singleton implementations I do something like this.

    3. Returning constant strings. A class has a method that returns its name as a string, for example. It's constant, and you don't want to dynamically allocate it. So it is a local static, and returning its pointer/ref is valid because that string hasn't "gone out of scope.". .NET makes this unnecessary with initializable static data members, but it's a technique that I use in C++ to emulate that behavior.


  • Kris2006

     

    Option Explicit On
    Option Strict On

     
    Public Class MainForm

       Protected numClicks As Integer


        Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
            Me.Close()
        End Sub


        Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
            ' displays the number of times the Count button is clicked

           

            ' add 1 to the numClicks variable each time the button is clicked

          
            numClicks += 1


            ' display message
            xMessageLabel.Text = "You have clicked the Count button " & _
                Convert.ToString(numClicks) & " time(s)."
        End Sub
    End Class



  • search and deploy

    I get the "end statement expected" when I type numClicks = += 1
  • Counting