Stange effects?

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?????
 
K

Kevin Spencer

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.
 
S

Sherif ElMetainy

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
 
A

Alexander M. Polak

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top