List.Add(this) 在类中

发布于 2024-08-24 23:41:50 字数 1985 浏览 4 评论 0原文

我不久前创建了一个类。我在类内部使用了 List.Add(this),这样我就可以访问稍后创建的控件。它似乎非常有用,我不知道如何创建控件(同一父控件中的多个控件,没有预定义的限制)并稍后访问它们。

我在互联网上寻找 Add(this),但找不到更多相关信息。

这是资源消耗大还是效率低?为什么我找不到更多相关信息?看起来很有用。

public class GlobalData
{

    private static List<Member> _Members;

public partial class ChildrenPanel
{

    private static List<ChildrenPanel> _ListCP = new List<ChildrenPanel>();

    //X and Y position Panel | Container is the control recieving the Control
    public void CreatePanel(int X, int Y, Panel Container)
    {
        // 
        // pnlStudent
        // 
        _pnlStudent.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        _pnlStudent.Controls.Add(_lblCLastName);
        _pnlStudent.Controls.Add(_lblCFirstName);
        _pnlStudent.Controls.Add(_lblGrade);
        _pnlStudent.Controls.Add(_lblSelected);
        _pnlStudent.Controls.Add(_lblSeason);
        _pnlStudent.Controls.Add(_lblAvailable);
        _pnlStudent.Controls.Add(_lblGender);
        _pnlStudent.Controls.Add(_ddlGrade);
        _pnlStudent.Controls.Add(_ddlSelectedSports);
        _pnlStudent.Controls.Add(_ddlAvailableSports);
        _pnlStudent.Controls.Add(_ddlSeason);
        _pnlStudent.Controls.Add(_rdbFemale);
        _pnlStudent.Controls.Add(_rdbMale);
        _pnlStudent.Controls.Add(_btnRemoveChild);
        _pnlStudent.Controls.Add(_btnRemoveSport);
        _pnlStudent.Controls.Add(_btnAddSport);
        _pnlStudent.Controls.Add(_txtCLastName);
        _pnlStudent.Controls.Add(_txtCFirstName);
        _pnlStudent.Location = new System.Drawing.Point(X, Y);
        _pnlStudent.Name = "pnlStudent";
        _pnlStudent.Size = new System.Drawing.Size(494, 105);
        //Still playing with the tab index
        _pnlStudent.TabIndex = 10;

        // Adds controls to selected forms panel
        Container.Controls.Add(_pnlStudent);
        // Creates a list of created panels inside the class
        ListCP.Add(this);

}  

I created a class awhile back. I used List.Add(this) inside of the class so I could access the controls I created later. It seemed to be very useful and I do not know how to create controls (more than one in the same parent control without a predefined limit) and access them later.

I was looking for Add(this) on the internet and couldn't find anymore information on it.

Is this a large resource hog or ineffective? Why can't I find more information on it? It seems very useful.

public class GlobalData
{

    private static List<Member> _Members;

public partial class ChildrenPanel
{

    private static List<ChildrenPanel> _ListCP = new List<ChildrenPanel>();

