Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating objects dynamically

In a UI app I’ve build, I create a label like this:

    private void InitializeCurrentPerlLabel() {
        currentPerlLabel = new System.Windows.Forms.Label();
        SuspendLayout();

        currentPerlLabel.AutoSize = true;
        currentPerlLabel.Location = new System.Drawing.Point(10, 10);
        currentPerlLabel.Name = "currentPerlLabel";
        currentPerlLabel.Size = new System.Drawing.Size(35, 35);
        currentPerlLabel.TabIndex = 0;
        currentPerlLabel.Font = new Font(Font, FontStyle.Bold);

        Controls.Add(currentPerlLabel);
        Name = "BBUI";
        ResumeLayout(false);
        PerformLayout();
    }

…and I call it simply like this in the constructor: InitializeCurrentPerlLabel().

Now, I’m at the point where I want to automate the creation of various elements. To do so, I will have all the data in a JSON file, will loop over the elements and create them with a build method.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To test, I’ve got a basic incarnation of the method I’ll eventually use to do this, but at this time, I’m just testing a single label. I’ve changed my code to this:

    private void BuildLabel(string name, Label label) {
        var data = Conf[name];
        label = new System.Windows.Forms.Label();

        SuspendLayout();

        label.AutoSize = data["autosize"];
        label.Location = new System.Drawing.Point(10, 10);
        label.Name = name;
        label.Size = new System.Drawing.Size(35, 35);
        label.TabIndex = 0;
        label.Font = new Font(Font, FontStyle.Bold);

        Controls.Add(label);
        Name = data["name"];
        ResumeLayout(false);
        PerformLayout();       
    }

…and call it like this: BuildLabel("currentPerlLabel", currentPerlLabel);.

It compiles fine, however, I get a System.NullReferenceException: Object reference not set to an instance of an object. error at runtime.

I’m not a fluent C# coder, so could I please get some help as to how I go about setting up my pre-declared currentPerlLabel Label object (through what I would call a reference) in the manner I’m hoping to?

Stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at BBUI.CurrentPerlLabel_Redraw()
   at BBUI.DrawComponents()
   at BBUI.Form1_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

>Solution :

Simply add a ref keyword on your label. This means thats the references can be changed, as you are creating a new one.

private void BuildLabel(string name, ref Label label) {
        var data = Conf[name];
        label = new System.Windows.Forms.Label();

        SuspendLayout();

        label.AutoSize = data["autosize"];
        label.Location = new System.Drawing.Point(10, 10);
        label.Name = name;
        label.Size = new System.Drawing.Size(35, 35);
        label.TabIndex = 0;
        label.Font = new Font(Font, FontStyle.Bold);

        Controls.Add(label);
        Name = data["name"];
        ResumeLayout(false);
        PerformLayout();       
    }

Usage :

BuildLabel("currentPerlLabel", ref currentPerlLabel);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading