none
Control Parent Class Properties From Derived Class RRS feed

  • Question

  • Hi,

    I have BaseClass with some properties, that take values after creating its object in Form2 class(Windows Form).

    BaseClass baseClass = new BaseClass(button1, button2, button3, button4, SongName,
                                                    FolderPath2, FileList, Points, label3, timer1, player1);

    Here is BaseClass

    class BaseClass {
            protected Button Button1 { get; set; }
            protected Button Button2 { get; set; }
            protected Button Button3 { get; set; }
            protected Button Button4 { get; set; }
            protected string SongName { get; set; }
            protected string FolderPath2 { get; set; }
            protected List<string> FileList { get; set; }
            protected WindowsMediaPlayer Player { get; set; }
            protected Label Label2 { get; set; }
            protected Label Label3 { get; set; }        
            protected Timer Timer1 { get; set; }
            protected int Points { get; set; }
    
            protected Random rnd = new Random();
    
            public BaseClass() { }
            public BaseClass(Button button1, Button button2, Button button3, Button button4,
                             string sName, string fPath, List<string> flist, 
                             int points, Label label, Timer timer, WindowsMediaPlayer player) {
    
                Button1 = button1;
                Button2 = button2;
                Button3 = button3;
                Button4 = button4;
                SongName = sName;
                FolderPath2 = fPath;
                FileList = flist;
                Player = player;
                Points = points;
                Label3 = label;
                Timer1 = timer;
            }


    Everything is fine here. Properties get the values without problem but when I try to manipulate on them from a derived class e.g. ButtonControl class

    public void EnableButtons() {            
                Button1.Enabled = true;
                Button2.Enabled = true;
                Button3.Enabled = true;
                Button4.Enabled = true;
            }

    I get this:

    I've found out that Button1 gets null right after I call DisableButtons() method from Form2

    ButtonControl butCont = new ButtonControl();
    butCont.DisableButtons();

    So As I guess Problem is that with ButtonControl butCont = new ButtonControl() I create completely new object and that's why its properties are null. 

    How can I fix this?

    Sunday, January 19, 2014 9:49 PM

Answers

  • As the default constructor of the base class doesn't initalize the Button properties, you need to use the other one if you don't initailize the Button properties in the constructor of the derived class:

    public ButtonControl : BaseClass
    {
    public ButtonControl(Button button1, Button button2, Button button3, Button button4,
                             string sName, string fPath, List<string> flist, 
                             int points, Label label, Timer timer, WindowsMediaPlayer player)
    :base(button1,button2,button3,button4,sName,fPath,flist,points,label,timer,player)
    {}
    }
    

    • Marked as answer by mishonacro Monday, January 20, 2014 1:41 PM
    Sunday, January 19, 2014 10:03 PM