How to use public int?!!?!!? windows form.
-
Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:
public void Random_Click(object sender, EventArgs e)
{
Random myRandom = new Random();
int myNumber = myRandom.Next(1,10);
}
public void Button2_Click(object sender, EventArgs e)
{
MessageBox(myNumber.ToString());
}This will not work and i cannot find the way to get it right plese help !!!
-
Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:
public void Random_Click(object sender, EventArgs e)
{
Random myRandom = new Random();
int myNumber = myRandom.Next(1,10);
}
public void Button2_Click(object sender, EventArgs e)
{
MessageBox(myNumber.ToString());
}This will not work and i cannot find the way to get it right plese help !!!
one way is to declare a global variable try this: public int myNumber; public void Random_Click(object sender, EventArgs e) { Random myRandom = new Random(); myNumber = myRandom.Next(1,10); } public void Button2_Click(object sender, EventArgs e) { MessageBox(myNumber.ToString()); } hope this helps
-
Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:
public void Random_Click(object sender, EventArgs e)
{
Random myRandom = new Random();
int myNumber = myRandom.Next(1,10);
}
public void Button2_Click(object sender, EventArgs e)
{
MessageBox(myNumber.ToString());
}This will not work and i cannot find the way to get it right plese help !!!
Your problem is working out the SCOPE of your variable. If you declare it in the method it dies when the method completes. if you declare it provate in the form (I ususally put them just before the form_load method) then the variable will be available until you close the form.
public clsLimitValue oValue { get; set; } private BindingSource oBS = new BindingSource(); private void frmNewLimit_Load(object sender, EventArgs e)
Never underestimate the power of human stupidity RAH
-
Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:
public void Random_Click(object sender, EventArgs e)
{
Random myRandom = new Random();
int myNumber = myRandom.Next(1,10);
}
public void Button2_Click(object sender, EventArgs e)
{
MessageBox(myNumber.ToString());
}This will not work and i cannot find the way to get it right plese help !!!
Hi, maybe this[^] will inspire you. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
one way is to declare a global variable try this: public int myNumber; public void Random_Click(object sender, EventArgs e) { Random myRandom = new Random(); myNumber = myRandom.Next(1,10); } public void Button2_Click(object sender, EventArgs e) { MessageBox(myNumber.ToString()); } hope this helps
Hello still nothing cannot solve this problem, so what i tried is i tried declare variable as a (public int nameOfInteger) just before the method, in the begining oif the class and everywhere else. Still confused thats my code
namespace ExerciseInt
{
public partial class Form1 : Form
{
int newNumber;public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { Random myRandom = new Random myNumber = myRandom.Next(1,10); } private void button2\_Click(object sender, EventArgs e) { MessageBox.Show(myNumber.ToString()); } }
}
so when i click button1 variable should be set up and when click button2 i would like to see this variable in the message box. looking forward to hear from you.
-
Hello still nothing cannot solve this problem, so what i tried is i tried declare variable as a (public int nameOfInteger) just before the method, in the begining oif the class and everywhere else. Still confused thats my code
namespace ExerciseInt
{
public partial class Form1 : Form
{
int newNumber;public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { Random myRandom = new Random myNumber = myRandom.Next(1,10); } private void button2\_Click(object sender, EventArgs e) { MessageBox.Show(myNumber.ToString()); } }
}
so when i click button1 variable should be set up and when click button2 i would like to see this variable in the message box. looking forward to hear from you.
Are you using some IDE? It would be helpful if you could give some error reports and I think I could think of one. Now, that would be newNumber and myNumber. Are those supposed to be the same? I think so. :^)
I came, saw, and then coded.