简体   繁体   中英

List.Add(this) inside of a class

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

}  

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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