Note that there are some explanatory texts on larger screens.

plurals
  1. PO'UserControl' constructor with parameters in C#
    primarykey
    data
    text
    <p>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:</p> <ol> <li>I know that when an object is constructed (without error/exception), my object is good.</li> <li>It helps avoid forgetting to set a certain property.</li> </ol> <p>This mindset is starting to hurt me in regards to form/usercontrol development. Imagine this <code>UserControl</code>:</p> <pre><code>public partial class MyUserControl : UserControl { public MyUserControl(int parm1, string parm2) { // We'll do something with the parms, I promise InitializeComponent(); } } </code></pre> <p>At designtime, if I drop this <code>UserControl</code> on a form, I get an <code>Exception</code>:</p> <blockquote> <p>Failed to create component 'MyUserControl' ...<br> System.MissingMethodException - No parameterless constructor defined for this object.</p> </blockquote> <p>It seems like, to me, the only way around that was to add the default constructor (unless someone else knows a way).</p> <pre><code>public partial class MyUserControl : UserControl { public MyUserControl() { InitializeComponent(); } public MyUserControl(int parm1, string parm2) { // We'll do something with the parms, I promise InitializeComponent(); } } </code></pre> <p>The whole point of not including the parameterless constructor was to avoid using it. And I can't even use the <code>DesignMode</code> property to do something like:</p> <pre><code>public partial class MyUserControl : UserControl { public MyUserControl() { if (this.DesignMode) { InitializeComponent(); return; } throw new Exception("Use constructor with parameters"); } } </code></pre> <p>This doesn't work either:</p> <pre><code>if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) </code></pre> <p>Fine, moving along ...</p> <p>I have my parameterless constructor, I can drop it on the form, and the form's <code>InitializeComponent</code> will look like this:</p> <pre><code>private void InitializeComponent() { this.myControl1 = new MyControl(); // blah, blah } </code></pre> <p>And trust me, because I did it (yes, ignoring the comments Visual Studio generated), I tried messing around and I passed parameters to <code>InitializeComponent</code> so that I could pass them to the constructor of <code>MyControl</code>.</p> <p>Which leads me to this:</p> <pre><code>public MyForm() { InitializeComponent(); // Constructed once with no parameters // Constructed a second time, what I really want this.myControl1 = new MyControl(anInt, aString); } </code></pre> <p>For me to use a <code>UserControl</code> with parameters to the constructor, I have to add a second constructor that I don't need? And instantiate the control twice?</p> <p>I feel like I must be doing something wrong. Thoughts? Opinions? Assurance (hopefully)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload