Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I increment a number in a Public Shared sub?

Status
Not open for further replies.

BlueBlade

Technical User
Joined
Mar 2, 2007
Messages
15
Location
US
Hi, I’m trying to increment a number each time the user click on a button. My code happens to be in a button that is a Public Shared sub. Because it is a public shared, I can’t access the query string in the usual manner like this:

Response.Redirect("default.aspx?question=1")
Dim num As String = Request.QueryString("question")

So I can’t use the Query String to increment my number.

The other trick I tried is to create a public class and set the number in that class. I then retrieve the number back from the class and try to increment it but each time I click on the button, the number resets to the original and does not increment. Here’s what the code looks like.


objNewNumber.QuestionChanged = newNumber
newNumber = newNumber + 1
qDetails = QuestionCatalog.GetQuestion(newNumber)
TheQuestion.lblTheQuestion.Text = qDetails.Question


That last piece of code may be a bit confusing but in short, all I want to do is to increment a number each time a user click on my button that is a Public Shared sub.
 
I tried a session, application, and cache variable but that too is illegal in this sub. Here’s the error message: You cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
 
No, I mean why are you not incrementing the counter from the page rather than the class? If there is a legitimate reason, then you can use httpContect.Current to get a reference to the Session/Application/Cache.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
I don't know if I've been trying to increment the couner from the page. Do you mean just inside a sub routain? I will try the httpContent.Current tip. Thanks I'll let you know if I was successful.
 
I don't know if I've been trying to increment the couner from the page. Do you mean just inside a sub routain?
I mean from anywhere within the Page as opposed to within a class. I can't see what the problem here is though. Surely if you want to increment a counter you just increment a session variable on the Button Click event?


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Ok I tried to use the httpContect.Current but I don't know how to use it. Here's what I tried.

httpContent.current.session("sessionVariable") = value

I'm sure it's totally wrong as this is a level that I've never programmed at.

How can I use it? Thanks again!
 
> httpContent.current.session("sessionVariable") = value

>> Does that session var exist ? (not session("sessionVariable") is nothing). If not, try the .Add("sessionVariable", value)


One other way is to use your global.asax (?) And create properties (shared)
 
This thread is getting far to complicated for what it should be. What should the counter do? Should it increment every time any user clicks the button (so if UserA and UserB both click it it will be 2) or should each user have their own counter?


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Ok I got it. I put a variable in the page like c8amsm suggested rather than the sub routain... it works fine now. Thanks for the help. It now increments the page each time the user click on the button. Thanks again for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top