Good way to ensure that a property on a UserControl gets set?


If our UserControl requires certain properties are set for it to be useful, what's the best way to ensure they are set? Even if just as a note to self and/or close colleagues.

We've tried using an Initialize method, but that's already caught us out because we forgot to update the Initialize in one instance, having slightly changed the design.

The other idea is to use a sensible default, so that if we forget to set it then there's at least something useful there. But in this case there isn't a useful default. I really want a <strong>strong note to self</strong> to remind me there's something here that needs to be set every time I create an instance of the control.


Nothing you could enforce at the designer level, there is no "I'm done designing" trigger. But certainly at runtime, just throw an exception at the earliest possible moment:

protected override void OnCreateControl(EventArgs e) { if (!DesignMode && !mumbleAssigned) throw new InvalidOperationException("Oops, Mumble property wasn't set"); base.OnCreateControl(e); }

You could make it more subtle if you implement the OnPaint() method. And simply e.Graphics.DrawText a reminder. Call Invalidate() in the property setter. This works at design time.


Nothing screams Must Be Implemented like an Exception. If there's a property that has critical importance, I always just make sure I know about it when it comes to running the code:

public object ImportantProperty { get { if (!this.DesignMode && _superImportantProperty == null) { throw new Exception("The property ImportantProperty must be set."); } } }

Of course, this could be a bit extreme if you could get away with a default property value. Always choose a graceful course of action over a catastrophic course of action when possible.


  • Help me to avoid multiple inheritance aka. help me to get proper oo design
  • How is FTP supposed to work with QNetworkAccessManager?
  • Can I include a Generic type parameter in a lamba expression? (VB.NET 2010)
  • How to merge cells and set value at the same time by using Apache POI?
  • Enforce an Edge to connect two distinct vertex classes
  • How do we specify the client area size of a form in design mode?
  • add scrollbar in qt window?
  • Mule ESB connecting to RabbitMQ
  • UICollectionview scroll horizontal and vertical
  • Is there a greater chance to collide when comparing GUIDs based on a hash vs \"Guid.NewGuid()?
  • Is it good to have multiple database running in a same project?
  • html/css: how to create a hexagonal image-placeholder
  • Google Cloud SQL and time zones
  • How to add a resource file to Visual Studios so that it's visible in “Select Resource” dialog?
  • wordpress query - next two events by metadata date
  • When is locking on types a good idea?
  • How to work with Master Page that is attached to the page via the page's basepage?
  • force json_encode to create strings
  • JavaScript IE rotation transform maths
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Redux, normalised entities and lodash merge
  • How to make Safari send if-modified-since header?
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • How to pass list parameters for each object using Spring MVC?
  • Comma separated Values
  • Android Studio and gradle
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • JaxB to read class hierarchy
  • How to get NHibernate ISession to cache entity not retrieved by primary key