Reputation: 1234
I am creating WPF application. I need to store a temporary value in some variable in one method and I need to get back that value in another method. is there any possible way to store the temporary value in a variable using WPF?
From Comment:
private void Button_Click(object sender, RoutedEventArgs e)
{
count++;
for (int i = 1; i <= count; i++)
{
var tb = new TextBox();
mycanvas.Children.Add(tb);
tb.Name = "txtbox"+i;
tb.Width = 100;
Canvas.SetLeft(tb,50);
Canvas.SetTop(tb, i*20);
}
}
Upvotes: 0
Views: 1324
Reputation: 11763
I think you're having a bit of a misunderstanding here ... WPF (Windows Presentation Foundation), is a technology that came to replace WinForms ...
Storing temp integers would be done exactly like it'll be done in WinForms, Console application or a dll.
You'll save it as a variable that the other method can see (depending on where it is), or send it as a parameter.
If you're talking about MVVM, things get a bit more interesting, but you can still store and send temp variables ...
Having said that, you can also stash away variables in the application settings, and use them (or their default values you can set) however you want. Here's more on this option: MSDN: Application Settings Overview .
If you want to do something on every i
, Jossef Harush is on the money, otherwise (or in addition, you can do something like the following as well:
private int global_variable_name;
private void Button_Click(object sender, RoutedEventArgs e)
{
count++;
for (int i = 1; i <= count; i++)
{
var tb = new TextBox();
mycanvas.Children.Add(tb);
tb.Name = "txtbox"+i;
tb.Width = 100;
Canvas.SetLeft(tb,50);
Canvas.SetTop(tb, i*20);
// You can set global_variable_name here, but it'll be
// silly to set it to `i`, since it's changing
global_variable_name = 8;
}
}
private void SomeOtherMethod() {
// you can use global_variable_name here
var sum = global_variable_name + 3;
}
Upvotes: 1
Reputation: 34227
Welcome you to StackOverflow, i agree with @Noctis answer.
In addition (after reading your comment),
If you would like to pass the current loop's index to a custom method, this is what you should do:
private void Button_Click(object sender, RoutedEventArgs e)
{
int count = 10;
count++;
for (int i = 1; i <= count; i++)
{
var tb = new TextBox();
mycanvas.Children.Add(tb);
tb.Name = "txtbox" + i;
tb.Width = 100;
Canvas.SetLeft(tb, 50);
Canvas.SetTop(tb, i * 20);
// My custom method
MyMethod(i);
}
}
private void MyMethod(int i)
{
// Do something with i
Console.WriteLine(i);
}
Upvotes: 0