1

I'm trying to create a game; When I click a button, I need to go to another page by using Query string, I need to know which button is clicked (Button1), and how many times it's clicked(Turn). Here's my code:

int Turn = 0;
protected void Button1_Click(object sender, EventArgs e) 
{
            string clickedButton = "Button1";
            Turn = Turn ++;
            Response.Redirect(string.Format("TheGame.aspx?button={0}&turn={1}", clickedButton , Turn)); 
}

But the thing is, I can never change my turn, It always stays 0. How can I do so?

Miss.Alpha
  • 111
  • 1
  • 10
  • 1
    You simply need to read the `Turn` value *out of* the query on each page refresh as well. Every request to the server for that page will restart the Turn variable in the page class at 0, you need to look at the query string and obtain the *current turn* value and store into the Turn variable. – Lasse V. Karlsen May 18 '16 at 18:25

4 Answers4

2

that's because whenever a new page is created Turn is set to 0. You should store Turn in Session an use it like this :

public int Turn
{
    get
    {
        if (Session["Turn"] == null)
            Session["Turn"]= 0;
        return (int)Session["Turn"];
    }
    set
    {
        Session["Turn"] = value;
    }
}
Kahbazi
  • 11,634
  • 1
  • 38
  • 62
1

The simplest way is to make the variable turn static,

static int Turn = 0;

that'll do!

Miss.Alpha
  • 111
  • 1
  • 10
0

Two reasons.

First, the post-fix increment operator returns the value prior to incrementing, so Turn++ increments Turn to 1, then returns 0. And you assign that value back to Turn, thus undoing your increment. Just use

Turn++

Second, Turn is being declared at the Page-class level. Every time the page loads, a new instance of this page is built, with a new Turn that is initialized to zero.

You'll need to add some sort of turn-tracking service-class to your solution, that tracks a users turns over the lifetime of the game. You'll also need to determine how the idea "lifetime of the game" maps to an abstraction on the server-side.

You can continue to use the query string as you are currently, but instead of initiating a page-level variable you should just use a local variable, increment, and return the new value with the new query string. Another option would be Session-state.

Marc L.
  • 2,801
  • 1
  • 27
  • 38
  • Thank you, now I understand! But what are those turn-tracking service-classes? Any example? – Miss.Alpha May 18 '16 at 17:14
  • @Miss.Alpha It's something you would write, a class that presents an interface for the Turn abstraction, and encapsulates the details of how it is managed internally. Inside, it might use session-state, the query string or even a database. This kind of code abstraction might be overkill in your case, though. You could directly use any underlying storage for `Turn` and increment it manually. The broader point is that you need to think about the "turn" as an idea and then how that idea should be translated to code. – Marc L. May 19 '16 at 16:29
0

The "++" operator increments the variable it is attached to. When used as a suffix like this, it retrieves the existing value, then increments the variable. When used as a prefix, it increments the variable, then retrieves the value. In this case, it retrieves the value "0", then increments Turn to "1". However, the assignment then changes the value of Turn back to "0".

Either use Turn++ by itself, or use the assignment Turn = Turn + 1 which is what I would do.

protected void Button1_Click(object sender, EventArgs e) 
{
        string clickedButton = "Button1";
        Turn = Turn + 1;
        Response.Redirect(string.Format("TheGame.aspx?button={0}&turn={1}", 
            clickedButton , Turn)); 
}

The "++" operator is often used like this:

protected void Button1_Click(object sender, EventArgs e) 
{
        string clickedButton = "Button1";
        Response.Redirect(string.Format("TheGame.aspx?button={0}&turn={1}", 
            clickedButton , ++Turn)); 
}

Note that I used the "++" as a prefix so that the value used to format the string would be the post-increment value "1" rather then the pre-increment value "0".

David Williams
  • 446
  • 4
  • 5