简体   繁体   中英

pass the value of a variable to another method C#

I am new to programming. How do I pass the value of a variable to another method? The code variable gets a value, but when the method exits, it is reset to zero. I need to set the visitcode variable to the value of the code variable. I've tried declaring a code variable in the public class, but it doesn't work either. i did it like this

public partial class _Default : System.Web.UI.Page
Int32 code = new Int32();



protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "visitcode")
    {
        Int32 code = Convert.ToInt32(e.CommandArgument);
    }
}

protected void button1_click(object sender, EventArgs e)
{
    string num = number.Value;
    string document = doc.Value;
    string format = "yyyy-MM-dd HH:mm:ss:fff";
    string stringDate = DateTime.Now.ToString(format);
    string visitcode = code
}

Each HTTP request is treated as a new request, this means that your "code" variable is always cleaned every time it reaches the server, so ASP.NET WebForms offers a way to store temporary values in the session variable.

Your code could see like this:

protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "visitcode")
    {
        Session["Code"] = e.CommandArgument;
    }
}

In a second request, you could retrieve the value as follows:

protected void button1_click(object sender, EventArgs e)
{
    ...
    string visitcode = Session["Code"];
}

As I mentioned, Session variable is temporary so you have to validate if your value is different to NULL, if so this means that the session is over.

I hope it is useful for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM