none
WinForms Class Design RRS feed

  • Question

  • Hi,
    I am learning OOP and trying to apply it to my C# WinForms applications.
    So I have a Form with 2 textboxes. One to enter professor. Another to enter lecturer.

    I have created an abstract 'Staff' class. Then I have a Professor class and a Lecturer class. Both inherit from Staff.
    In Staff, I have declared an abstract property StaffName. So this must be implemented in other classes. Am I correct so far?

    These classes are called on Button_Click event...

    Professor professor = new Professor();            professor.StaffName = staffTextBox1.Text;            Lecturer lecturer = new Lecturer();            lecturer.StaffName = staffTextBox2.Text;            MessageBox.Show(string.Format("Analyst {0}\nSupervisor {1}", professor.StaffName, lecturer.StaffName), "Output", MessageBoxButtons.OK);

    And my classes...

        public abstract class Staff
        {
            protected string staffName;
            public abstract string StaffName
            {
                get;
                set;
            }
        }
    
        public class Professor : Staff
        {
            public override string StaffName
            {
                get
                {
                    return staffName;
                }
                set
                {
                    staffName = value;
                }
            }
        }
    
        public class Lecturer : Staff
        {
            public override string StaffName
            {
                get
                {
                    return staffName;
                }
                set
                {
                    staffName = value;
                }
            }
        }

    Is this a correct OOP technique in WinForms or is there issues with my code design?

    Thanks for your help!!

    Wednesday, November 21, 2012 5:45 PM

Answers

  • Hi!

    I don't think there is need to override the StaffName property, the idea of inheritance is to reuse code and in this case you are overriding the same property in both subclases, there is no need for that in this case , you can refrence the StaffName from the base class from which they inherit.

    Regards!

    Wednesday, November 21, 2012 5:52 PM
  • As the StaffName property will apply in the same way to both Professors and Lecturers, it properly should be implemented in the base class Staff.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Wednesday, November 21, 2012 5:50 PM
  • This is only a portion of an example.  You haven't really shown  how you're using these class to really *do* anything, so we can't comment on how effective they are at doing it.  So far all you've done is display the textboxes' text in a message box; you don't need any custom classes at all to do that.

    Oh, and when entering you code please don't squish it all down to one line...

    • Edited by servy42 Wednesday, November 21, 2012 6:14 PM
    • Marked as answer by Bob ShenModerator Tuesday, December 4, 2012 9:24 AM
    Wednesday, November 21, 2012 6:13 PM

All replies

  • As the StaffName property will apply in the same way to both Professors and Lecturers, it properly should be implemented in the base class Staff.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Wednesday, November 21, 2012 5:50 PM
  • Hi!

    I don't think there is need to override the StaffName property, the idea of inheritance is to reuse code and in this case you are overriding the same property in both subclases, there is no need for that in this case , you can refrence the StaffName from the base class from which they inherit.

    Regards!

    Wednesday, November 21, 2012 5:52 PM
  • This is only a portion of an example.  You haven't really shown  how you're using these class to really *do* anything, so we can't comment on how effective they are at doing it.  So far all you've done is display the textboxes' text in a message box; you don't need any custom classes at all to do that.

    Oh, and when entering you code please don't squish it all down to one line...

    • Edited by servy42 Wednesday, November 21, 2012 6:14 PM
    • Marked as answer by Bob ShenModerator Tuesday, December 4, 2012 9:24 AM
    Wednesday, November 21, 2012 6:13 PM