Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2006
    Thanked 0 Times in 0 Posts

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

    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:

    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.

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Near Chicago, IL, USA
    Thanked 32 Times in 31 Posts
    If you want the new value saved, you need to set the member to the new value, not assign to a variable and increment the variable.

    Try this.
    (VB doesn't support ++ increment operator, I assume?)

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

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts