Stange effects?

  • Thread starter Thread starter Alexander M. Polak
  • Start date Start date
A

Alexander M. Polak

One of our students wrote on a vb web form
Public intIndex As Integer = 0


Private Sub btSet_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btSet.Click
intIndex = 2
lblOriginal.Text = "intIndex = : " & CType(intIndex, String)
End Sub


Private Sub btAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btAdd.Click
intIndex += intIndex
lblResult.Text = "intIndex = : " & CType(intIndex, String)
End Sub

Clicking on the button set wil seed the integer int Index with 2
Clicking on the Add button adss the index to it's self
Both times de value is displayed on the screen.
Strait forward no?
Cliking the btAdd displays a "0" Why?????
 
I'm not sure I understand the question. You have 2 buttons on the page, both
of which display a calculated result from an integer field value that is
initialized to 0. The first button's Event Handler sets the value of the
integer to 2, and displays that value (2). The second button's Event Handler
doubles the value of the same integer and displays that. Therefore, as the
integer is initialized to 0, if you click the first button and then the
second, the second will display 4 (2 + 2). If you click the second button
first, the second will display 0 (0 + 0). Is there a problem with that?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.
 
Hello

The value int the integer value is not saved between trips to the server, so
every time it is initialized to zero.
To save the value, use the ViewState collection, then retrieve the value in
the Load event

Regards
 
Yes thanks
Found te same answer just a minute ago.
Even if public every trip around the integer is reset again to the old value
 
Back
Top