    //X and Y position Panel | Container is the control recieving the Control
    public void CreatePanel(int X, int Y, Panel Container)
    {
        // 
        // pnlStudent
        // 
        _pnlStudent.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        _pnlStudent.Controls.Add(_lblCLastName);
        _pnlStudent.Controls.Add(_lblCFirstName);
        _pnlStudent.Controls.Add(_lblGrade);
        _pnlStudent.Controls.Add(_lblSelected);
        _pnlStudent.Controls.Add(_lblSeason);
        _pnlStudent.Controls.Add(_lblAvailable);
        _pnlStudent.Controls.Add(_lblGender);
        _pnlStudent.Controls.Add(_ddlGrade);
        _pnlStudent.Controls.Add(_ddlSelectedSports);
        _pnlStudent.Controls.Add(_ddlAvailableSports);
        _pnlStudent.Controls.Add(_ddlSeason);
        _pnlStudent.Controls.Add(_rdbFemale);
        _pnlStudent.Controls.Add(_rdbMale);
        _pnlStudent.Controls.Add(_btnRemoveChild);
        _pnlStudent.Controls.Add(_btnRemoveSport);
        _pnlStudent.Controls.Add(_btnAddSport);
        _pnlStudent.Controls.Add(_txtCLastName);
        _pnlStudent.Controls.Add(_txtCFirstName);
        _pnlStudent.Location = new System.Drawing.Point(X, Y);
        _pnlStudent.Name = "pnlStudent";
        _pnlStudent.Size = new System.Drawing.Size(494, 105);
        //Still playing with the tab index
        _pnlStudent.TabIndex = 10;

        // Adds controls to selected forms panel
        Container.Controls.Add(_pnlStudent);
        // Creates a list of created panels inside the class
        ListCP.Add(this);

}  

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

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

发布评论

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

评论(3

左岸枫 2024-08-31 23:41:50

只需确保在不再需要实例时再次删除实例,否则持有对它的引用的列表将永远将其保留在内存中(毕竟,欢迎来到 .NET 中的内存泄漏)。

Just make sure that you Remove the instance again when it's no longer needed, otherwise the List holding a reference to it will keep it in memory forever (Welcome to memory leaks in .NET after all).

水晶透心 2024-08-31 23:41:50

一旦我看到一些代码,我可能会修改这个答案,但我最初的反应是它不是资源消耗者。至于是否有效,需要一些示例代码。

将对象添加到集合中不会占用大量资源,因为您只是将对象的引用添加到集合中。您仍然只有一个对象,但有两个(或更多)变量指向该对象,因此您使用的唯一额外资源是引用使用的最小内存。

I may revise this answer once I see some code, but my initial response is that it is not a resource hog. As to whether it is effective or not, some example code will be required.

Adding an object to a collection does not take up a large amount of resources because you are simply adding a reference to the object into the collection. You still only have a single object, but two (or more) variables that point to that object, so the only extra resources you are using are the minimal memory used by the references.

萝莉病 2024-08-31 23:41:50

如果您的列表是静态的或全局可用的,那么您正在做一些非常糟糕的事情。

ASP.Net 的结构使得每个用户对页面的每个请求(包括回发)都会生成页面类的新实例。这是很多页面实例。如果对所有这些实例的引用都保存在某处,则这些实例永远不会被垃圾回收。您已经创建了类似于内存泄漏的东西,并且在部署到生产环境后您很快就会发现自己耗尽了资源。

这里真正危险的事情是,如果您只进行功能测试而不进行负载测试,那么问题可能根本不会在测试期间出现,因为它可以在几百个(甚至几千个)请求中正常工作,然后就会崩溃。 。

如果您担心动态控件,有几种更好的方法可以解决此问题:

  1. 对允许的最大控件数量设置固定限制,并将所有控件都添加到前面的页面中。然后仅在需要时显示/渲染它们(通过 .Visible 属性切换)。
  2. 使其由数据驱动。不要动态添加控件,而是将某些内容插入到数据库表中,然后将该表上的查询绑定到转发器或其他数据控件(我的首选方法)。
  3. 只需确保重新创建每个动态即可您需要在页面生命周期中的正确位置(预初始化)进行控制。

If your List is static or otherwise globally available, then you're doing something very bad.

ASP.Net is structured such that every request to your page - including postbacks - from every user results in a new instance of the page class. that's a lot of page instances. If references to all these instances are saved somewhere, the instances can never be garbage collected. You've created something analogous to a memory leak and you'll quickly find yourself running out of resources after you deploy to production.

The really dangerous thing here is that if you only do functional testing and no load testing the problem will likely not show up during your tests at all, because it will work fine for a few hundred (maybe even thousand) requests before blowing up on you.

If you're worried about dynamic controls, there are several better ways to handle this:

  1. Put a fixed limit on the maximum number of controls you will allow, and add all of them to the page up front. Then only show/render them (toggled via the .Visible property) as you need them.
  2. Make it data-driven. Rather than dynamically add a control, insert something to a database table and then bind a query on that table to a repeater or other data control (my preferred method).
  3. Just make sure you're recreating every dynamic control you need at the right place (Pre-Init) in the page lifecycle.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文