[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,
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.
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
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.
Re: [2005] how could my variables get reset each time the button is pushed?
vb 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)
i = TextBox1.Text
End Sub
End Class
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...
Re: [2005] how could my variables get reset each time the button is pushed?
'i' was being used at class level
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...
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
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...