Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

C# variable pass

Oct 6, 2007 2:12PM PDT

I got stuck, tryin to crank out how to pass 'xp' to click event #2

public partial class Form1: Form
{
bool firstclick=false;
public Form1()
{
InitializeComponent();
}
private void Form1_click1(object sender, EventArgs e)
{
if(firstclick==false){int xp = 1;firstclick=true;}
else{xp++;}
}
private void Form1_click2(object sender, EventArgs e)
{
expLabel.Text = "Exp: " + xp;
}
}

the variable 'xp' needs to be passed to click2 from click1.
can somone show how u would make a single passing method so that click3 and click4 can use the same passing method as click1 and click2.

Discussion is locked

- Collapse -
streamwrite/read
Oct 8, 2007 11:24AM PDT

is it possible to write 'xp' out to a file and then call the streamreader to bring the variable into the click event#2?
That would be too slow though wouldnt it? There has to be a more efficient way.

- Collapse -
A few options....
Oct 9, 2007 9:45AM PDT

1) Make the variable part of the class. So in this case, xp becomes part of the Form1 class so that everything can access it.

2) Store the variable into the "Session" or "Application" object to get it back when the second click is done.

3) Put the value of XP into a hidden field on the form so that each click will get it as part of the post back.

Regards,
JB

- Collapse -
lame
Oct 9, 2007 12:11PM PDT

the only statement that might be faster would be #2. So what your saying is simmilar to what i suggested in storing it in a kind of page file for the application?

- Collapse -
Not really...
Oct 10, 2007 2:31AM PDT

In the first one it is similar to the firstclick variable that is part of the Form class that doesn't have to be written out to a file as it should be passed back as part of the Form data possibly though it may have to be made static to achieve that on one level.

In the second case it isn't getting written into a file but is stored within memory if you understand how those objects work as they come from the old ASP style of things.

In the third case it is a matter of not liking either of the previous ideas.

- Collapse -
not worth it
Oct 11, 2007 11:19AM PDT

when u start making a huge project the resources skyrocket and now u got an application that is holding variables in memory when they really dont need to be...