C# code snippets for using UpdateControls

Developer
Jan 20, 2012 at 8:07 PM
Edited Jan 20, 2012 at 8:11 PM

I made some code snippets for defining dependents and independents faster. Save the snippet files in a folder and then use Visual Studio's Code Snippets Manager to include that folder in snippet lookup (it's in the Tools menu). You can also dump the snippets in the default folder, e.g. c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

Typical usage: type "ind" and press Tab twice to make an independent, or "dep" and Tab to make a dependent. Type the data type, press tab, then type the desired property name.

Coordinator
Jan 20, 2012 at 9:36 PM

Sweet! I'll take a look this weekend. I'd like to get these and your two-way list binding published to NuGet, if I can figure out how to add snippets from a package. At the very least, I can add them to the MSI.

Developer
Jan 24, 2012 at 12:27 AM

Of course, it's conventional to define your Dependents in the constructor, but a code snippet only inserts code in one place, so the dependents in the snippet are auto-defined on the first property call. Another small irritation is that if you don't already have a "using" statement for UpdateControls.Fields, the code snippet somehow blocks the little "tooltip menu", the three-pixel-high widget that offers to insert the using statement. So you have to assign a data type and name first, then press Enter to exit "snippet editing mode".

The depList snippet includes a "name" argument, but I forgot to actually define an overload of DependentList<T> that includes a name. IndependentList<T> doesn't accept a name either, but the snippet doesn't include a name so it works. Feel free to fix this situation. Actually, I'm thinking that the "_name" field of NamedIndependent and NamedDependent (along with various constructor args, Dependent.New and Independent.New) should be changed to type object, so that arbitrary metadata can be assigned; this adds flexibility without increasing the minimum object size.

Developer
Jan 24, 2012 at 12:53 AM

This somehow reminds me, I originally defined three constructor overloads of Independent<T>: (string name), (T value) and (string name, T value). But I had to delete the plain (string) overload because it turned out to be ambiguous in case T is string! Worse, the compiler doesn't complain and automatically picks the (string) overload, which would change the meaning of code that originally used the (T) overload... now we're left with two overloads, (T value) and (string name, T value). Since the name is optional, it may seem more logical to offer a (T value, string name) overload instead; but the name comes first for consistency with Dependent<T>, where I decided it was better that the name come first, because having it come second looks a little clumsy if the first argument is a multi-line lambda function.

Coordinator
Jan 24, 2012 at 3:09 PM

Too bad about the limitations of snippets. But at least it offers a bit of guidance. People can move the code to the constructor if they decide to do so.

I'll get these into the NuGet package, along with your other changes.