?
Solved

C# assignment results in null reference

Posted on 2007-07-29
2
Medium Priority
?
354 Views
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)
        {
            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?
0
Comment
Question by:tbragsda
2 Comments
 
LVL 6

Accepted Solution

by:
thuannguy earned 500 total points
ID: 19587684
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
 
LVL 1

Author Comment

by:tbragsda
ID: 19588574
Thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

850 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