C# 静态类和数据成员问题

发布于 2024-10-24 01:50:00 字数 612 浏览 1 评论 0原文

我不确定如何使用 C# .Net 3.5 实现我的想法。我有一个名为 Common 的静态类,其中包含常用方法。其中一种方法是PrepareReportParameters。此方法接受字符串 ReportParams 并解析它以获取参数值。我将此 ReportParams 字符串加载到 Dictionary 中。然后验证所需要的元素是否存在。我这样检查:

if (ReportParamList.ContainsKey("PAccount"))
{
    ReportParamList.TryGetValue("PAccount", out PrimaryAccount);
}

其中 PrimaryAccount 是我的 Common 类中的静态变量。我可以在其他地方以 Common.PrimaryAccount 的形式访问它。

虽然这种访问报告参数的方法可以工作,但我希望以 Common.ReportParameters.PrimaryAccount 的形式访问 PrimaryAccount。 问题是,我不知道 ReportParameters 应该是什么类型,以及如何将所有报告参数添加到此类型?我应该如何定义ReportParameters?听起来可行还是没有任何意义。请帮忙!

I am not sure how to implement what I have in mind using C# .Net 3.5. I have a static class called Common which contains common methods. One of the method is PrepareReportParameters. This method accepts a string ReportParams and parse it to get the parameter values. I load this ReportParams string into a Dictionary . And then verify whether the required elements exist. I check that like:

if (ReportParamList.ContainsKey("PAccount"))
{
    ReportParamList.TryGetValue("PAccount", out PrimaryAccount);
}

where PrimaryAccount is a static variable in my Common class. And I can access this elsewhere as Common.PrimaryAccount.

Though, this approcah of accessing the report parameters will work but I want PrimaryAccount to be accessed as Common.ReportParameters.PrimaryAccount.
Here is the problem, I don't know what type ReportParameters should be and how can I have all the report parameters added to this type? How should I define ReportParameters? Does it sound feasible or it doesn't make any sense. Please H E L P!

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

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

发布评论

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

评论(3

感情洁癖 2024-10-31 01:50:00

听起来您基本上习惯使用全局变量来传递状态。这通常是一个非常糟糕的主意。

为什么您的方法不返回主要帐户值?然后可以将其传递给其他需要它的事物。

如果您发现自己有很多静态成员,特别是如果其他类正在获取可变静态变量,请考虑是否可以应用更多的面向对象设计。它将更容易理解、更容易测试、更容易维护。

编辑:好的,目前您拥有:

public static class Common
{
    public static int PrimaryAccount;
    // other static fields

    public static void PrepareReportParameters(string reportParameters)
    {
        // Code to set the fields
    }
}

而不是使用普通类:

public class ReportParameters
{
    public int PrimaryAccount { get; private set; }
    // Other properties

    private ReportParameters(int primaryAccount, ....)
    {
        this.PrimaryAccount = primaryAccount;
    }

    // Could use a constructor instead, but I prefer methods when they're going to
    // do work
    public static ReportParameters Parse(string report)
    {
        // Parse the parameter, save values into local variables, then
        return new ReportParameters(primaryAccount, ...);
    }
}

然后从代码的其余部分调用它,并将 ReportParameters 引用传递给需要它的任何内容。

It sounds like you're basically used to using global variables to pass around state. That's generally a really bad idea.

Why doesn't your method just return the primary account value? That can then be passed to other things which need it.

If you find yourself with a lot of static members - and in particular if other classes are fetching mutable static variables - consider whether there's a more OO design you could apply. It'll be easier to understand, easier to test, and easier to maintain.

EDIT: Okay, so currently you have:

public static class Common
{
    public static int PrimaryAccount;
    // other static fields

    public static void PrepareReportParameters(string reportParameters)
    {
        // Code to set the fields
    }
}

Instead of that, use a normal class:

public class ReportParameters
{
    public int PrimaryAccount { get; private set; }
    // Other properties

    private ReportParameters(int primaryAccount, ....)
    {
        this.PrimaryAccount = primaryAccount;
    }

    // Could use a constructor instead, but I prefer methods when they're going to
    // do work
    public static ReportParameters Parse(string report)
    {
        // Parse the parameter, save values into local variables, then
        return new ReportParameters(primaryAccount, ...);
    }
}

Then call this from the rest of your code, and pass the ReportParameters reference to anything that needs it.

路弥 2024-10-31 01:50:00

您可以创建一个名为 ReportParameters 的类,并具有相关的强类型属性,并为 Common 提供它的静态实例?

You could create a class called ReportParameters with the relevant strongly-typed properties, and give Common a static instance of it?

撩起发的微风 2024-10-31 01:50:00

我不确定这是最好的设计。仅在调用PrepareReportParameters 后才允许访问Common.PrimaryAccount,这存在一定程度的“代码味道”。也许您会考虑一个实例类,在构造函数中传入参数?

I'm not sure this is the best design. Theres a certain amount of 'code smell' to having Common.PrimaryAccount only to be allowed to be accessed after PrepareReportParameters is called. Maybe you'd consider an instance class, passing in the parameters in the constructor?

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