Results 1 to 10 of 10

Thread: [2005] how could my variables get reset each time the button is pushed?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Smile [2005] how could my variables get reset each time the button is pushed?

    Hello I'm Writing a program that computes the number of ‘1’ bits in a given 32-bit integer number.
    I did a program that works fine with the first number i put. but when i try another number, it misses up!. then i have to run the program again. I think i need to reset count and moon at the begining of the button so the variables get reset each time the button is pushed. but I don't know how to do that!

    this what i have so far


    Code:
    Public Class Form1
    Dim count As Integer
    Dim moon As Integer = 2 ^ 31 - 1
    Dim i As Integer
     
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    i = TextBox1.Text
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x As Integer
    For x = 0 To 31
    If i >= moon Then
    count = count + 1
    i = i - moon
    End If
    moon = moon / 2
    Next
    MsgBox(count)
    End Sub
    End Class



    thank you,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] how could my variables get reset each time the button is pushed?

    Of course you know how. If you want count and moon set to specific values at the beginning of the Button's Click event handler then that's exactly what you do: you set count and moon to specific values at the start of the Button's Click event handler. I know you know how to set a variable because I can see you doing it in that code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Re: [2005] how could my variables get reset each time the button is pushed?

    well, I did not get the point yet I'm still beginner

    anyway, i tried it again this way. the first time you put a number, it works fine. if you change the number, it works fine. if you keep the number, it gives you "0"

    Code:
    Public Class Form1
        Dim i As Integer
        Dim x As Integer
    
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            i = TextBox1.Text
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim count As Integer
            Dim moon As Integer = 2 ^ 31 - 1
            For x = 0 To 31
                If i >= moon Then
                    count = count + 1
                    i = i - moon
                End If
                moon = moon / 2
            Next
    
            MsgBox(count)
        End Sub
    End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] how could my variables get reset each time the button is pushed?

    When do you get the value the user has entered from the TextBox? When they enter it. Where do you assign that value? To the 'i' variable. When you perform your calculation your are changing the value of the 'i' variable. If the user then clicks the Button again without changing the value in the TextBox then you're going to start your calculation with the wrong value for 'i'.

    Get rid of that TextChanged event handler altogether and just get the value from the TextBox when you need it: when the Button is clicked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] how could my variables get reset each time the button is pushed?

    vb Code:
    1. Public Class Form1
    2.     Dim i As Integer
    3.     Dim x As Integer
    4.  
    5.  
    6.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    7.         i = TextBox1.Text
    8.     End Sub
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.  
    12.         Dim count As Integer
    13.         Dim moon As Integer = 2 ^ 31 - 1
    14.         For x = 0 To 31
    15.             If i >= moon Then
    16.                 count = count + 1
    17.                 i = i - moon
    18.             End If
    19.             moon = moon / 2
    20.         Next
    21.  
    22.         MsgBox(count)
    23.  
    24.         i = TextBox1.Text
    25.  
    26.     End Sub
    27. End Class

  6. #6
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] how could my variables get reset each time the button is pushed?

    Why is 'i' and 'x' defined a class level? I thought you only defined Public variables at class level?

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer = TextBox1.Text
            Dim count As Integer = 0
            Dim moon As Integer = 2 ^ 31 - 1
            For x As Integer = 0 To 31
                If i >= moon Then
                    count += 1
                    i -= moon
                End If
                moon = moon / 2
            Next
    
            MsgBox(count)
        End Sub
    End Class
    Try that...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] how could my variables get reset each time the button is pushed?

    'i' was being used at class level

  8. #8
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] how could my variables get reset each time the button is pushed?

    Yes, but I have never seen you do 'Dim i As Integer'

    It should be Public i As Integer = 0, if it is being used at class level...
    shouldn't it?

    And the Text_Changed sub was not necessary...

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] how could my variables get reset each time the button is pushed?

    no. dim or private for class level, accessible only within that class
    public, accessible within the project

  10. #10
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] how could my variables get reset each time the button is pushed?

    Ah I see, but it's still not required to be a class level variable, as the Text_Changed event is not required...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width