|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Help with incrementing variable
I am not sure what I am doing wrong...
I am trying to increment a variable but each time it will only increment once and then resets to 0 I have declared a variable in the class section of my codebehind.vb of my aspx page public class webform1 ... dim i as integer ... I have to buttons that trigger subs (fwd and back) which should increment the variable by 1 each time sub goFwd() .... i = i + 1 .... end sud sub goBack() ... i = i - 1 ... But no matter how many times I click the button to trigger goFwd, i always = 1, it doesnt seem to increment the value each time. If any can understand what I mean here and can help me, it would be greatly appreciated. |
|
#2
|
|||
|
|||
|
If the coding is done properly it will work fine. Make sure the variable is declared globally and isnt being interfered with by another command.
Public I as integer I += 1 or I -= 1 This could be your problem |
|
#3
|
|||
|
|||
|
I believe it is coded properly...here is what I've got, it still doesnt work
Public Class WebForm2 Inherits System.Web.UI.Page Public i as integer ..... Private sub Page_Load(.....) handles MyBase.load response.write(i) end sub sub goFwd(......) i += 1 response.write(i) end sub end class everytime I click the button to trigger goFwd, I got a 01, the 0 from Page_Load and 1 from goFwd, it wont go to 2 or 3 or anything else, any ideas what I might be doing wrong? I know this is simple and everything looks fine to me but still wont work for some reason, Im sure im gonna kick myself when I find the solution.... |
|
#4
|
|||
|
|||
|
Try putting Public Shared I as integer instead. Also double check and make sure you are not using (i) anywhere else. From what I can see, it should work fine. You could also create a function an pass the integer back and forth. Not recommended though.
|
|
#5
|
|||
|
|||
|
I found what I was doing wrong..
Class member variables are not stored in the ViewState by default so each time the page loads or postbacks, a new instance of the class is created which kept intializing my i variable back to zero. So I had to use this in my Page Load event if not IsNothing(Viewstate("i")) Then i = Viewstate("i") else i = 0 Viewstate.Add("i", i) End If and then also call ViewState("i") = i after each time I changed the value. Thank you much for taking the time to try to help me, happy programming! |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Help with incrementing variable |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|