Sunday, March 04, 2007

Save Your Fingers - Let Your Snippets Do The Typing

Snippets are a really cool tool for creating commonly used code blocks or structures that can't be re-used through class libraries/sub-procedures etc. because the definition or layout is similar but not the content of the code. Generally snippets can save you a whole lot of typing. If you are not familiar with Visual Studio 2005 snippets, we suggest you Google them for more information.

I've created some snippets for code I commonly write and posted them in zip file that can be downloaded from Yortsoft website.

These snippets include;
  • dispobj - Expands to create a class definition for an object that correctly implements the dispoable pattern, including the IDisposable interface, suppression of finalisation if disposed prior to the finaliser being called by the GC, use of isDisposing in a common private method to allow only non-managed resources to be disposed when called from the finaliser and an IsDisposed property.
  • propctlapp - Creates an 'appearance property' for use in a (user or custom) control class. This property comes with XML documentation, the category attribute applied with a setting of 'Appearance', the set accessor contains an if condition for checking to see if the value actually changed before calling the Invalidate method (and any other user code you insert), the description attribute (value set by snippet variable) and the browesable attribute set to true.
  • eventdec - Declares an event using the generic EventHandler delegate and creates a protected virtual OnEventName for raising the event. Also includes XML documentation for the event and method created.
  • invokeself - Creates a method, for use within a form or control class, which invokes itself if this.InvokeRequired is true, or executes the required code if this.InvokeRequired is false.
  • memberregions - Creates the following 'regions' in the current code module; Fields, Events, Constructors/Destructors, Public Methods, Public Properties, Overrides, Private Methods, Event Handlers. This is really a personal preference I have for formatting my own code, but I've seen other people on the 'Net doing the same so I thought someone else might like this snippet too.
  • propc - Creates a property with both get and set accessors, with an if (oldvalue != value) condition in the set accessor. Also includes XML documentation for the property created.

I'll add more snippets as I think of them. Let me know what you think of these ones, or if you have an improvement to suggest.

No comments:

Post a comment