静态成员访问

发布于 2024-12-04 07:47:28 字数 1627 浏览 1 评论 0原文

很可能我的做法都是错误的,但我有一个名为 CategoryControl 的用户控件,可能有很多类似的控件,因此我决定将其许多功能用作静态方法会更好。我想知道是否有一种“更好”的方法来访问这些方法,然后在整个类中传递一个实例。这些方法是公共静态的,因为它们将被其他方法更新。我想到了制作扩展方法的想法..?

public CategoryControl(UserCategory userCategory)
{
   InitializeComponent();

   PopulateControl(userCategory, this);
}

private static void PopulateControl(UserCategory userCategory, CategoryControl instance)
{

   SetCategoryTitle(userCategory, instance);

   SetPercentCorrect(userCategory, instance);

   SetQuestionsMissed(userCategory, instance);

   SetBackgroundBar(userCategory, instance);

   SetForegroundBar(userCategory, instance);

}

更新::

更长的故事是我在屏幕上有一个面板,该面板包含相关的用户类别。我所说的相关是指用户可以选择更改课程,从而显示一组新的类别。用户还可以根据与软件的交互来更改类别的值。所以...

面板显示课程的类别。

我在面板中维护一个活动类别控件的列表,主窗体告诉面板何时绘制一组新的类别。

public void InitializeProgressPanel(UserCategories parentCategories)
{
   Contract.Requires(parentCategories != null, "parentCategories is null.");

   RemoveAllControlsFromList(_categoryControls);

   UserCategories sortedUserCategories = parentCategories.SortByWorst();

   int categoriesCount = parentCategories.Count();

   int spacer = (Height - (CategoryControl.Controls_Height * categoriesCount)) / categoriesCount+1;

   for (int i = 0; i < sortedUserCategories.Count; i++)
   {
      CategoryControl cc = new CategoryControl((UserCategory)sortedUserCategories[i]);

      cc.Left = 0;

      if (i == 0)
         cc.Top = spacer;
      else
         cc.Top = (Controls[i - 1].Bottom + spacer);

      Controls.Add(cc);
      _categoryControls.Add(cc);

      }
}

It is likely that I am going about this all wrong, but I have a user control called CategoryControl, there can be many like it, for that reason I decided that many of its functions would be better served as static methods. I wanted to know if there is a "better" way of accessing these methods then passing an instance all over the class. The methods are public static as they will be updated by other methods. The though of making extension methods comes to mind..?

public CategoryControl(UserCategory userCategory)
{
   InitializeComponent();

   PopulateControl(userCategory, this);
}

private static void PopulateControl(UserCategory userCategory, CategoryControl instance)
{

   SetCategoryTitle(userCategory, instance);

   SetPercentCorrect(userCategory, instance);

   SetQuestionsMissed(userCategory, instance);

   SetBackgroundBar(userCategory, instance);

   SetForegroundBar(userCategory, instance);

}

Updated::

The longer story is that I have a Panel on the screen, the panel contains relevant user categories. By relevant I mean that the user has the option of changing courses thus displaying a new set of categories. A user can also change the values of a category based on their interaction with the software. So...

A panel shows the categories of a course.

I maintain a list of the active Category Controls in the panel, and the main form tells the panel when to draw a new set of categories.

public void InitializeProgressPanel(UserCategories parentCategories)
{
   Contract.Requires(parentCategories != null, "parentCategories is null.");

   RemoveAllControlsFromList(_categoryControls);

   UserCategories sortedUserCategories = parentCategories.SortByWorst();

   int categoriesCount = parentCategories.Count();

   int spacer = (Height - (CategoryControl.Controls_Height * categoriesCount)) / categoriesCount+1;

   for (int i = 0; i < sortedUserCategories.Count; i++)
   {
      CategoryControl cc = new CategoryControl((UserCategory)sortedUserCategories[i]);

      cc.Left = 0;

      if (i == 0)
         cc.Top = spacer;
      else
         cc.Top = (Controls[i - 1].Bottom + spacer);

      Controls.Add(cc);
      _categoryControls.Add(cc);

      }
}

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

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

发布评论

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

评论(5

时光倒影 2024-12-11 07:47:28

如果我手头有一个可以扩展的类,我当然不会创建扩展方法。请记住,扩展方法的目的是扩展您自己无法扩展的类型。

接下来的问题是,你应该说:

class C 
{
    public void Foo() { ... }
}

或者

class C
{
    public static void Foo(C c) { ... }
}

我会问一些问题,例如:

  • 该类是否会被子类化?如果是这样,这应该是一个虚拟方法吗?
  • Foo 是实例对自身所做的事情,还是实例对它所做的事情?动物自己进食,但动物由其他人喂养

更新:

我还会问自己一些问题:

  • 您设置的属性和其他内容是否会改变?类中的可变性越少,测试就越容易,推理就越容易,错误也就越少。如果属性等永远不会改变,那么不要在任何类型的方法中设置它们。将它们设置在构造函数中,然后再也不用担心它们;他们是对的。

I would certainly not make extension methods if I had a class in hand that I could extend. Remember, the purpose of extension methods is to extend types that you cannot extend yourself.

The question at hand then is, should you say:

class C 
{
    public void Foo() { ... }
}

or

class C
{
    public static void Foo(C c) { ... }
}

I would ask some questions like:

  • Is the class ever going to be subclassed? If so, should this be a virtual method?
  • Is Foo the kind of thing that an instance does to itself, or the sort of thing that it has done to it? An animal eats on its own, but an animal is fed by someone else.

UPDATE:

Some more questions I'd ask myself:

  • Are the properties and whatnot you are setting ever going to change? The less mutability you have in a class, the easier it is to test, the easier it is to reason about, and the fewer bugs you'll have. If the properties and whatnot are never going to change then do not set them in any kind of method. Set them in the constructor and then never worry about them again; they're correct.
场罚期间 2024-12-11 07:47:28

为什么不让他们成为实例成员,然后这样做

private UserCategory _userCategory;

public CategoryControl(UserCategory userCategory)
{
   InitializeComponent();

   this._userCategory = userCategory;
   this.PopulateControl();
}

private void PopulateControl()
{
   // to see userCategory you'd do "this._userCategory"
   // to see the specific instance you could simply do "this"

   SetCategoryTitle();

   SetPercentCorrect();

   SetQuestionsMissed();

   SetBackgroundBar();

   SetForegroundBar();

}

Why not make them instance members, and do it like this

private UserCategory _userCategory;

public CategoryControl(UserCategory userCategory)
{
   InitializeComponent();

   this._userCategory = userCategory;
   this.PopulateControl();
}

private void PopulateControl()
{
   // to see userCategory you'd do "this._userCategory"
   // to see the specific instance you could simply do "this"

   SetCategoryTitle();

   SetPercentCorrect();

   SetQuestionsMissed();

   SetBackgroundBar();

   SetForegroundBar();

}
悲念泪 2024-12-11 07:47:28

似乎在交互涉及的两个类之一上拥有该功能比在某些第三方上更好。

我想到了两种方法:

  1. CategoryControl 可以有一个公共函数 PopulateCategory(UserCategory userCat)
  2. UserCategory 可以有一个公共函数 PopulateFromControl(CategoryControl ctrl)

如果所有关于标题和百分比等的操作都需要是单独的操作,那么您只需遵循上面的模型,但每个项目都有单独的功能。

Seems better to have the functionality on one of the two classes involved in the interaction, rather than on some third party.

Here are two ways that spring to mind:

  1. CategoryControl could have a public function PopulateCategory(UserCategory userCat)
  2. UserCategory could have a public function PopulateFromControl(CategoryControl ctrl)

If all those operations about title and percent etc need to be separate actions, you'd just follow the model above but have separate functions for each item.

一个人的旅程 2024-12-11 07:47:28

只是在黑暗中拍摄,但我可能会尝试更多类似这样的事情:

private void PopulateControl(UserCategory userCategory)
{
    CategoryTitle = GetCategoryTitle(userCategory);
    PercentCorrect = GetPercentCorrect(userCategory);
    ...
}

Just a shot in the dark here, but I'd probably try for something more like this:

private void PopulateControl(UserCategory userCategory)
{
    CategoryTitle = GetCategoryTitle(userCategory);
    PercentCorrect = GetPercentCorrect(userCategory);
    ...
}
写下不归期 2024-12-11 07:47:28

有些问题可能会有所帮助...(?)

  • 您认为使方法静态化有什么好处?将方法转换为静态方法,您将取消“this”的隐式传递,并每次都手动传递它。这有什么帮助? (它不会使代码变得更加高效,它只是意味着您必须将“实例”传递到您进行的每个调用中,因此您需要编写更多代码)

  • 用户类别变化很大吗?如果不是,与其在每次调用时都传递它,不如将其设为成员变量是否更有意义?

  • 您真的想一一调用所有这些静态方法来更改控件的所有不同参数吗?看看客户端如何使用这个类,您可能会发现您可以将所有这些选项整合到一个或两个方法中,这些方法接受一堆参数并一次性应用它们。 (通常,如果您想更改一项设置,则需要同时更改多项设置)

Some questions may help...(?)

  • What benefit do you perceive in making the methods static? Converting the method to static, you are taking away the implicit passing of "this", and passing it in manually every time. How does that help? (It won't make the code any more efficient, it just means you have to pass 'instance' into every call you make, so you need to write more code)

  • Does the user category change a lot? If not, rather than passing it in for every call, would it make more sense to make it a member variable?

  • Would you really want to call all these static methods one by one to change all the different parameters of the control? Look at how the client will use this class and you may find that you can roll all of those options into one or two methods that take a bunch of parameters and apply them all in one hit. (Often if you want to change one setting, you will want to change several settings together)

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