none
How do i declare an integer? VB RRS feed

  • Question

  • How do i declare an integer in vb.net?

    i tryed this code:


       Dim test As New Integer
       test = 0
    
       Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            test = test + 1
            Label1.Text = test
            If Label1.Text = "100000" Then
                test = 0
            End If
        End Sub


    basicly, what i want to do is:

    1. when the form loads, Make a integer called test.

    2. Timer3's interval is set to 1.

    3. every time Timer3 ticks, i want the integer to go up by one.'

    4. after test has added 1 to itself, i want Label1's text, to match the integers value

    5. check if label1's text is 100000, if it is, make the integer equal to 0

    6. repeat steps 3 & 4

    The problem is that it sais that the Integer is NOT declared.

    Sunday, December 3, 2017 6:35 PM

Answers

  •  It would simply be like this...

    Dim test as Integer = 0

     

      Also,  to assign it's value to a Label which is a String type,  you should use the ToString method...

    Label1.Text = test.ToString


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, December 3, 2017 6:43 PM
    • Proposed as answer by Frank L. Smith Sunday, December 3, 2017 6:44 PM
    • Marked as answer by Nave GCT Sunday, December 3, 2017 8:12 PM
    Sunday, December 3, 2017 6:42 PM

All replies

  •  It would simply be like this...

    Dim test as Integer = 0

     

      Also,  to assign it's value to a Label which is a String type,  you should use the ToString method...

    Label1.Text = test.ToString


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, December 3, 2017 6:43 PM
    • Proposed as answer by Frank L. Smith Sunday, December 3, 2017 6:44 PM
    • Marked as answer by Nave GCT Sunday, December 3, 2017 8:12 PM
    Sunday, December 3, 2017 6:42 PM
  • How do i declare an integer in vb.net?

    i tryed this code:


       Dim test As New Integer
       test = 0
    
       Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            test = test + 1
            Label1.Text = test
            If Label1.Text = "100000" Then
                test = 0
            End If
        End Sub


    basicly, what i want to do is:

    1. when the form loads, Make a integer called test.

    2. Timer3's interval is set to 1.

    3. every time Timer3 ticks, i want the integer to go up by one.'

    4. after test has added 1 to itself, i want Label1's text, to match the integers value

    5. check if label1's text is 100000, if it is, make the integer equal to 0

    6. repeat steps 3 & 4

    The problem is that it sais that the Integer is NOT declared.

    I see where you're going with it.

    As a suggestion, try this with one label on your form (based on what I did a few minutes ago):

    Public Class Form1
        Private WithEvents tmr As Timer
        Private counter As Integer
    
    
        Private Sub _
            Form1_Load(sender As Object, _
                       e As EventArgs) _
                       Handles MyBase.Load
    
            tmr = New Timer _
                With {.Interval = 1000, _
                      .Enabled = True}
    
            Label1.Text = ""
    
        End Sub
    
    
        Private Sub _
            tmr_Tick(sender As Object, _
                     e As System.EventArgs) _
                     Handles tmr.Tick
    
            counter += 1
    
            Dim ts As TimeSpan = TimeSpan.FromSeconds(counter)
    
            Label1.Text = String.Format("{0:00}:{1:00}{2:00}", _
                                        ts.Hours, _
                                        ts.Minutes, _
                                        ts.Seconds)
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, December 3, 2017 7:09 PM
  •  

    Nave GCT,

     If you use a Timer that is created dynamically in the code as Frank has shown in his example,  be aware that you should be doing one or the other of the following ....

     ....either add it to the Form's Components container so it will be disposed when your app is closed and the Form is disposed....

    Public Class Form1
        Private WithEvents tmr As New Timer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If Me.components Is Nothing Then
                Me.components = New System.ComponentModel.Container
            End If
            Me.components.Add(tmr)
        End Sub
    
        '..... rest of your code
    
    End Class
    
     

     ... Or use the Form's Closing event to dispose the Timer control.

    Public Class Form1
        Private WithEvents tmr As New Timer
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            tmr.Dispose()
        End Sub
    
        '..... rest of your code
    
    End Class
    
     

     However,  the best way would be to add it to the Form in the Designer which will add it to the Form's Components container automatically.  I am sure Frank only showed that code as a simplified example and knows about this but,  i figured i would mention the differences just so you are aware of it if you create it dynamically.  8)



    If you say it can`t be done then i`ll try it

    Sunday, December 3, 2017 7:32 PM
  • This one helped a lot! Thanks so much!

    it solved my problem.


    The Code Rules

    Sunday, December 3, 2017 8:13 PM
  • Your first answer helped.

    Thank you for helping,

          Nave GCT


    The Code Rules

    Sunday, December 3, 2017 8:14 PM