c# undef所有参数的快速方法

发布于 2025-01-08 19:17:04 字数 214 浏览 0 评论 0原文

我有一个 C# winform 应用程序,它正在执行大量计算。有一个“运行”按钮来触发该过程。我希望能够“重新触发或重新运行或重新提交”信息,而无需重新启动程序。问题是我有很多变量需要重置。有没有办法取消(重置)所有参数?

private Double jtime, jendtime, jebegintime, javerage, .... on and on

I have a C# winform app which is doing a lot of calculation. there is a "run" button to trigger the process. I would like to be able to "re-trigger or re-run or re-submit" the information without having to restart the program. Problem is I have a lot of variables that need to be reset. Is there a way to undef (reset) all parameters?

private Double jtime, jendtime, jebegintime, javerage, .... on and on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狂之美人 2025-01-15 19:17:04

创建存储这些变量的对象实例。引用该对象,当想要“重置”时,重新实例化您的对象。例如

public class SomeClass
{
   public double jTime;
   ...
}

...

SomeClass sc = new SomeClass();
sc.jTime = 1;
sc = new SomeClass();

Create an instance of an object that stores these variables. Reference this object, and when wanting to "reset", reinstantiate your object. e.g.

public class SomeClass
{
   public double jTime;
   ...
}

...

SomeClass sc = new SomeClass();
sc.jTime = 1;
sc = new SomeClass();
苏辞 2025-01-15 19:17:04

最好的办法就是将它们全部集中在一个班级中。
然后在重置时,您只需创建一个具有初始化值的新类。

The best way would have been if you had them all in a class.
Then on reset you'd just create a new class with initialized values.

微凉徒眸意 2025-01-15 19:17:04

你可以使用反射;虽然反射的性能低于其他建议的解决方案,但我不完全确定您的解决方案领域,反射可能是一个不错的选择。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Data data = new Data();

            //Gets all fields
            FieldInfo[] fields = typeof(Data).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //Might want to put some logic here to determin a type of the field eg: (int, double) 
                //etc and based on that set a value

                //Resets the value of the field;
                field.SetValue(data, 0);
            }

            Console.ReadLine();
        }

        public class Data
        {
            private Double jtime, jendtime, jebegintime, javerage = 10;
        }
    }
}

You could use Reflection; although Reflection is the less performant than the other proposed solutions, but I am not entirely sure of your solution domain and Reflection might be a good option.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Data data = new Data();

            //Gets all fields
            FieldInfo[] fields = typeof(Data).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //Might want to put some logic here to determin a type of the field eg: (int, double) 
                //etc and based on that set a value

                //Resets the value of the field;
                field.SetValue(data, 0);
            }

            Console.ReadLine();
        }

        public class Data
        {
            private Double jtime, jendtime, jebegintime, javerage = 10;
        }
    }
}
黑白记忆 2025-01-15 19:17:04

是的,只需使用 Extract Method 重构技术即可。基本上在单独的方法中提取重置逻辑,然后在需要时调用它

private void ResetContext()
{
   jtime = jendtime = jebegintime = javerage = 0;
}

Yes, simply use Extract Method refactoring technique. Basically extract reset logic in a separate method and then just call it when need

private void ResetContext()
{
   jtime = jendtime = jebegintime = javerage = 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文