Multiple Stopwatches and dynamically naming variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jameswobrien
    New Member
    • Oct 2008
    • 1

    #1

    Multiple Stopwatches and dynamically naming variables

    Hi,

    I am trying to make a program to allows me to record times for certain events.
    I was thinking of using the system.diagnost ic.stopwatch class to do this.

    What i need to do is be able to use a for loop to create an X (user defined) number of stopwatches, each one called L and then the number of the stop watch.

    Then i need to press a button and it reads the value of another variable (F), and then stops the clock of L(F).

    Im not sure if this is possible and how to do it.

    Your help will be greatly appreciated

    Thanks james
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Well, how about making an array of them. Then you could read in F and stop L[F]. That should work. Just remember that arrays are zero-based, so if F is not zero based, you would want to stop L[F - F_base]. So if F starts at 1, you want to stop L[F - 1].

    Comment

    • jg007
      Contributor
      • Mar 2008
      • 283

      #3
      something like this? I have left it very simple but it basically creates x ( starting at '0' ) number of stopwatches and starts them and then when you click on button 2 it stops the one ( without error checking ) that has the number entered in textbox1.text

      Code:
       Dim x, f As Integer
          Dim l As New ArrayList
      
      
      
      
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              x = 5
      
              For n = 0 To x
                  l.Add(New System.Diagnostics.Stopwatch)
                  l(n).Start()
              Next
              Timer1.Enabled = True
      
          End Sub
      
          Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
              f = TextBox1.Text
              l(f).stop()
              MsgBox(CType(l(f), System.Diagnostics.Stopwatch).Elapsed.Seconds)
          End Sub
      
      
      End Class

      Comment

      Working...