简体   繁体   English

有没有可能在c#4.5中存储临时整数值的方法?

[英]is there any possible way to store temporary integer value in c# 4.5?

I am creating WPF application. 我正在创建WPF应用程序。 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? 有没有可能使用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); 
    } 
} 

I think you're having a bit of a misunderstanding here ... WPF (Windows Presentation Foundation), is a technology that came to replace WinForms ... 我认为您在这里有点误会... WPF(Windows Presentation Foundation)是一项取代WinForms的技术...

Storing temp integers would be done exactly like it'll be done in WinForms, Console application or a dll. 存储临时整数将完全像在WinForms,控制台应用程序或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 ... 如果您正在谈论MVVM,事情会变得更加有趣,但是您仍然可以存储和发送临时变量...

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 . 有关此选项的更多信息: MSDN:应用程序设置概述


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: 如果您想在每个i上执行某项操作,则Jossef Harush会赚钱,否则(或此外,您也可以执行以下操作:

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;
}

Welcome you to StackOverflow, i agree with @Noctis answer. 欢迎您使用StackOverflow,我同意@Noctis的回答。

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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