'UserControl' constructor with parameters in C#

Call me crazy, but I'm the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the properties are required to actually construct the object, they should go in the constructor. I get two advantages:

  • I know that when an object is constructed (without error/exception), my object is good.
  • It helps avoid forgetting to set a certain property.
  • This mindset is starting to hurt me in regards to form/usercontrol development. Imagine this UserControl :

    public partial class MyUserControl : UserControl
    {
      public MyUserControl(int parm1, string parm2)
      {
        // We'll do something with the parms, I promise
        InitializeComponent();
      }
    }
    

    At designtime, if I drop this UserControl on a form, I get an Exception :

    Failed to create component 'MyUserControl' ...
    System.MissingMethodException - No parameterless constructor defined for this object.

    It seems like, to me, the only way around that was to add the default constructor (unless someone else knows a way).

    public partial class MyUserControl : UserControl
    {
      public MyUserControl()
      {
        InitializeComponent();
      }
    
      public MyUserControl(int parm1, string parm2)
      {
        // We'll do something with the parms, I promise
        InitializeComponent();
      }
    }
    

    The whole point of not including the parameterless constructor was to avoid using it. And I can't even use the DesignMode property to do something like:

    public partial class MyUserControl : UserControl
    {
      public MyUserControl()
      {
        if (this.DesignMode)
        {
          InitializeComponent();
          return;
        }
    
        throw new Exception("Use constructor with parameters");
      }
    }
    

    This doesn't work either:

    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
    

    Fine, moving along ...

    I have my parameterless constructor, I can drop it on the form, and the form's InitializeComponent will look like this:

    private void InitializeComponent()
    {
      this.myControl1 = new MyControl();
    
      // blah, blah
    }
    

    And trust me, because I did it (yes, ignoring the comments Visual Studio generated), I tried messing around and I passed parameters to InitializeComponent so that I could pass them to the constructor of MyControl .

    Which leads me to this:

    public MyForm()
    {
      InitializeComponent(); // Constructed once with no parameters
    
      // Constructed a second time, what I really want
      this.myControl1 = new MyControl(anInt, aString);  
    }
    

    For me to use a UserControl with parameters to the constructor, I have to add a second constructor that I don't need? And instantiate the control twice?

    I feel like I must be doing something wrong. Thoughts? Opinions? Assurance (hopefully)?


    Design decisions made regarding the way Windows Forms works more or less preclude parameterized .ctors for windows forms components. You can use them, but when you do you're stepping outside the generally approved mechanisms. Rather, Windows Forms prefers initialization of values via properties. This is a valid design technique, if not widely used.

    This has some benefits, though.

  • Ease of use for clients. Client code doesn't need to track down a bunch of data, it can immediately create something and just see it with sensible (if uninteresting) results.
  • Ease of use for the designer. Designer code is clearer and easier to parse in general.
  • Discourages unusual data dependencies within a single component. (Though even microsoft blew this one with the SplitContainer )
  • There's a lot of support in forms for working properly with the designer in this technique also. Things like DefaultValueAttribute , DesignerSerializationVisibilityAttribute , and BrowsableAttribute give you the opportunity to provide a rich client experience with minimal effort.

    (This isn't the only compromise that was made for client experience in windows forms. Abstract base class components can get hairy too.)

    I'd suggest sticking with a parameterless constructor and working within the windows forms design principles. If there are real preconditions that your UserControl must enforce, encapsulate them in another class and then assign an instance of that class to your control via a property. This will give a bit better separation of concern as well.


    There are two competing paradigms for designing classes:

  • Use parameterless constructors and set a bunch of properties afterwards
  • Use parameterized constructors to set properties in the constructor
  • The Visual Studio Windows Forms Designer forces you to provide a parameterless constuctor on controls in order to work properly. Actually, it only requires a parameterless constructor in order to instantiate controls, but not to design them (the designer will actually parse the InitializeComponent method while designing a control). This means that you can use the designer to design a form or user control without a parameterless constructor, but you cannot design another control to use that control because the designer will fail to instantiate it.

    If you don't intend to programmatically instantiate your controls (ie build your UI "by hand"), then don't worry about creating parameterized constructors, since they won't be used. Even if you are going to programmatically instantiate your controls, you may want to provide a parameterless constructor so they can still be used in the designer if need be.

    Regardless of which paradigm you use, it is also generally a good idea to put lengthy initialization code in the OnLoad() method, especially since the DesignMode property will work at load time, but not work in the constructor.


    I would recommend

    public partial class MyUserControl : UserControl
    {
        private int _parm1;
        private string _parm2;
    
        private MyUserControl()
        {
            InitializeComponent();
        }
    
        public MyUserControl(int parm1, string parm2) : this()
        {
            _parm1 = parm1;
            _parm2 = parm2;
        }
    }
    

    As this way the base constructor is always called first and any references to components are valid.

    You could then overload the public ctor if need be, ensuring the control is always instantiated with the correct values.

    Either way, you ensure that the parameterless ctor is never called.

    I haven't tested this so if it falls over I apologise!

    链接地址: http://www.djcxy.com/p/61990.html

    上一篇: C#中UserControl中的Text属性

    下一篇: '用户控件'构造函数的参数在C#中