C# assignment results in null reference

Posted on 2007-07-29
Last Modified: 2013-11-07
Here is a portion of a User Control class, UserControlTask:

    public interface ITaskItem
        void Run();
        bool Check();
        string buttonText { get; }
        string itemID { get; }
        Form1 daddy { get; set; }
        ProgressBar pb { get; set; }

    public partial class UserControlTask<T> : UserControl where T : ITaskItem, new()
        T t;
        public delegate void Run_();
        public Run_ Run;
        public delegate bool Check_();
        public Check_ Check;

        public string itemID;

        private Form1 daddy;

        public UserControlTask(Form1 sender, int y)
            Top = y;
            t = new T();
            itemID = t.itemID;

            daddy = sender;

            t.daddy = sender;        /* <- problem here */
            t.pb = this.progressBarTask;    /* <- problem here */

            Run = new Run_(t.Run);
            Check = new Check_(t.Check);
            buttonTask.Text = t.buttonText;

progressBarTask is a Progress Bar in the user control. You can see that I instantiate an object of type T and use accessors to save references to my main form, and to progressBarTask in the user control. However, instead, both t.daddy and t.pb evaluate to null after the assignment (where "problem here" is commented). The object is derived from a class Task, which contains the accessors:

    abstract class Task : ITaskItem
        private Form1 daddy_;
        private ProgressBar pb_;
        public SqlConnection conn = new SqlConnection("server=x;user=x;password=x");

        public Task()

        protected string itemID_;
        protected string buttonText_;

        public Form1 daddy
            get { return daddy_; }
            set { daddy_ = daddy; }

        public ProgressBar pb
            get { return pb_; }
            set { pb_ = pb; }

        public string itemID
            get { return itemID_; }

        public string buttonText
            get { return buttonText_; }

        public abstract void Run();
        public abstract bool Check();

...SO... why is it that I can't make that assignment?
Question by:tbragsda
    LVL 6

    Accepted Solution

    You must write the getter like this:
     public Form1 daddy
                get { return daddy_; }
                set { daddy_ = value; }  //<-- must be 'value'

            public ProgressBar pb
                get { return pb_; }
                set { pb_ = value; } //<-- must be 'value'
    LVL 1

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now