C# assignment results in null reference

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)
        {
            InitializeComponent();
            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;
            loadTaskParams();
        }

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?
LVL 1
tbragsdaAsked:
Who is Participating?
 
thuannguyConnect With a Mentor Commented:
Hi,
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'
        }
0
 
tbragsdaAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.