<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://interactiveasp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Phil Gilmore</title><link>http://interactiveasp.net/blogs/spgilmore/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>Using Generics in Delphi</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/12/23/using-generics-in-delphi.aspx</link><pubDate>Wed, 23 Dec 2009 22:55:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:5304</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=5304</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/12/23/using-generics-in-delphi.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/GenericsLogoBracketsWhite_5F00_2F3B5FF2.png"&gt;&lt;img title="GenericsLogoBracketsWhite" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="136" alt="GenericsLogoBracketsWhite" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/GenericsLogoBracketsWhite_5F00_thumb_5F00_5EF90871.png" width="100" align="left" border="0" /&gt;&lt;/a&gt; Phil Gilmore&lt;/p&gt;  &lt;p&gt;12/23/2009, revised 02/25/2010&lt;/p&gt;  &lt;h2&gt;Introduction&lt;/h2&gt;  &lt;p&gt;Generics were introduced in Delphi in version 2009.&amp;#160; They were implemented as first class citizens in the Object Pascal language.&amp;#160; They work splendidly and support all the features you would expect.&amp;#160; This is a huge improvement for this struggling language.&amp;#160; If you are in the majority of Delphi users who refuse to move past Delphi 7, you now have plenty of reason to upgrade.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What’s so great about generics?&lt;/h2&gt;  &lt;p&gt;Once you’ve written a class using generic type parameters (generics), you can use that class with any type and the type you choose to use with any given use of that class replaces the generic types you used when you created the class.&amp;#160; The chosen type now appears as your method parameters, return types and property types.&amp;#160; You don’t have to cast or convert the types… it’s as if the class was written with that type in mind all along.&lt;/p&gt;  &lt;p&gt;For example, most of us have used the TObjectList.&amp;#160; You can use it by itself and just stick objects in it and pull them out again.&amp;#160; But that requires you to cast the objects from TObject back out to TWhateverMyClassNameIs every time you fetch them.&amp;#160; You can also write a descendant class from TObjectList and override the TObject members with your custom type.&amp;#160; But then the class only works with your type.&lt;/p&gt;  &lt;p&gt;Imagine using a generic TObjectList.&amp;#160; TObjectList&amp;lt;TMyCustomType&amp;gt; and its methods accept parameters of type TMyCustomType and its collection yields TMyCustomType objects instead of TObject.&amp;#160; And you never have to write code to make it aware of TMyCustomType.&amp;#160; That’s what generics can do for you.&amp;#160; &lt;/p&gt;  &lt;p&gt;This is done by building the class with a placeholder for a runtime type.&amp;#160; At runtime, you provide that type to the class when you declare it.&amp;#160; Then that type replaces your placeholder throughout the class when you instantiate an object from it.&lt;/p&gt;  &lt;h2&gt;First look&lt;/h2&gt;  &lt;p&gt;Syntactically, you’ll initially be surprised how much Delphi generics resemble C# generics.&amp;#160; There are few differences and they’re similar enough that you won’t have any trouble switching gears between the two.&amp;#160; Let’s look at a generic class.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;unit&lt;/span&gt;&lt;span class="pas1-space"&gt; GenericSample1;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;interface

type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TGenericArray&amp;lt;T&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;array&lt;/span&gt;&lt;span class="pas1-space"&gt; &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;of&lt;/span&gt;&lt;span class="pas1-space"&gt; T;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyData&amp;lt;T&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; Add(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aExistingSet: TGenericArray&amp;lt;T&amp;gt;;
      &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aNewValue: T): TGenericArray&amp;lt;T&amp;gt;;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;implementation

&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TMyData&amp;lt;T&amp;gt; }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; TMyData&amp;lt;T&amp;gt;.Add(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aExistingSet: TGenericArray&amp;lt;T&amp;gt;;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aNewValue: T): TGenericArray&amp;lt;T&amp;gt;;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;{ You could append the new &lt;br /&gt;  value to the existing set and return it here. }
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;.
&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;A class can have a type parameter (usually named T when only one parameter is present) as shown above.&amp;#160; You can also have a parameterized method like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;unit&lt;/span&gt;&lt;span class="pas1-space"&gt; ParameterizedMethodExample1;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;interface

type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyData&amp;lt;T&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; ParameterizedMethod&amp;lt;T2&amp;gt;(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aGenericParameter: T2);
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;implementation


&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TMyData&amp;lt;T&amp;gt; }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; TMyData&amp;lt;T&amp;gt;.ParameterizedMethod&amp;lt;T2&amp;gt;(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aGenericParameter: T2);
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// Do something with T2.
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;.

&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can have multiple parameters:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;unit&lt;/span&gt;&lt;span class="pas1-space"&gt; MultipleParametersExample1;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;interface

type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyData&amp;lt;T, T2&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public

&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;implementation


end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Generic Constraints&lt;/h2&gt;

&lt;p&gt;Given that the type is unknown, it’s sometimes difficult to do much with a member or parameter of a generic type, because an unknown type has unknown members and the compiler won’t let you assume what those members are.&amp;#160; It must know the type.&amp;#160; &lt;/p&gt;

&lt;p&gt;You can assert some assumptions using generic type constraints.&amp;#160; This is where we first deviate from C# syntax.&amp;#160; In C#, your constraints are declared at the end of the class or method declaration, prefaced with the &lt;em&gt;where&lt;/em&gt; keyword.&amp;#160; Here are some C# constraint declarations.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;public class MyData&amp;lt;T&amp;gt; where T: class { }&lt;/p&gt;

  &lt;p&gt;public class MyData&amp;lt;T&amp;gt; where T: class, IMydata { }&lt;/p&gt;

  &lt;p&gt;public class MyData&amp;lt;T&amp;gt; where T: class, IMydata, new { }&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It almost looks a little Pascal-ish, doesn’t it?&amp;#160; Instead of a suffix of the declaration, Delphi neatly declares constraints inline as if it were a type, in traditional Pascal fashion.&amp;#160; Here are the equivalent Delphi declarations.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;unit&lt;/span&gt;&lt;span class="pas1-space"&gt; ConstraintsExample;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;interface

type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  IMyInterface = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;interface&lt;/span&gt;&lt;span class="pas1-space"&gt; [&lt;/span&gt;&lt;span class="pas1-string"&gt;'{78B5F879-BE4A-436A-B4FB-96A5329B3386}'&lt;/span&gt;&lt;span class="pas1-symbol"&gt;]
    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; GetText: &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;string&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyConcreteClass = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TInterfacedObject, IMyInterface)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; GetText: &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;string&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyData1&amp;lt;T: &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; WhatCanItDo(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParm: T);
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyData2&amp;lt;T: TMyConcreteClass, IMyInterface&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; WhatCanItDo(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParm: T);
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TMyData3&amp;lt;T: &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;, IMyInterface, &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;constructor&lt;/span&gt;&lt;span class="pas1-symbol"&gt;&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; WhatCanItDo(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParm: T): T;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;implementation


&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TMyData1&amp;lt;T&amp;gt; }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; TMyData1&amp;lt;T&amp;gt;.WhatCanItDo(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParm: T);
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;var
&lt;/span&gt;&lt;span class="pas1-space"&gt;  p: pointer;
  obj: TObject;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// Here, &amp;lt;T: class&amp;gt; tells us it descends from TObject
&lt;/span&gt;&lt;span class="pas1-space"&gt;  obj := TObject(aParm);

  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// We also know that it is a reference type.
&lt;/span&gt;&lt;span class="pas1-space"&gt;  p := Pointer(TObject(aParm));
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TMyData2&amp;lt;T&amp;gt; }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; TMyData2&amp;lt;T&amp;gt;.WhatCanItDo(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParm: T);
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;var
&lt;/span&gt;&lt;span class="pas1-space"&gt;  intf: IMyInterface;
  conc: TMyConcreteClass;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// Here, &amp;lt;T: TMyConcreteClass, IMyInterface&amp;gt;
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// tells us T descends from TMyConcreteClass,
&lt;/span&gt;&lt;span class="pas1-space"&gt;  conc := TMyConcreteClass(aParm);

  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// and that T implements IMyInterface.
&lt;/span&gt;&lt;span class="pas1-space"&gt;  intf := TMyConcreteClass(aParm) &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;as&lt;/span&gt;&lt;span class="pas1-space"&gt; IMyInterface;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TMyData3&amp;lt;T&amp;gt; }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; TMyData3&amp;lt;T&amp;gt;.WhatCanItDo(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParm: T): T;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// Here, &amp;lt;T: class, IMyInterface, constructor&amp;gt; tells us that
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// the T type has a default (parameterless) constructor
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// and we can create an instance of that type.
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// We also know that any instance we create implements
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-comment"&gt;// the IMyInterface interface.

&lt;/span&gt;&lt;span class="pas1-space"&gt;  result := T.Create&lt;/span&gt;&lt;span class="pas1-space"&gt;;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TMyConcreteClass }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; TMyConcreteClass.GetText: &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;string&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  result := &lt;/span&gt;&lt;span class="pas1-string"&gt;'Sample text.'&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;.
&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;In the code above, I show constraints using the &lt;em&gt;class&lt;/em&gt;, &lt;em&gt;interface &lt;/em&gt;and &lt;em&gt;constructor &lt;/em&gt;elements.&amp;#160; Constraints may have a base class instead of the &lt;em&gt;class &lt;/em&gt;element, indicating that the type must descend from the base class indicated.&amp;#160; You may also use the &lt;em&gt;record &lt;/em&gt;element to indicate that the type is a value type or record type.&amp;#160; &lt;/p&gt;

&lt;p&gt;Given the constraints in the examples above, TMyData1 must receive a class for its type parameter.&amp;#160; It cannot be a primitive or a record.&amp;#160; TMyData2 and TMyData3 must receive a class that is or descends from TMyConcreteClass.&amp;#160; Any primitive, record or incompatible class type will generate a compiler error.&amp;#160;&amp;#160; &lt;/p&gt;

&lt;p&gt;Here are example declarations to consume these classes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;var
&lt;/span&gt;&lt;span class="pas1-space"&gt;  data1: TMyData1&amp;lt;TObject&amp;gt;;
  data2: TMyData2&amp;lt;TMyConcreteClass&amp;gt;;
  data3: TMyData3&amp;lt;TMyConcreteClass&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here are some declarations that violate our constraints.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;blockquote&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;var
&lt;/span&gt;&lt;span class="pas1-space"&gt;  mustBeObjectData: TMyData1&amp;lt;TMyRecord&amp;gt;;
  mustBeConcreteType: TMyData2&amp;lt;TObject&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;/blockquote&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here are the errors our violations will raise at compile time.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 8pt courier new"&gt;&lt;span class="pas1-space"&gt;[DCC Error] Unit1.pas(48): E2511 
Type parameter ‘T’ must be a class type
[DCC Error] Unit1.pas(49): E2515 
Type parameter ‘T’ is not compatible with type ‘TMyConcreateClass’
[DCC Fatal Error] Project1.dpr(6): F2063 
could not compile used unit ‘Unit2.pas’&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The first is because we pass a record where the constraint demands a class, and the second is because we pass a TObject where the constraint demands a class the descends from TMyConcreteClass, which TObject does not do.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Multiple parameter syntaxes&lt;/h2&gt;

&lt;p&gt;When using multiple type parameters, syntax has some strange complications.&amp;#160; So far, we’ve looked at the syntax for a single type Parameter.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 8pt courier new"&gt;&lt;span class="pas1-space"&gt;public
  function Method1&amp;lt;T&amp;gt;: T;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The syntax for multiple parameters is a comma-separated list of type parameters.&amp;#160; You can optionally use a semicolon to separate the methods as well.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 8pt courier new"&gt;&lt;span class="pas1-space"&gt;public
  function Method2&amp;lt;T1, T2&amp;gt;: T2;
  function Method3&amp;lt;T1; T2&amp;gt;: T2;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;However, type parameter constraints will throw a wrench into the mix.&amp;#160; If you add a constraint to any of the type parameters, the comma is no longer allowed and the semicolon is required.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 8pt courier new"&gt;&lt;span class="pas1-space"&gt;public
  function Method4&amp;lt;T1: class; T2&amp;gt;: T2;
  function Method5&amp;lt;T1; T2: IInterface&amp;gt;: T2;
  function Method6&amp;lt;T1: class; T2: IInterface&amp;gt;: T2;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;There is no difference in the implementation section.&amp;#160; A comma-separated list of type parameter names is all that is needed.&amp;#160; However, the the parameter names must be complete and correct, and there is currently a bug in Delphi 2010 where code completion does not generate the implementation declarations properly (only the first parameter is included in the method implementation).&amp;#160; Here is a proper set of declarations for all the examples above:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 8pt courier new"&gt;&lt;span class="pas1-space"&gt;function TMyClass.Method1&amp;lt;T&amp;gt;: T; begin end;
function TMyClass.Method2&amp;lt;T1, T2&amp;gt;: T2; begin end;
function TMyClass.Method3&amp;lt;T1, T2&amp;gt;: T2; begin end;
function TMyClass.Method4&amp;lt;T1, T2&amp;gt;: T2; begin end;
function TMyClass.Method5&amp;lt;T1, T2&amp;gt;: T2; begin end;
function TMyClass.Method6&amp;lt;T1, T2&amp;gt;: T2; begin end;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Scope&lt;/h2&gt;

&lt;p&gt;A type parameter in a class type parameter list is visible anywhere inside that class.&amp;#160; It can be used for method parameters, method return values, field types, property types or method local variables.&lt;/p&gt;

&lt;p&gt;A method can also have a generic type parameter list and those parameter lists also support constraints.&amp;#160; A type parameter in a method type parameter list is visible anywhere inside that method.&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here is a unit which demonstrates various generic parameter scopes.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt courier new"&gt;&lt;span class="pas1-reservedword"&gt;unit&lt;/span&gt;&lt;span class="pas1-space"&gt; ScopeExample;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;interface
uses&lt;/span&gt;&lt;span class="pas1-space"&gt; Classes;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;type
&lt;/span&gt;&lt;span class="pas1-space"&gt;  TScopeExample&amp;lt;T: &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;&amp;gt; = &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;class&lt;/span&gt;&lt;span class="pas1-symbol"&gt;(TObject)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;public
&lt;/span&gt;&lt;span class="pas1-space"&gt;    fGenericField: T;
    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; GenericMethod&amp;lt;T2: TStringList&amp;gt;(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParameter1: T; &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParameter2: T2): T2;
    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; UsedAsParameterType(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParameter: T);
    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; UsedAsMethodReturnType: T;
    &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;property&lt;/span&gt;&lt;span class="pas1-space"&gt; UsedAsPropertyType: T
      &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;read&lt;/span&gt;&lt;span class="pas1-space"&gt; UsedAsMethodReturnType
      &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;write&lt;/span&gt;&lt;span class="pas1-space"&gt; UsedAsParameterType;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;implementation

&lt;/span&gt;&lt;span class="pas1-comment"&gt;{ TScopeExample&amp;lt;T&amp;gt; }

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; TScopeExample&amp;lt;T&amp;gt;.GenericMethod&amp;lt;T2&amp;gt;(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParameter1: T;
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParameter2: T2): T2;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;if&lt;/span&gt;&lt;span class="pas1-space"&gt; (T.ClassInfo = TStringList.ClassInfo) &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;then
&lt;/span&gt;&lt;span class="pas1-space"&gt;    result := T2(aParameter1)
  &lt;/span&gt;&lt;span class="pas1-reservedword"&gt;else
&lt;/span&gt;&lt;span class="pas1-space"&gt;    result := aParameter2;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;function&lt;/span&gt;&lt;span class="pas1-space"&gt; TScopeExample&amp;lt;T&amp;gt;.UsedAsMethodReturnType: T;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;var
&lt;/span&gt;&lt;span class="pas1-space"&gt;  lUsedAsLocalVariable: T;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  result := lUsedAsLocalVariable;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;procedure&lt;/span&gt;&lt;span class="pas1-space"&gt; TScopeExample&amp;lt;T&amp;gt;.UsedAsParameterType(&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;const&lt;/span&gt;&lt;span class="pas1-space"&gt; aParameter: T);
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;var
&lt;/span&gt;&lt;span class="pas1-space"&gt;  lUsedAsLocalVariable: T;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;begin
&lt;/span&gt;&lt;span class="pas1-space"&gt;  lUsedAsLocalVariable := aParameter;
&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;;

&lt;/span&gt;&lt;span class="pas1-reservedword"&gt;end&lt;/span&gt;&lt;span class="pas1-symbol"&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Generics in VCL&lt;/h2&gt;

&lt;p&gt;There is a concept of self-obsolescence behind generics.&amp;#160; Because you now have generics, you may quickly go through your previous patterns and adopt them to use generics.&amp;#160; But once they’re done, you find that they’re so reusable that you need not write any code to accommodate your most common patterns anymore after that.&amp;#160; Well, there’s more news.&amp;#160; Be it good or bad, The VCL has implemented many of those patterns using generics.&amp;#160; So you may not need to make those conversions in the first place.&amp;#160; Let’s explorer some of the generic structures and classes that the VCL now provides.&lt;/p&gt;

&lt;p&gt;While generic declarations are now lightly sprinkled throughout the VCL, the starting point for most generics support is implemented in three basic units.&amp;#160; &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;uses 
    &lt;br /&gt;SysUtils, 

    &lt;br /&gt;Generics.Defaults, 

    &lt;br /&gt;Generics.Collections;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In general, support for iterators and comparers&amp;#160; are declared in Generics.Defaults, and collection structures are declared in Generics.Collections.&lt;/p&gt;

&lt;p&gt;Here are the public declarations in Generics.Defaults.pas:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="4" width="400" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;&lt;strong&gt;Classes / Interfaces&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;IComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Implements a compare function returning less-than, greater-than or equal-to.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;IEqualityComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Implements a compare function returning equal-to or not.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Abstract class for implementing IComparer&amp;lt;T&amp;gt;.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TEqualityComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Abstract class for implementing IEqualityComparer&amp;lt;T&amp;gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TSingletonImplementation&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Weak-reference implementation of TInterfacedObject.&amp;#160; Instances are not automatically freed.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TDelegatedEqualityComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;A comparer that takes a delegated comparison function and uses it to determine equality.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TDelegatedComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;A comparer that takes a delegated function to do the actual comparison.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TCustomComparer&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;An abstract singleton class for comparison operations.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TStringComparer&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Compares strings.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;&lt;strong&gt;Method prototypes / delegates&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TComparison&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;A delegate method prototype for less-than, greater-than, or equal-to comparisons.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;TEqualityComparison&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;A delegate method prototype for equality comparisons.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;THasher&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;A delegate method prototype for getting the hash value of an object or value.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;&lt;strong&gt;Concrete Functions&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;function BobJenkinsHash(const Data; 
        &lt;br /&gt;Len, InitData: Integer): Integer;&lt;/td&gt;

      &lt;td valign="top" width="126"&gt;Returns a hash from the given data.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="272"&gt;function BinaryCompare(const Left, Right: Pointer; 
        &lt;br /&gt;Size: Integer): Integer;&lt;/td&gt;

      &lt;td valign="top" width="158"&gt;A method to compare binary data.&amp;#160; Does not match comparison prototypes, so must be used from a comparison class rather than pass as a delegated method.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here are the public declarations in Generics.Collections.pas:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="4" width="400" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;&lt;strong&gt;Classes / Interfaces&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TArray&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Contains no data, but provides methods for working with arrays.&amp;#160; Strangely, the class is not generic and the members are.&amp;#160; This can easily be confused with TArray&amp;lt;T&amp;gt;, which is in System.pas, which contains data and no methods.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TEnumerator&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Implements the iterator pattern on your data.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TEnumerable&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Returns an enumerator, allowing your class to be used in a For..In construct.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TList&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic collection or objects, records or primitives.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TQueue&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic queue (first in / last out) collection.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TStack&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic stack (first in / first out) collection.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TPair&amp;lt;TKey, TValue&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A structure containing two child members of disparate types.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A searchable collection with lookup keys.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TObjectList&amp;lt;T: class&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic collection of objects (records and primitives not allowed).&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TObjectQueue&amp;lt;T: class&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic queue of objects.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TObjectStack&amp;lt;T: class&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic stack of objects.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TObjectDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;A generic collection of owned objects with lookup keys.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;&lt;strong&gt;Enums&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TCollectionNotification&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Used in TCollectionNotifyEvent&amp;lt;&amp;gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TDictionaryOwnerships&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Used by TObjectDictionary&amp;lt;&amp;gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;&lt;strong&gt;Method prototypes / delegates&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;TCollectionNotifyEvent&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Used by various collections to implement an observer pattern.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;&lt;strong&gt;Concrete functions&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;InCircularRange&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;&lt;strong&gt;Type aliases&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;PObject&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;Pointer to TObject.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;&lt;strong&gt;Exceptions&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="142"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="256"&gt;ENotsupportedException&lt;/td&gt;

      &lt;td valign="top" width="181"&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here are the pertinent declarations in SysUtils.pas:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="4" width="400" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;&lt;strong&gt;Method prototypes / delegates&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TProc&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A procedure with no parameters.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TProc&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A procedure with one generic parameter.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TProc&amp;lt;T1, T2&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A procedure with two generic parameters.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TProc&amp;lt;T1, T2, T3&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A procedure with three generic parameters.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TProc&amp;lt;T1, T2, T3, T4&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A procedure with four generic parameters.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TFunc&amp;lt;TResult&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A function with no parameters and a generic return type.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TFunc&amp;lt;T, TResult&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A function with one generic parameter and a generic return type.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TFunc&amp;lt;T1, T2, TResult&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A function with two generic parameters and a generic return type.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TFunc&amp;lt;T1, T2, T3, TResult&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A function with three generic parameters and a generic return type.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TFunc&amp;lt;T1, T2, T3, T4, TResult&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A function with four generic parameters and a generic return type.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="254"&gt;TPredicate&amp;lt;T&amp;gt;&lt;/td&gt;

      &lt;td valign="top" width="144"&gt;A function with one generic parameter and a boolean return type.&amp;#160; This is used by collections as a “filter” for elements.&amp;#160; Elements that are passed to a predicate function which return false are excluded from the result.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;h2&gt;&amp;#160;&lt;/h2&gt;

&lt;p&gt;The delegate types in SysUtils are interesting if you know how they are used.&amp;#160; They are probably just going in one ear and out the other if you don’t.&amp;#160; These delegates are often the prototypes for anonymous methods.&amp;#160; These are methods that can be declared in place and passed as parameters to other methods, but they are not pointers to methods of another object or class (although they can be).&amp;#160; You can create your own methods that take one of these types as a parameter, call that method and do something with the result.&amp;#160; This is a means of passing some control back to the caller, as they provide the code you’re calling.&amp;#160; I expected these function prototypes to be used as predicates on many of the new generic collection types… but I find that there are not used anywhere.&amp;#160; This is strange, but for now, it simplifies our discussion.&amp;#160; You will not need to know anything about the declarations in SysUtils.pas to use generics in Delphi.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;What is Covariance and Contravariance?&lt;/h2&gt;

&lt;p&gt;Covariance is the ability to implicitly convert a value from a more specific type to an ancestor type.&lt;/p&gt;

&lt;p&gt;Contravariance is the ability to implicitly convert a value from a less specific type to a derived type.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Generic Covariance and Contravariance&lt;/h2&gt;

&lt;p&gt;It is often disappointing to a user who begins using generics to find that covariance and contravariance is not supported in their implementation.&amp;#160; They are not supported in Delphi for native Windows.&amp;#160; Interestingly, they are supported in Prism on the .NET 3.5 framework although they aren’t available in C# until version 3.0 comes out on the .NET 4.0 platform.&lt;/p&gt;

&lt;p&gt;At first it would seem that a method with a parameter of type TList&amp;lt;TStrings&amp;gt; could accept a value of type TList&amp;lt;TStringlist&amp;gt;.&amp;#160; Because TList is compatible with TList and TStrings is an ancestor of TStringList, it would seem that TList&amp;lt;TStringList&amp;gt; derives from TList&amp;lt;TStrings&amp;gt; but it doesn’t.&amp;#160; The type system is actually comparing TList&amp;lt;T1&amp;gt; to TList&amp;lt;T2&amp;gt;, which are incompatible types.&amp;#160; &lt;/p&gt;

&lt;p&gt;The expression TList&amp;lt;T&amp;gt; is not to be thought of as a composition of 2 types with two sets of type information to be compared to another two sets of type information when determining compatibility. TList&amp;lt;T&amp;gt; is a single type with a single set of type information.&amp;#160; Hence, compatibility between T1 and T2 is not applicable…&amp;#160; To make such type comparisons would require covariance and contravariance.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Generics in older versions of Delphi (Templates)&lt;/h2&gt;

&lt;p&gt;Generics were introduced in Delphi 2009.&amp;#160; However, with some effort, you can achieve something similar in older versions.&amp;#160; Delphi can be tricked into building a sort of untyped template with some compromise in readability.&amp;#160; &lt;/p&gt;

&lt;p&gt;The article titled “Templates in Object Pascal” by Rossen Assenov describes how to do this.&amp;#160; The article can currently be found here.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://edn.embarcadero.com/article/27603"&gt;&lt;font color="#0000ff"&gt;http://edn.embarcadero.com/article/27603&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, you write your template in two parts, an interface and an implementation.&amp;#160; Then in the consuming unit, you declare a type alias for your “type parameter” (the type placeholder you put in your template) and then immediately add an include directive for the interface snippet.&amp;#160; In the implementation section you add an include directive for the implementation snippet to finish it up.&amp;#160; This has the limitation that the template can only be consumed once per unit, but that’s a small price to pay for some of the benefits you might reap.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Phil Gilmore (&lt;a href="http://www.interactiveasp.net"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=5304" width="1" height="1"&gt;</description></item><item><title>Getting started with MyGeneration, a primer and tutorial</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/12/03/getting-started-with-mygeneration-a-primer-and-tutorial.aspx</link><pubDate>Fri, 04 Dec 2009 00:36:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:3761</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=3761</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/12/03/getting-started-with-mygeneration-a-primer-and-tutorial.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationLogo2_5F00_4AC0D70B.png"&gt;&lt;img title="MyGenerationLogo2" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="244" alt="MyGenerationLogo2" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationLogo2_5F00_thumb_5F00_0CC7464D.png" width="55" align="left" border="0" /&gt;&lt;/a&gt;12/03/2009&lt;/p&gt;  &lt;p&gt;Phil Gilmore&lt;/p&gt;  &lt;h2&gt;About MyGeneration&lt;/h2&gt;  &lt;p&gt;MyGeneration is a developer's code-generation tool that gives you access to your database’s schema.&amp;#160; It is useful for building O/RM entity classes based on your database schema.&lt;/p&gt;  &lt;p&gt;MyGeneration is mature, free and open-source.&amp;#160; It supports many databases.&amp;#160; It supports scripting in many languages (C# script VB.NET script, jscript, vbscript, etc.).&amp;#160; It supports output into any language.&amp;#160; You can write your own templates or use someone else’s.&amp;#160; &lt;/p&gt;  &lt;p&gt;There are many templates available.&amp;#160; Some come with the installer, but many more can be browsed and downloaded from the internet right in the MyGeneration main window.&lt;/p&gt;  &lt;p&gt;I found it easy enough to get started using someone else's downloadable templates, so I will only take a brief moment to describe that.&amp;#160; I will then spend more time explaining how to write your own templates. &lt;/p&gt;  &lt;p&gt;The MyGeneration main page is here and has the latest download link to download.com.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.mygenerationsoftware.com/portal/default.aspx"&gt;&lt;font color="#800080"&gt;http://www.mygenerationsoftware.com/portal/default.aspx&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As the this writing, the download link points &lt;a href="http://download.cnet.com/windows/net/?tag=mncol%253Bsort&amp;amp;rpp=10&amp;amp;sort=downloadCount+asc" target="_blank"&gt;here&lt;/a&gt;.&amp;#160; This download is an NSIS installer package.&amp;#160; I recommend installing all components.&amp;#160; &lt;/p&gt;  &lt;p&gt;When you first run MyGeneration, you are presented with the Default Settings screen.&amp;#160; The purpose of this window is to give you a place to define your database connection.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/mygeneration_5F00_defaultsettings_5F00_labeled_5F00_2963C220.png"&gt;&lt;img title="mygeneration_defaultsettings_labeled" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="273" alt="mygeneration_defaultsettings_labeled" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/mygeneration_5F00_defaultsettings_5F00_labeled_5F00_thumb_5F00_04D2349C.png" width="605" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Using the OLEDB configuration wizard (1, see image below), you can easily produce a working connection string.&amp;#160; You can also get a generic one &lt;a href="http://www.connectionstrings.com" target="_blank"&gt;here&lt;/a&gt; to get your started if you have trouble.&amp;#160; Or, enter the string manually if you have the aptitude for it.&amp;#160; However you do it, the point is to get the connection string into the connection string field (2).&lt;/p&gt;  &lt;p&gt;Once you test your connection string (3) and can connect, type a name for your connection in the box above (4) and save it (5).&amp;#160; Now you can always refer to this connection string by selecting it in the dropdown list (4) and clicking the load button.&amp;#160; Set the language mapping to your preference (i.e. C#).&amp;#160; The other settings are usually left as default.&amp;#160; Click the Save button at the top of the page and you may continue.&lt;/p&gt;  &lt;h2&gt;MyMeta&lt;/h2&gt;  &lt;p&gt;The MyMeta library is the body that fetches metadata from your database engine.&amp;#160; It’s the part that&amp;#160; must be aware of your particular database engine in order to support it.&amp;#160; You can see what MyMeta can see by opening the MyMeta browser window and viewing its tree contents.&amp;#160; If you don’t see your database schema in the MyMeta browser, your connection isn’t set up correctly or you don’t have permissions to see the things you’re looking for.&amp;#160; In any case, the data won’t be accessible for code generation unless it is visible in the MyMeta browser.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaBrowser1_5F00_1970DE4D.png"&gt;&lt;img title="MyMetaBrowser1" style="border-right: 0px; border-top: 0px; display: inline; margin: 10px; border-left: 0px; border-bottom: 0px" height="158" alt="MyMetaBrowser1" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaBrowser1_5F00_thumb_5F00_0008DB13.png" width="201" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Templates&lt;/h2&gt;  &lt;p&gt;There are two ways to do code generation in MyGeneration.&amp;#160; The first is to use someone else’s template.&amp;#160; The second is to write your own.&amp;#160; Most folks who are using a mainstream O/RM such as NHibernate and a mainstream language such as C# can pick a template that works with both and be done in a few minutes.&amp;#160; Others can write some templates in a few hours for any systems they want to use, as long as MyMeta supports their database engine.&lt;/p&gt;  &lt;h2&gt;Using someone else’s templates&lt;/h2&gt;  &lt;p&gt;In case you want to just get some code generation done and be done with this, I’ve put this here so you don’t have to wade through the rest of this document to get your work done.&lt;/p&gt;  &lt;p&gt;MyGeneration’s installer will give you a handful of templates that you can use.&amp;#160; These are limited and probably don’t have what you want.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGeneration_5F00_TemplateBrowser1_5F00_793C775C.png"&gt;&lt;img title="MyGeneration_TemplateBrowser1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="291" alt="MyGeneration_TemplateBrowser1" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGeneration_5F00_TemplateBrowser1_5F00_thumb_5F00_6DA6BA1D.png" width="241" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;One of the nice things about MyGeneration is that you can browser for more templates online without leaving the application.&amp;#160; Click the globe icon in the template browser to find more templates online…&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGeneration_5F00_OnlineTemplateBrowser1_5F00_49152C99.png"&gt;&lt;img title="MyGeneration_OnlineTemplateBrowser1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="484" alt="MyGeneration_OnlineTemplateBrowser1" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGeneration_5F00_OnlineTemplateBrowser1_5F00_thumb_5F00_443278DD.png" width="152" align="left" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now we’re talking!&amp;#160; You now have the latest templates.&amp;#160; It isn’t always easy to find what you’re looking for, but usually they’re categorized pretty well.&amp;#160; You may look for a template by the language you want to generate, or you may search by the database engine or data access framework you’re using, such as NHibernate.&lt;/p&gt;  &lt;p&gt;Double-click a template to see some pretty good detail about it if you need to know more about it.&amp;#160; When you’ve found a template you like, click the save button and continue for more templates or close the Online Template Library window when you’re done.&lt;/p&gt;  &lt;p&gt;The templates you saved will now appear in your regular Template Browser.&amp;#160; You can right-click on the desired template in the Template Browser, click EXECUTE from the popup menu, and code generation will begin.&lt;/p&gt;  &lt;p&gt;What happens at that point depends on the template.&amp;#160; Many templates will need you to give them some input parameters to do their job.&amp;#160; When this is the case, they will prompt you for parameters with their own popup form.&amp;#160; When it is complete, you will have output in the output window, or the output will be saved to a set of files.&amp;#160;&amp;#160; This is also specific to the template.&amp;#160; Most templates that output to files will prompt you for an output location.&lt;/p&gt;  &lt;p&gt;Modifying someone else’s template&lt;/p&gt;  &lt;p&gt;If you followed the steps above in “Using someone else’s templates”, you know how to get your hands on a template that you can modify.&amp;#160; Take any template in your template browser and double-click it.&amp;#160; The template can be modified right inside the MyGeneration application.&amp;#160; If you’re modifying someone else’s template, I cannot give you very specific direction.&amp;#160; From here on, I will only give direction about how to create a new template.&lt;/p&gt;  &lt;p&gt;Creating your own template&lt;/p&gt;  &lt;p&gt;Relax.&amp;#160; This really is easy.&amp;#160; You can pick from several languages and probably won’t have to learn a new language to do this.&amp;#160; There is code insight, useful error messages and bugs are easy to fix.&lt;/p&gt;  &lt;p&gt;Here some more information about MyGeneration that you will need to know.&amp;#160; First, MyGeneration has several systems.&amp;#160; We’ve already covered MyMeta, MyMeta Browser, Template Browser, and Online Template Browser.&amp;#160; There are separate help files for different systems too.&amp;#160; This is useful to know because you will be using these help files from time to time.&amp;#160; The help files really have good information, but it can be hard to find what you’re looking for, especially if you don’t know which help file to look in.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGeneration_5F00_HelpMenu_5F00_584B9566.png"&gt;&lt;img title="MyGeneration_HelpMenu" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="239" alt="MyGeneration_HelpMenu" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGeneration_5F00_HelpMenu_5F00_thumb_5F00_6CD0E4E4.png" width="213" align="left" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The systems you’re primarily concerned with are Zeus, and MyMeta.&amp;#160; Unless you are using any of the other systems listed under Help.&lt;/p&gt;  &lt;p&gt;Zeus is the scripting engine that MyGeneration uses.&amp;#160; It is the engine that takes your templates, executes them to create a program for code generation and then executes that code generation program and either passes the output back to MyGeneration or outputs it to a file (if the script tells it to).&amp;#160; Zeus can use several languages.&amp;#160; It supports C# Script, VB.Net Script, VBScript and JScript.&amp;#160; I will proceed under the assumption that you are using C# Script.&lt;/p&gt;  &lt;p&gt;MyMeta, as we have discussed, is a set of objects that your Zeus script can ask for when it needs information about your database schema.&amp;#160; There are many object types in this library and you will probably want to just browse over the help file for it to familiarize yourself with some of the interfaces that you will be using.&amp;#160; They are mostly self-evident.&lt;/p&gt;  &lt;p&gt;The Zeus engine has an output object that to which the generated code will be written.&amp;#160; This output would be sent to the Output window by default.&amp;#160; However, many templates will redirect this output to a file.&amp;#160; If that happens, the output may not appear in the output window.&lt;/p&gt;  &lt;p&gt;The Zeus engine has an input object that can be queried for parameters that the user enters are runtime.&amp;#160; Runtime is the time at which the Zeus script is executed.&amp;#160; Using the input object is optional.&lt;/p&gt;  &lt;p&gt;To create a new template, click File, then New –&amp;gt; C# Zeus Template.&amp;#160; The template will appear in a new tab on the main MyGeneration panel.&amp;#160; Inside that “Untitled” panel are 4 more tabs.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="4" width="400" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="127"&gt;&lt;strong&gt;Tab&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="273"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="127"&gt;Template Code&lt;/td&gt;        &lt;td valign="top" width="273"&gt;You write this script.&amp;#160; This script builds the Template Source script.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="127"&gt;Interface Code&lt;/td&gt;        &lt;td valign="top" width="273"&gt;You write this script.&amp;#160; It builds a UI for gathering parameters from the user.&amp;#160; &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="127"&gt;Template Source&lt;/td&gt;        &lt;td valign="top" width="273"&gt;This script is generated and you do not touch it.&amp;#160; This is the code generation program.&amp;#160; This is the output of the Template Code script.&amp;#160; Look here if you have errors if your Template Code script.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="127"&gt;Interface Source&lt;/td&gt;        &lt;td valign="top" width="273"&gt;This script is generated and you do not touch it.&amp;#160; As the TemplateCode generates the Template Source, so too does the Interface Code generate the Interface Source.&amp;#160; Use this to debug your Interface Code.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="127"&gt;Output&lt;/td&gt;        &lt;td valign="top" width="273"&gt;If the output from the Template Source is not redirected to a file, you will see it here when you execute the template.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As you can see, you will do your editing primarily in the template code tab.&amp;#160; If you build a UI, you will also use the interface code tab.&amp;#160; &lt;/p&gt;  &lt;p&gt;In its simplest form, a template can be comprised simply of template code which accesses the MyMeta object as input and the output object as output.&amp;#160; Next, you will want to redirect output to files, then finally build a UI.&amp;#160; The data gathered in the UI is made available to the template code via the input object.&amp;#160; &lt;/p&gt;  &lt;p&gt;Let’s start with something simple.&amp;#160; In the Template Code and Interface Code tabs, you will see some default code.&amp;#160; Keep it.&amp;#160; You will need it.&amp;#160; Run the template (F5).&amp;#160;&amp;#160; You see the output in the output tab.&amp;#160; Great!&amp;#160; Back at the Template Code window, find the Render() method.&amp;#160; This is where you will write your code.&amp;#160; It is your entry point.&amp;#160; You can write your own methods outside of it and call them from the Render method.&amp;#160; &lt;/p&gt;  &lt;h2&gt;Server tags&lt;/h2&gt;  &lt;p&gt;&amp;lt;% and %&amp;gt; tags define blocks of code within the Template Code.&amp;#160; As in ASP or JSP, this is technically a code file, but anything outside the server tags is output as a literal.&amp;#160; Note that in the Template Code window, there is literal region that looks like this:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;%&amp;gt;        &lt;br /&gt;You can toggle in and out of script like this         &lt;br /&gt;&amp;lt;%&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;Take a look at the Template Source tab.&amp;#160; You’ll see that this literal has been converted to this:&lt;/p&gt;  &lt;p&gt;output.writeln(&amp;quot;You can toggle in out of script like this&amp;quot;);&lt;/p&gt;  &lt;p&gt;To keep things simple for now, I removed indentation of this literal in the template code window.&amp;#160; You will find that line breaks and tabs will be adhered to in these literal regions.&amp;#160; For finer control, you can use direct output in the Template Code like this:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;output.writeln(&amp;quot;You can toggle in out of script like this&amp;quot;);        &lt;br /&gt;%&amp;gt;         &lt;br /&gt;You can toggle in out of script like this         &lt;br /&gt;&amp;lt;%&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;The output becomes:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;output.writeln(&amp;quot;You can toggle in out of script like this&amp;quot;);        &lt;br /&gt;output.writeln(&amp;quot;&amp;quot;);         &lt;br /&gt;output.writeln(&amp;quot;You can toggle in out of script like this&amp;quot;);&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;Of course, if you only did your literal output in this way, your template code would be identical to your template source.&amp;#160; But until you get used to it, you can do this to fine tune your whitespace.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You can also dereference a single symbol from your code without using a call to output.writeln() using the &amp;lt;%= %&amp;gt; tags.&amp;#160; For example:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;int keyCount = input.Keys.Count;        &lt;br /&gt;int j = 0;         &lt;br /&gt;foreach (string inputKey in input.Keys)         &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; j++;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string inputValue = input[inputKey].ToString();         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; %&amp;gt;Input key &amp;lt;%=j.ToString()%&amp;gt;&amp;lt;%         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; %&amp;gt; of &amp;lt;%=keyCount.ToString()%&amp;gt;&amp;lt;%         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; %&amp;gt;: &amp;lt;%=inputKey%&amp;gt;&amp;lt;%         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; %&amp;gt; = &amp;lt;%=inputValue%&amp;gt;&amp;lt;%=&amp;quot;\r\n&amp;quot;%&amp;gt;&amp;lt;%         &lt;br /&gt;}&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;The Dereference Symbol tag pair is equivalent to the output.writeln.&amp;#160; Notice that you never put a semicolon between these tags.&amp;#160; To do so would generate something like this in the Template Source.&amp;#160;&amp;#160; So this tag:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;&amp;lt;%=j.ToString();%&amp;gt;&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;Would generate this:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;output.write(j.ToString(););&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;While it’s obvious to see how the premature semicolon breaks the generated code, it is still sometimes hard to remember to omit your trailing semicolons when using the Dererence Symbol tags.&amp;#160; Understanding that the text is output verbatim to an output.writeln() call makes it easier to keep things straight.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Using MyMeta&lt;/h2&gt;  &lt;p&gt;Okay, so we could output anything we want now, but we don’t have much to do without some input.&amp;#160; The input is going to start from our database schema.&amp;#160; This comes from the MyMeta object.&amp;#160; MyMeta is a global object instance of the MyMeta library and is available throughout your script.&amp;#160; Have a look at what it can do…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaInsight1_5F00_01563463.png"&gt;&lt;img title="MyMetaInsight1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="132" alt="MyMetaInsight1" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaInsight1_5F00_thumb_5F00_43C8D699.png" width="319" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The databases property will give you a collection of databases.&amp;#160; From there, you can drill down into tables, columns, keys, stored procedures, etc.&amp;#160; To get a single database, first you have to access it by index from the MyMeta.Databases collection.&lt;/p&gt;  &lt;p&gt;MyMeta.Databases[&amp;quot;Northwind&amp;quot;]&lt;/p&gt;  &lt;p&gt;Type the line above in the Render method.&amp;#160; This references the Northwind database.&amp;#160; For RDBMS servers like Oracle, there may be one or many databases.&amp;#160; For local databases, this may not apply.&amp;#160; &lt;/p&gt;  &lt;p&gt;Now that we are referencing a database, you should be able to reference schema information for the tables on that database.&amp;#160; You can drill down from the database reference to its tables collection.&amp;#160; But lo, press the period key at the end of that line, and no code insight comes up.&amp;#160; The code insight is useful but limited.&amp;#160; It does not show you types, and sometimes it doesn’t know the type of the subject and can’t show its members.&amp;#160; &lt;/p&gt;  &lt;p&gt;Workaround – If you assign that reference to a strongly typed variable, the variable gives the code insight enough information to show you its members.&amp;#160; There’s just one problem.&amp;#160; You don’t know the type either.&amp;#160; You’ll have to guess.&amp;#160; This sounds terrible, but it’s not that bad.&amp;#160; Remember I said you need to be familiar with the help files?&amp;#160; Open the MyMeta help file from the MyGeneration help menu.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaHelpContents_5F00_156F50EC.png"&gt;&lt;img title="MyMetaHelpContents" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="648" alt="MyMetaHelpContents" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaHelpContents_5F00_thumb_5F00_257E1FA3.png" width="219" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Right there in the root you can see a list of interfaces provided by MyMeta.&amp;#160; You can easily guess which type our reference is.&amp;#160; It is an IDatabase.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaInsight2_5F00_12C925EC.png"&gt;&lt;img title="MyMetaInsight2" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="144" alt="MyMetaInsight2" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyMetaInsight2_5F00_thumb_5F00_553BC822.png" width="392" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now we have the next level of code insight.&amp;#160; This is only required for code insight.&amp;#160; Instead of this (which provides code insight):&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;IDatabase db = MyMeta.Databases[&amp;quot;Northwind&amp;quot;];        &lt;br /&gt;ITable tbl = db.Tables[&amp;quot;table1&amp;quot;];         &lt;br /&gt;IColumn col = tbl.Columns[&amp;quot;id&amp;quot;];         &lt;br /&gt;output.writeln(col.Name);&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;This would work too.&amp;#160; But you have to be more familiar with MyMeta object hierarchies.&amp;#160; &lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;output.writeln(        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; MyMeta         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Databases[&amp;quot;Northwind&amp;quot;]         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Tables[&amp;quot;table1&amp;quot;]         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Columns[&amp;quot;id&amp;quot;].Name         &lt;br /&gt;);&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;Most collections in MyMeta have named members and can be accessed by ordinal index or by named index:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;MyMeta.Databases[0];        &lt;br /&gt;MyMeta.Databases[“Northwind”];&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;You now have all the tools you need to build and run a useful code generation template using MyGeneration.&lt;/p&gt;  &lt;h2&gt;Redirecting output&lt;/h2&gt;  &lt;p&gt;Expect to be executing your templates repeatedly during development of the template itself and periodically during development of whatever project consumes your generated code.&amp;#160; It becomes cumbersome to copy the contents of the output window from the clipboard (MyGeneration automatically copies it to the clipboard when it writes to the output window) over to an editor to overwrite previous output.&amp;#160; It would be convenient to write output directly to a file on disk.&amp;#160; The output object can do this.&amp;#160; It can also write to multiple files.&lt;/p&gt;  &lt;p&gt;For more detail on the output object, see the Zeus help files under IZeusOutput and ZeusOutput class.&amp;#160; But here is a basic use of output to files.&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;// At first, our output buffer is empty.&amp;#160; Add something to it.        &lt;br /&gt;output.writeln(“Something”);         &lt;br /&gt;        &lt;br /&gt;// Save the current buffer to 2 different files.         &lt;br /&gt;output.save(path1, “o”);         &lt;br /&gt;output.save(path2, “o”);         &lt;br /&gt;        &lt;br /&gt;// Start a new buffer and save it to a third file.         &lt;br /&gt;output.clear();         &lt;br /&gt;%&amp;gt;Something else&amp;lt;%         &lt;br /&gt;output.save(path3, “o”);         &lt;br /&gt;        &lt;br /&gt;// Start a new buffer and since we don’t clear it, it shows up in the output window.         &lt;br /&gt;output.clear();         &lt;br /&gt;%&amp;gt;Wrote to: &amp;lt;%=path1%&amp;gt;&amp;lt;%         &lt;br /&gt;%&amp;gt;Wrote to: &amp;lt;%=path2%&amp;gt;&amp;lt;%         &lt;br /&gt;%&amp;gt;Wrote to: &amp;lt;%=path3%&amp;gt;&amp;lt;%&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;You may have noticed that I’m mixing&amp;#160; %&amp;gt;literals&amp;lt;% (outside the server code) with explicit writeln.output() calls.&amp;#160; Remember that these are interchangable and you’ll want to shoot for consistency most of the time.&amp;#160; I just mixed them up to show some variety in my examples.&lt;/p&gt;  &lt;p&gt;The second parameter of the save() method (“o”) is not documented under IZeusOutput very well.&amp;#160; They are found under the ZeusOutput class, but poorly.&amp;#160; I’ll document them here to save you time.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="4" width="400" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="77"&gt;Value&lt;/td&gt;        &lt;td valign="top" width="323"&gt;Description&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="77"&gt;“o”&lt;/td&gt;        &lt;td valign="top" width="323"&gt;Overwrites an existing file.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="77"&gt;“b”&lt;/td&gt;        &lt;td valign="top" width="323"&gt;Overwrites existing file but backs it up first.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="77"&gt;“a”&lt;/td&gt;        &lt;td valign="top" width="323"&gt;Appends an existing file.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="77"&gt;“d”&lt;/td&gt;        &lt;td valign="top" width="323"&gt;Do not overwrite (silent failure).&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Building a UI for your template.&lt;/p&gt;  &lt;p&gt;If you need parameters for your template execution they can be gathered at runtime by a UI which you build into your template.&amp;#160; The UI code is written in the editor under the Interface Code tab.&amp;#160; Again, the default sample code should be kept to get you started.&amp;#160; The Setup() method is your entry point.&amp;#160; In here, you have a global object named ui.&amp;#160; It is of type Zeus.UserInterface.GuiController.&amp;#160; You will find help for it in Zeus help under Zeus.UserInterface.&amp;#160; &lt;/p&gt;  &lt;p&gt;In a nutshell, you have some Addxxxx methods to create UI elements such as buttons and comboboxes.&amp;#160; These methods return objects of the corresponding type (IGuidButton, etc.).&amp;#160; Those objects then have layout properties such as top, bottom, width, etc.&amp;#160; To keep things simple, the IGuiController ui is very good as choosing default layout parameters for you.&amp;#160; if you add an element to the UI, it will appear in vertical order relative to other elements you have added.&amp;#160; It will fill the form horizontally.&amp;#160; While this isn’t always pretty, it makes it easy to get started.&lt;/p&gt;  &lt;p&gt;First thing’s first.&amp;#160; The interface is disabled for now.&amp;#160; Uncomment the contents of the Setup() method to see what happens.&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;public override void Setup()        &lt;br /&gt;{         &lt;br /&gt;&amp;#160; ui.Width&amp;#160; = 100;         &lt;br /&gt;&amp;#160; ui.Height = 100;         &lt;br /&gt;&amp;#160; GuiLabel lblDemo = ui.AddLabel(&amp;quot;lblDemo&amp;quot;, &amp;quot;Demo&amp;quot;, &amp;quot;Demo Tooltip&amp;quot;);         &lt;br /&gt;&amp;#160; ui.ShowGui = true;         &lt;br /&gt;}&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationDefaultUI_5F00_30AA3A9E.png"&gt;&lt;img title="MyGenerationDefaultUI" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="104" alt="MyGenerationDefaultUI" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationDefaultUI_5F00_thumb_5F00_5E2B5A61.png" width="127" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;That wasn’t hard.&amp;#160; The code sizes the form (lines 1 and 2), adds a label (line 3) and displays the form (line4).&amp;#160; To expound on this, we’ll keep lines 1, 2, and 4.&amp;#160; We’ll increase the size of the form and replace line 3 with our customized form code.&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;public override void Setup()        &lt;br /&gt;{         &lt;br /&gt;&amp;#160; ui.Width&amp;#160; = 500;         &lt;br /&gt;&amp;#160; ui.Height = 300;         &lt;br /&gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160; // Customize things here...         &lt;br /&gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160; ui.ShowGui = true;         &lt;br /&gt;}&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;Next we’ll add some controls that are actually going to be useful in this sort of project.&amp;#160; We’ll add&amp;#160; a database selection combo box and an output file selector (with labels).&amp;#160; I’ll add them to their own method to keep the Setup() method cleaner.&amp;#160; Remember that the ui object creates the controls for us.&amp;#160; This is a bit easier than MyMeta.&amp;#160; These functions have code insight that tells us the return type.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationUICodeInsight1_5F00_76BAF7B1.png"&gt;&lt;img title="MyGenerationUICodeInsight1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="41" alt="MyGenerationUICodeInsight1" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationUICodeInsight1_5F00_thumb_5F00_724476EA.png" width="507" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Easy enough?&amp;#160; This won’t take long.&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;public void CreateControls()        &lt;br /&gt;{         &lt;br /&gt;&amp;#160; // Database selection drop-down.         &lt;br /&gt;&amp;#160; GuiLabel lblSelectDatabase =         &lt;br /&gt;&amp;#160;&amp;#160; ui.AddLabel(&amp;quot;lblSelectDatabase&amp;quot;, &amp;quot;Select a database:&amp;quot;, &amp;quot;&amp;quot;);         &lt;br /&gt;&amp;#160; GuiComboBox cboSelectDatabase =         &lt;br /&gt;&amp;#160;&amp;#160; ui.AddComboBox(&amp;quot;cboSelectDatabase&amp;quot;, &amp;quot;Select a database&amp;quot;);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160; // Output file picker         &lt;br /&gt;&amp;#160; GuiLabel lblOutputFile =         &lt;br /&gt;&amp;#160;&amp;#160; ui.AddLabel(&amp;quot;lblOutputFile&amp;quot;, &amp;quot;Output filename: &amp;quot;, &amp;quot;&amp;quot;);         &lt;br /&gt;&amp;#160; GuiTextBox txtOutputFile =         &lt;br /&gt;&amp;#160;&amp;#160; ui.AddTextBox(&amp;quot;txtOutputFile&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;Output filename&amp;quot;);         &lt;br /&gt;&amp;#160; GuiFilePicker fpOutputFile =         &lt;br /&gt;&amp;#160;&amp;#160; ui.AddFilePicker(&amp;quot;fpOutputFile&amp;quot;, &amp;quot;...&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;txtOutputFile&amp;quot;, false);         &lt;br /&gt;}&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Notice that when passing a control as a parameter to an Addxxx() method, it expects the control name as a string, not the object reference for that control.&amp;#160; To keep things straight, I always make the string ID parameter which I pass to the add method identical to the object reference to which I assign the ui element (&lt;em&gt;lblOutputfile&lt;/em&gt; = ui.AddLabel(“&lt;em&gt;lblOutputFile&lt;/em&gt;”…) for example).&lt;/p&gt;  &lt;p&gt;Well, that creates the form controls, but they have no data behind them.&amp;#160; You will frequently need to bind MyMeta elements to your UI elements.&amp;#160; We will want to do so with our database selection combo box.&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;cboSelectDatabase.BindData(MyMeta.Databases);&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;How easy was that?&amp;#160; The layout is still a little ugly, but we now have a working UI.&amp;#160; The next step is to use it from the Template Code.&amp;#160; We’ll get to that in a moment.&lt;/p&gt;  &lt;h2&gt;UI Layout&lt;/h2&gt;  &lt;p&gt;Right now, let’s fix up the layout a bit for sanity’s sake.&amp;#160; This is easy too.&amp;#160; Just set the width, height, top and left properties of each UI element.&amp;#160; You can calculate them in conjunction with the width and height properties of the ui object.&lt;/p&gt;  &lt;p&gt;Here is the finished UI.&amp;#160; As simple as it is, it may be all you need for some projects.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationUIWithLayout_5F00_3FE0A36B.png"&gt;&lt;img title="MyGenerationUIWithLayout" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px; border-right-width: 0px" height="150" alt="MyGenerationUIWithLayout" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MyGenerationUIWithLayout_5F00_thumb_5F00_344AE62C.png" width="500" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;public class GeneratedGui : DotNetScriptGui        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Declare UI elements in class scope.         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; GuiLabel lblSelectDatabase;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; GuiComboBox cboSelectDatabase;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; GuiLabel lblOutputFile;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; GuiTextBox txtOutputFile;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; GuiFilePicker fpOutputFile; &lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public GeneratedGui(ZeusContext context) : base(context) {} &lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; //-----------------------------------------        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // The User Interface Entry Point         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //-----------------------------------------         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public override void Setup()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.Width&amp;#160; = 500;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.Height = 300;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.Title = &amp;quot;Building our first UI!&amp;quot;;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Customize things here...         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CreateControls();         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LayoutControls();         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DataBindControls();         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.ShowGui = true;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public void CreateControls()        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Database selection drop-down.         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblSelectDatabase =         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.AddLabel(&amp;quot;lblSelectDatabase&amp;quot;, &amp;quot;Select a database:&amp;quot;, &amp;quot;&amp;quot;);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cboSelectDatabase =         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.AddComboBox(&amp;quot;cboSelectDatabase&amp;quot;, &amp;quot;Select a database&amp;quot;); &lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Output file picker        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblOutputFile =         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.AddLabel(&amp;quot;lblOutputFile&amp;quot;, &amp;quot;Output filename: &amp;quot;, &amp;quot;&amp;quot;);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; txtOutputFile =         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.AddTextBox(&amp;quot;txtOutputFile&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;Output filename&amp;quot;);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fpOutputFile =         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.AddFilePicker(&amp;quot;fpOutputFile&amp;quot;, &amp;quot;...&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;txtOutputFile&amp;quot;, false);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void LayoutControls()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Form         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.Width&amp;#160; = 500;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.Height = 150;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ui.Title = &amp;quot;Building our first UI!&amp;quot;;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int margin = 12;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Position dropdown and label.         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblSelectDatabase.Width = 100;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblSelectDatabase.Left = margin;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblSelectDatabase.Top = margin;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cboSelectDatabase.Width = ui.Width         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; - lblSelectDatabase.Width - margin * 3;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cboSelectDatabase.Top = lblSelectDatabase.Top;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cboSelectDatabase.Left = lblSelectDatabase.Left         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; + lblSelectDatabase.Width + margin ;&lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Position file picker        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblOutputFile.Width = lblSelectDatabase.Width;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblOutputFile.Left = lblSelectDatabase.Left;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblOutputFile.Top = lblSelectDatabase.Top         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; + lblSelectDatabase.Height + margin;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fpOutputFile.Width = 25;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fpOutputFile.Top = lblOutputFile.Top;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fpOutputFile.Left = ui.Width - fpOutputFile.Width - margin;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; txtOutputFile.Width = ui.Width         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; - fpOutputFile.Width - lblOutputFile.Width - margin * 4;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; txtOutputFile.Top = lblOutputFile.Top;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; txtOutputFile.Left = cboSelectDatabase.Left;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public void DataBindControls()        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cboSelectDatabase.BindData(MyMeta.Databases);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Consuming the UI, Method 1: Input&lt;/h2&gt;  &lt;p&gt;You have a working UI now but what good is it?&amp;#160; What does it actually do?&amp;#160; By itself, it lets you specify parameters but those parameters have to be consume by the Template Code script.&amp;#160; It does so in two ways.&amp;#160; I’ve mentioned before that the first way is through the input object.&amp;#160; The input object is also a global instance.&amp;#160; Once again, see the Zeus help file under IZeusInput and ZeusInput.&amp;#160; The input &lt;/p&gt;  &lt;p&gt;object provides the values for the UI elements as selected by the user at runtime.&amp;#160; It is more convenient than the second method, which is to access the elements themselves.&amp;#160; It is more convenient because every element has a single scalar value.&amp;#160; For example, a dropdown list has a list of elements, a selected value, a selected index, etc.&amp;#160; The input object contains a single string for that control, the selected value.&amp;#160; &lt;/p&gt;  &lt;p&gt;The input object is a collection of values, each with a name corresponding to a control on the UI form (more specifically the string ID property used in the construction of said control) and a value as entered or selected in that control at runtime.&amp;#160; Objects retrieved from this collection must be cast to specific types, usually string.&lt;/p&gt;  &lt;p&gt;Here’s an interesting exercise.&amp;#160; Put this code in your Template Code’s Render method.&amp;#160; It will output all the items in the input collection.&amp;#160; Now you’ll know exactly what’s available to you.&amp;#160; You’ll see the elements from your UI as well as several fields defined by MyGeneration.&amp;#160; MyGeneration input key names begin with double underscores.&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;&amp;#160; output.writeln(&amp;quot;Input fields: &amp;quot;);        &lt;br /&gt;&amp;#160; foreach(string s in input.Keys)         &lt;br /&gt;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160; output.write(s);         &lt;br /&gt;&amp;#160;&amp;#160; try         &lt;br /&gt;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; output.write(&amp;quot;=&amp;quot;);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; output.write((string)input);         &lt;br /&gt;&amp;#160;&amp;#160; }         &lt;br /&gt;&amp;#160;&amp;#160; catch { }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; output.writeln(&amp;quot;&amp;quot;);         &lt;br /&gt;&amp;#160; }&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;You’ll also see an entry for cboSelectDatabase.&amp;#160; It is as you selected in the UI.&amp;#160; You can now use this in your template code like this:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;IDatabase selectedDB =        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; MyMeta.Databases[(string)input[&amp;quot;cboSelectDatabase&amp;quot;]];         &lt;br /&gt;output.writeln(&amp;quot;Selected database: &amp;quot; + selectedDB.Name);         &lt;br /&gt;output.writeln(&amp;quot;&amp;quot;); &lt;/p&gt;      &lt;p&gt;foreach (ITable t in selectedDB.Tables)        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; output.writeln(&amp;quot;\tTable: &amp;quot; + t.Name);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; foreach(IColumn c in t.Columns)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; output.write(&amp;quot;\t\tColumn: name=&amp;quot; + c.Name);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; output.write(&amp;quot;, dbTargetType=&amp;quot; + c.DbTargetType);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; output.write(&amp;quot;, DataType=&amp;quot; + c.DataType);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; output.write(&amp;quot;, DataTypeName=&amp;quot; + c.DataTypeName);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; output.writeln(&amp;quot;&amp;quot;);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; output.writeln(&amp;quot;&amp;quot;);         &lt;br /&gt;}&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;That script will output basic schema for all tables in your database.&amp;#160; &lt;/p&gt;  &lt;p&gt;Notice that when retrieving the value of the cboSelectDatabase combo box, we did not have to drill into a SelectedValue property to get the selected value.&amp;#160; The input object gives us a single string for cboSelectDatabase when we pass its name as a key.&amp;#160; The string is its selected value.&lt;/p&gt;  &lt;h2&gt;Consuming the UI, Method 2: UI&lt;/h2&gt;  &lt;p&gt;The second method is discouraged.&amp;#160; You can use the ui object to retrieve the UI elements directly and query their properties for the values you want.&amp;#160; &lt;/p&gt;  &lt;p&gt;In the previous example, we might have changed this line:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;IDatabase selectedDB =        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; MyMeta.Databases[(string)input[&amp;quot;cboSelectDatabase&amp;quot;]];&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;and made it work like this:&lt;/p&gt; &lt;code&gt;   &lt;blockquote&gt;     &lt;p&gt;IDatabase selectedDB =        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; MyMeta.Databases[((GuiComboBox)ui[&amp;quot;cboSelectDatabase&amp;quot;]).SelectedValue];&lt;/p&gt;   &lt;/blockquote&gt; &lt;/code&gt;  &lt;p&gt;When I change that line, the rest of the template code still works.&amp;#160; &lt;/p&gt;  &lt;p&gt;I hope this has given you everything you need to get started.&amp;#160; It certainly won’t cover everything you need to learn to achieve your particular task, but my goal was to get you started so that you would know where to look and have the tools to troubleshoot your work.&amp;#160; If I’ve left out something fundamental, please let me know.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Phil Gilmore (&lt;a href="http://interactiveasp.net/blogs/spgilmore/default.aspx" target="_blank"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=3761" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/MyGeneration/default.aspx">MyGeneration</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Programming/default.aspx">Programming</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Code+Generation/default.aspx">Code Generation</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Development+Tools/default.aspx">Development Tools</category></item><item><title>CodeRush Xpress for Visual Studio 2008 version 3.2.1 cheat sheet</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/06/19/coderush-xpress-for-visual-studio-2008-cheat-sheet.aspx</link><pubDate>Fri, 19 Jun 2009 18:37:52 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:947</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=947</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/06/19/coderush-xpress-for-visual-studio-2008-cheat-sheet.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://www.devexpress.com/" target="_blank"&gt;&lt;img style="border-right-width: 0px; margin: 10px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="CodeRushCollage" align="left" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/CodeRushCollage_5F00_3.gif" width="166" height="240"&gt;&lt;/a&gt; 06/18/2009, Phil Gilmore&lt;/p&gt; &lt;p&gt;I've been a CodeRush fan since I bought my first copy from Eagle Software for Delphi many years ago.&amp;nbsp; I recently downloaded CodeRush Xpress for Visual Studio 2008, version 3.2.1.&amp;nbsp; I have constructed a cheat sheet for it which is available for download on this server.&amp;nbsp; In addition to publishing the cheat sheet, I thought I'd write a small review of the product.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I'm busy these days so I didn't get a chance to get cozy with it right away.&amp;nbsp; That's a point I want to make.&amp;nbsp; I installed it but was able to work with it present but without it causing me any problems.&amp;nbsp; Most other add-in packages require immediate attention just to tone down the overbearing features that don't work the way you want them to and hinder your productivity.&amp;nbsp; CodeRush Xpress did not do that.&amp;nbsp; In fact, I didn't find a place where CRXP could be configured at all.&amp;nbsp; I had to go to the help file to figure out how to use it.&amp;nbsp; This wasn't inconvenient, but the fact that it didn't clutter up my Visual Studio menu was a pleasant surprise.&lt;/p&gt; &lt;p&gt;The help file was very well constructed.&amp;nbsp; Everything was grouped intuitively, every topic was covered, easy to find, easy to browse, and very succinct.&amp;nbsp; "To do this, press these keys, this is what you'll see" is the flow.&amp;nbsp; And every feature where applicable has a screen shot.&amp;nbsp; Very nice.&amp;nbsp; A+ on the documentation.&lt;/p&gt; &lt;p&gt;After reading the help file, I knew I would need to build a cheat sheet to remember all the hot keys, as most of the features are hot-key driven.&amp;nbsp; Many are available in the context menu, but I'm a keyboard guy anyway.&amp;nbsp; &lt;/p&gt; &lt;p&gt;So I've created the cheat sheet and was further surprised by how often I was using these features.&amp;nbsp; CRXP is the free version with fewer features than its commercial sibling, but very functional, aesthetically impressive and with unprecedented usefulness.&lt;/p&gt; &lt;p&gt;I do have one small complaint about the product.&amp;nbsp; It creates a lot of files for itself in the source folder for each of your products.&amp;nbsp; You can safely delete them, but they sure clutter up your source control tools.&amp;nbsp; They appear to be cache files (among other things) so it's probably best to leave them where they lie if possible.&amp;nbsp; I'm using Subversion, so I can easily add an Ignore flag on the root folder and the problem is solved.&amp;nbsp; Other systems may not be so tolerant.&amp;nbsp; These files seem to be project specific, so there may not be a better alternative.&lt;/p&gt; &lt;p&gt;Here is a screen shot of the pocket reference cheat sheet.&amp;nbsp; The standard cheat sheet contains additional comments about each feature and is larger.&amp;nbsp; Both fit on a single printed letter-size page (the pocket reference can printed and be trimmed to just over a quarter of a letter-size page).&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="CodeRushXpressCheatSheetShot" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/CodeRushXpressCheatSheetShot_5F00_1.png" width="236" height="260"&gt; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;With that, I leave you with a recommendation that you download, install, learn and use the product, and a short wish list for the talented folks at Developer Express:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Remove DX* files when issuing a CLEAN SOLUTION operation.  &lt;li&gt;Regular expression support in the class nav and file nav dialogs, especially ^ and $ anchors. &lt;li&gt;The quicknav has a popup tooltip window with details about the item under the mouse cursor.&amp;nbsp; I'm a keyboard guy. It would be nice if the arrow keys could invoke this tooltip window as well.  &lt;li&gt;Drop marker.&amp;nbsp; I was very disappointed to see that this feature was withheld in the Xpress edition.  &lt;li&gt;A "Clear All Markers" feature.&amp;nbsp; I often hit escape too many times while clearing intellisense windows and find myself jumping around unexpectedly through the code to page markers I didn't know were there (dropped by the file nav feature). &lt;li&gt;An "About" dialog (or node in the options tree) to tell me which version I have installed. &lt;li&gt;Update the documents to tell me about the configuration dialogs! &lt;li&gt;More granular breaking points in Increase and Decrease Selection.&amp;nbsp; For example, I should always be able to click on a symbol, increase selection and highlight just that symbol.&amp;nbsp; If that symbol is a method parameter, selection immediately includes the entire parameter declaration, including the type.&amp;nbsp; It does not select Just the current symbol.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Addendum:&amp;nbsp; I have revised my wish list somewhat.&amp;nbsp; I had not discovered that the context menu in the QuickNav and FileNav dialogs had a link to open the Options window.&amp;nbsp; Here, you can configure many things, including markers and shortcut keys.&amp;nbsp; Since then, I have removed the configurable shortcut keys feature request.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;You can download CodeRush Xpress 3.2.1 for Visual Studio 2008 here:&lt;/p&gt; &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vcsharp/dd218053.aspx"&gt;http://msdn.microsoft.com/en-us/vcsharp/dd218053.aspx&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;You can download my CodeRush Xpress 3.2.1 cheat sheets here:&lt;/p&gt; &lt;p&gt;&lt;a href="http://interactiveasp.net/media/p/944.aspx"&gt;http://interactiveasp.net/media/p/944.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=947" width="1" height="1"&gt;</description></item><item><title>How to support file uploads in ASP.Net MVC</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/06/03/how-to-support-file-uploads-in-asp-net-mvc.aspx</link><pubDate>Wed, 03 Jun 2009 22:57:37 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:873</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=873</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/06/03/how-to-support-file-uploads-in-asp-net-mvc.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MVCFirefoxLogo_5F00_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 10px; border-top: 0px; border-right: 0px" border="0" alt="MVCFirefoxLogo" align="left" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MVCFirefoxLogo_5F00_thumb.png" width="100" height="75"&gt;&lt;/a&gt; 06/03/2009, Phil Gilmore&lt;/p&gt; &lt;p&gt;ASP.NET MVC can support file uploads.&amp;nbsp; You need two components to support file uploads.&amp;nbsp; &lt;/p&gt; &lt;ul&gt; &lt;li&gt;A form in your markup (view) which contains an &lt;em&gt;&amp;lt;input type="file"...&amp;gt;&lt;/em&gt; tag and which has the proper &lt;em&gt;enctype&lt;/em&gt; attribute.&lt;/li&gt; &lt;li&gt;A controller action which will receive the upload information and perform a task with it.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Your form can be as simple as this:&lt;/p&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;form &lt;/span&gt;&lt;span style="color: red"&gt;action&lt;/span&gt;&lt;span style="color: blue"&gt;="/MyController/SendFile" &lt;/span&gt;&lt;span style="color: red"&gt;enctype&lt;/span&gt;&lt;span style="color: blue"&gt;="multipart/form-data" &lt;/span&gt;&lt;span style="color: red"&gt;method&lt;/span&gt;&lt;span style="color: blue"&gt;="post"&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="file" &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="SourceFile" &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;="SourceFile" /&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="submit" &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;="Send" &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;="btnUpload" &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="Submit1" /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;form&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Notice the &lt;em&gt;enctype&lt;/em&gt; attribute on the &lt;em&gt;&amp;lt;form...&amp;gt;&lt;/em&gt; tag.&amp;nbsp; This is required to support file uploads.&amp;nbsp; Notice too that the action points to our controller action which knows how to work with the uploaded file.&amp;nbsp; Lastly, you'll see the &lt;em&gt;&amp;lt;input type="file...&amp;gt;&lt;/em&gt; tag inside the form.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is a screen shot of the form, rendered in Firefox 3.0.&lt;/p&gt;
&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="UploadForm" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/UploadForm_5F00_1.png" width="236" height="111"&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The form as shown above contains only Html and is fairly universal regardless of the web framework you are using.&amp;nbsp; This means it should be familiar to you.&amp;nbsp; You have probably seen the enctype attribute on a form with its accompanying. The id attribute of the &lt;em&gt;&amp;lt;input type="file"... &amp;gt;&lt;/em&gt; tag will be mapped by the MVC framework to a property of your model or to a parameter in the action method (in this case the &lt;em&gt;SendFile&lt;/em&gt; action).&amp;nbsp; Here is an example of an action which takes the file as a parameter.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContentResult &lt;/span&gt;SendFile(&lt;span style="color: #2b91af"&gt;HttpPostedFileBase &lt;/span&gt;SourceFile)
{

}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This action gets an HttpPostedFileBase object containing properties for the original local filename and the content of the file itself.&amp;nbsp; Notice that the MVC framework only maps this parameter to the file data if the parameter name (&lt;em&gt;SourceFile&lt;/em&gt;) matches the id and name parameters from the &lt;em&gt;&amp;lt;input type="file"...&amp;gt;&lt;/em&gt; tag in the form.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you have a strange circumstance which prohibits you from organizing your form and action in this way and MVC cannot map the file data to an HttpPostedFileBase parameter, you can retrieve it manually from the Request object.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContentResult &lt;/span&gt;SendFile()
{
    &lt;span style="color: #2b91af"&gt;HttpPostedFileBase &lt;/span&gt;SourceFile = Request.Files[0];
    &lt;span style="color: green"&gt;//...
&lt;/span&gt;}&lt;/pre&gt;&lt;pre class="code"&gt;
&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Here is an evaluation of the HttpPostedFileBase object as provided to the SendFile action method.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/UploadFilesQuickwatch.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="UploadFilesQuickwatch" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/UploadFilesQuickwatch_5F00_thumb.png" width="591" height="293"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Once you have an HttpPostedFileBase object, you can work with the file data.&amp;nbsp; Here are the members of the HttpPostedFileBase class.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected &lt;/span&gt;HttpPostedFileBase();
&lt;span style="color: blue"&gt;public virtual int &lt;/span&gt;ContentLength { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
&lt;span style="color: blue"&gt;public virtual string &lt;/span&gt;ContentType { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
&lt;span style="color: blue"&gt;public virtual string &lt;/span&gt;FileName { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
&lt;span style="color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Stream &lt;/span&gt;InputStream { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
&lt;span style="color: blue"&gt;public virtual void &lt;/span&gt;SaveAs(&lt;span style="color: blue"&gt;string &lt;/span&gt;filename);&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is pretty simple stuff.&amp;nbsp; You can save the file, read the file or get the file's name and mime type (&lt;em&gt;ContentType&lt;/em&gt;).&amp;nbsp; ContentLength is generally not necessary since the InputStream already contains the data of the correct size.&lt;/p&gt;
&lt;p&gt;Here is a working action that displays the content of any file uploaded to it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContentResult &lt;/span&gt;SendFile()
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(Request.Files.Count == 0)
        &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContentResult&lt;/span&gt;() { ContentType = &lt;span style="color: #a31515"&gt;"text/plain"&lt;/span&gt;, Content = &lt;span style="color: #a31515"&gt;"File upload failed." &lt;/span&gt;};
    &lt;span style="color: blue"&gt;else
    &lt;/span&gt;{
        &lt;span style="color: #2b91af"&gt;HttpPostedFileBase &lt;/span&gt;SourceFile = Request.Files[0];
        &lt;span style="color: #2b91af"&gt;ContentResult &lt;/span&gt;result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContentResult&lt;/span&gt;();
        result.ContentType = &lt;span style="color: #a31515"&gt;"text/plain"&lt;/span&gt;;

        &lt;span style="color: #2b91af"&gt;StreamReader &lt;/span&gt;reader = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamReader&lt;/span&gt;(SourceFile.InputStream);
        &lt;span style="color: blue"&gt;string &lt;/span&gt;content = reader.ReadToEnd();
        result.Content = content;

        &lt;span style="color: blue"&gt;return &lt;/span&gt;result;
    }
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;This method doesn't use the MVC-mapped HttpPostedFileBase parameter.&amp;nbsp; This is not as elegant, but doesn't require a matching name or id on the file upload element in the form markup.&amp;nbsp; This may be useful in some cases.&lt;/p&gt;
&lt;p&gt;Lastly, entering that &amp;lt;input type="file"...&amp;gt; markup into the form can be error-prone.&amp;nbsp; While the element is not large or hard to read, it must be exact and is inconsistent with styles supporting strongly-typed views mapped to an MVC model.&amp;nbsp; Therefore it may be prudent to extend the HtmlHelper class to make this easier, cleaner and more consistent.&amp;nbsp; I have written an extension method to do this.&amp;nbsp; It has 2 overloads. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static string &lt;/span&gt;Upload(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HtmlHelper &lt;/span&gt;helper, &lt;span style="color: blue"&gt;string &lt;/span&gt;name)
{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;result = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;"&amp;lt;input type=\"file\" id=\"{0}\" name=\"{0}\" /&amp;gt;"&lt;/span&gt;, name);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;result;
}

&lt;span style="color: blue"&gt;public static string &lt;/span&gt;Upload(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HtmlHelper &lt;/span&gt;helper, &lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object &lt;/span&gt;htmlAttributes)
{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;attributes;
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(htmlAttributes &lt;span style="color: blue"&gt;is &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)
        attributes = (htmlAttributes &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;).ToAttributeString();
    &lt;span style="color: blue"&gt;else
        &lt;/span&gt;attributes = (&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RouteValueDictionary&lt;/span&gt;(htmlAttributes)).ToAttributeString();

    &lt;span style="color: blue"&gt;string &lt;/span&gt;result = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;"&amp;lt;input type=\"file\" name=\"{0}\" {1} /&amp;gt;"&lt;/span&gt;, name, attributes);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;result;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Using this extension, my markup can be changed to this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;font size="2"&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;lt;% &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (Html.BeginForm(&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;"SendFile"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;"MyController"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;FormMethod&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Post, &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; { id = &lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;"sendFileForm"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;, enctype = &lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;"multipart/form-data"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; }))&lt;/p&gt;
&lt;p&gt;{ %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; Html.Upload(&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;"SourceFile"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; { style=&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;"width: 300px;"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; })%&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;br&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;input&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;&lt;font color="#ff0000" size="2"&gt;type&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;="submit"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;&lt;font color="#ff0000" size="2"&gt;value&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;="Send"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;&lt;font color="#ff0000" size="2"&gt;name&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;="btnUpload"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;&lt;font color="#ff0000" size="2"&gt;id&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;="Submit1"&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;&amp;lt;% } %&amp;gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Personally, I don't think this is as pretty as straight HTML, but it's easier to write consistently, and if you're using MVC, your whole page probably looks like this already anyway.&amp;nbsp; It's your choice.&amp;nbsp; Either way you build the markup, the controller action is unaffected.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;None of this is very complicated but I recently had to run through it and construct a proof-of-concept for some coworkers and figured I'd publish it for the masses.&amp;nbsp; I hope it was helpful.&lt;/p&gt;&lt;/font&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=873" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>Building data sequences in C#</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/05/19/building-data-sequences-in-c.aspx</link><pubDate>Tue, 19 May 2009 20:03:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:768</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=768</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/05/19/building-data-sequences-in-c.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceLogo.png"&gt;&lt;img style="border-right-width: 0px; margin: 10px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="SequenceLogo" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceLogo_5F00_thumb.png" align="left" border="0" width="258" height="112" /&gt;&lt;/a&gt; 05/19/2009, Phil Gilmore&lt;/p&gt;
&lt;p&gt;Beyond the overlap they share with enumerable collections, sequences are mostly unsupported in C#.&amp;nbsp; A notable exception are some of the static methods of the Enumerable class.&amp;nbsp; Check them out.&amp;nbsp; This support is all I need for most sequence work I do.&amp;nbsp; But the support therein is insufficient for anything complex.&amp;nbsp; To augment this support, I have written some extension methods to various CLR classes to add pseudo-support for sequences and sets.&amp;nbsp; One that seems to be especially useful is a sequence generator method.&amp;nbsp; It immediately became useful in many instances, so I thought I'd share it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;What can I do with a sequence generator?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Generate a time-of-day list to allow users to select a time from a drop-down list, perhaps in 15-minute intervals.  &lt;/li&gt;
&lt;li&gt;Generate a list of months to allow users to select a month from a drop-down list.  &lt;/li&gt;
&lt;li&gt;Generate a list of years to allow a user to select their birth year from a drop-down list.  &lt;/li&gt;
&lt;li&gt;Generate a list of any type of sequential data for user selection.  &lt;/li&gt;
&lt;li&gt;Build an "upline" of parent-child relationships in table of hierarchical data traversed through a self-join.  &lt;/li&gt;
&lt;li&gt;Any operation that would normally be constructed using a For loop or While loop to populate a list or collection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Why not collections and loops?&lt;/h2&gt;
&lt;p&gt;Looking at this list, you might be pondering the usefulness of this since many of these things can be achieved using a collections built in a for-loop or through various Linq maneuvers.&amp;nbsp; This is true.&amp;nbsp; Consider the example above in building an upline from a hierarchical table.&amp;nbsp; But the value of sequence generation is easier to see when considering that sequence data can be built by analyzing the last item in the sequence.&amp;nbsp; For example, you could use a For loop to iterate through your file system and build a List of mp3 filenames in your music collection.&amp;nbsp; This is more the function of a collection than a sequence.&amp;nbsp; This data already exists as separate elements that are found and stored in a collection.&amp;nbsp; If one mp3 file is deleted, the next does not change.&amp;nbsp; In a sequence where elements are related by generation, the removal or change of an element may affect the generation of the rest of the sequence.&amp;nbsp; For example, consider a sequence of incremental integers starting with 1000 and incrementing by 1 with a sequence size of 5.&amp;nbsp; The elements of this sequence would be 1000, 1001, 1002, 1003 1004.&amp;nbsp; If the first element were to change to 1002, the sequence would continue its pattern and become 1002, 1003, 1004, 1005, 1006.&amp;nbsp; The generation of elements in a sequence also does not necessarily require existing data to be collected.&amp;nbsp; Whereas a collection of mp3 filenames requires a filesystem that can be queried to retrieve these filenames, a sequence of incremental integer is generated from only a single seed element (1000 in my first example).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Building a sequence generator:&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; CreateSequence&amp;lt;T&amp;gt;(&lt;br /&gt;    &lt;span style="color: blue"&gt;this &lt;/span&gt;T startElement, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, T&amp;gt; getNextElement, &lt;span style="color: #2b91af"&gt;Predicate&lt;/span&gt;&amp;lt;T&amp;gt; isLastElement)&lt;br /&gt;{&lt;br /&gt;    T currentElement = startElement;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: blue"&gt;while &lt;/span&gt;(!isLastElement(currentElement))&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: blue"&gt;yield return &lt;/span&gt;currentElement;&lt;br /&gt;        currentElement = getNextElement(currentElement);&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: blue"&gt;yield return &lt;/span&gt;currentElement;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The first parameter (startElement) is the object on which the method is called.&amp;nbsp; This is probably a primitive object, but can be anything.&amp;nbsp; There are no constraints on the T type parameter so reference and value types are both game.&amp;nbsp; Even literals work.&amp;nbsp; You can call this method on the literal number 1.&amp;nbsp; Of course, this is part of the syntax of C# extension methods.&amp;nbsp; When consuming this method, you do not pass the first parameter.&amp;nbsp; But it's important to understand that this is the first element in the sequence (the seed).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The getNextElement parameter (the first that you provide) is a function.&amp;nbsp; This function can be a delegate to a method, an anonymous method, or a lambda expression.&amp;nbsp; In my examples, I use a lambda expression.&amp;nbsp; The function is given the current element and is expected to return the next element in the sequence.&amp;nbsp; For simple sequences, it will probably just add or subtract some number to the current element to produce the next one.&lt;/p&gt;
&lt;p&gt;The isLastElement parameter is another function.&amp;nbsp; As each element is added to the sequence, this function is executed to determine if the end of the sequence has been reached.&amp;nbsp; If the function returns false, then the sequence continues and getNextElement will be called again to get yet another element.&amp;nbsp; If it instead returns true, the current element will be the last element added to the sequence and the CreateSequence method will return thereafter.&lt;/p&gt;
&lt;p&gt;Beware of infinite loops when using this.&amp;nbsp; If the IsLastElement function never returns true, you will encounter an infinite loop.&amp;nbsp; For this reason, it may be a good habit to use &amp;gt;= and &amp;lt;= operators instead of == operators, for example.&amp;nbsp; Even though your sequence may be simple as in our examples, better safe than sorry.&amp;nbsp; For more complicated sequences, be even more careful.&lt;/p&gt;
&lt;p&gt;The method can be refined if desired.&amp;nbsp; It may be desirable to change the isLastElement predicate from an UNTIL condition to a WHILE condition (and hence renaming it to isValidElement instead), for example.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Using the sequence generator:&lt;/h2&gt;
&lt;p&gt;Most of these examples can be done using the static methods of the Enumerable class with a little more simplicity.&amp;nbsp; But I'll still keep the examples simple.&amp;nbsp; You can build up to the hard stuff yourself.&amp;nbsp; Here is a simple example using the sequence generator method in an ASP.NET MVC web form where a TIME must be selected.&amp;nbsp; First, the code to generate the view data:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: green"&gt;// Create a sequence of times, 15 minutes apart.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;referenceDate = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.Date;&lt;br /&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;&amp;gt; availableTimes = referenceDate.CreateSequence&lt;br /&gt;(&lt;br /&gt;    x =&amp;gt; x.AddMinutes(15), &lt;br /&gt;    x =&amp;gt; x.CompareTo(referenceDate.AddDays(1).AddMinutes(-15)) &amp;gt;= 0&lt;br /&gt;).ToList();&lt;br /&gt;&lt;br /&gt;&lt;span style="color: green"&gt;// Convert times to a list of bindable objects for a dropdown list in the MVC view.&lt;br /&gt;&lt;/span&gt;ViewData[&lt;span style="color: #a31515"&gt;"DueTimesAvailable"&lt;/span&gt;] = availableTimes.Select(&lt;br /&gt;    x =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SelectListItem&lt;/span&gt;()&lt;br /&gt;    {&lt;br /&gt;        Text = x.ToString(&lt;span style="color: #a31515"&gt;"hh:mm tt"&lt;/span&gt;),&lt;br /&gt;        Value = x.ToString(&lt;span style="color: #a31515"&gt;"hh:mm tt"&lt;/span&gt;),&lt;br /&gt;        Selected = x.Equals(referenceDate)&lt;br /&gt;    }).ToList();&lt;br /&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The first block of code creates a list of datetimes starting at midnight on the current date, every 15 minutes until 11:45pm on the same day.&amp;nbsp; The second block converts these DateTimes to a List&amp;lt;SelectListItem&amp;gt; using Linq.&amp;nbsp; Here's a simple example, creating a list of month names.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; months = 1.CreateSequence(m =&amp;gt; m + 1, m =&amp;gt; m &amp;gt;= 12)&lt;br /&gt;    .Select(i =&amp;gt; System.Threading.&lt;span style="color: #2b91af"&gt;Thread&lt;br /&gt;        &lt;/span&gt;.CurrentThread.CurrentCulture&lt;br /&gt;        .DateTimeFormat.GetMonthName(i)&lt;br /&gt;    ).ToList();&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;Screen shots:&lt;/h2&gt;
&lt;p&gt;In the example above, we get month names in a sequence ("January", "Februrary", "March"...).&amp;nbsp; Below are some of these examples in action in an ASP.NET MVC page.&amp;nbsp; Notice that Months and Times are in ascending order and Years are in descending order.&amp;nbsp; They were not sorted in this way.&amp;nbsp; They were generated in those orders by either incrementing or decrementing the sequence seed.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceTime_5F00_2.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="SequenceTime" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceTime_5F00_thumb.png" border="0" width="165" height="305" /&gt;&lt;/a&gt; &lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceMonth.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="SequenceMonth" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceMonth_5F00_thumb.png" border="0" width="166" height="196" /&gt;&lt;/a&gt; &lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceYear.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="SequenceYear" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/SequenceYear_5F00_thumb.png" border="0" width="149" height="115" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;So where does this leave sets?&amp;nbsp; &lt;/h2&gt;
&lt;p&gt;Additional extension methods and classes can be used to treat these sequences and native Enums as sets.&amp;nbsp; Watch for more details in another blog post.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=768" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Universal method chaining in C# 3.5</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/04/07/universal-method-chaining-in-c-3-5.aspx</link><pubDate>Tue, 07 Apr 2009 23:32:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:581</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=581</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/04/07/universal-method-chaining-in-c-3-5.aspx#comments</comments><description>&lt;p&gt;&lt;img height="100" width="103" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Chain-Small_5F00_2.jpg" align="left" alt="Chain Small" border="0" style="border-right-width: 0px; margin: 10px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" /&gt;&lt;/p&gt;
&lt;p&gt;04/07/2009&lt;/p&gt;
&lt;p&gt;Phil Gilmore&lt;/p&gt;
&lt;p&gt;Here I will present a solution that provides method chaining in C# 3.5.&amp;nbsp; I hope it helps you and I hope that we will see it added as part of the next update to the .NET.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Is it really a big deal?&lt;/h2&gt;
&lt;p&gt;I've been working more and more with jQuery and have become accustomed to the convenience of method chaining.&amp;nbsp; But when I come back to C# I find that I'm annoyed that method chaining seems to have been left out of the picture, even as many of the newer extension methods were implemented in C# 3.5.&amp;nbsp; It is especially noteworthy that so many of the methods in the IEnumerable&amp;lt;&amp;gt; and List&amp;lt;&amp;gt; classes return void.&amp;nbsp; I have always wanted to do method chaining on the Sort() method for example.&amp;nbsp; Without chaining, a method requires a variable to hold the reference and in many situations this can be a hassle.&amp;nbsp; Here is a useless example of the standard means of using some of these methods:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Standard syntax:&lt;/h2&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;&lt;/span&gt; &lt;/pre&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; myList = (&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 5, 4, 3, 2, 1 }).ToList();
myList.AddRange(&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 6, 7, 8, 9, 10 });
myList.Sort();
myList.Reverse();
myList.ForEach(i =&amp;gt; i + 1);
myList.Insert(0, 0);

&lt;span style="color: blue"&gt;return &lt;/span&gt;myList;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can see in the example above that every operation must be done independently and that a variable declaration is required when chaining isn't allowed.&amp;nbsp; Given the flow of chainable extension methods introduced in .Net 3.5, (take the ToList() method, for example), I would like all methods of the List&amp;lt;&amp;gt; class to be chainable.&amp;nbsp; Then my could would look like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Ideal syntax:&lt;/h2&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 5, 4, 3, 2, 1 })
    .ToList()
    .AddRange(&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 6, 7, 8, 9, 10 })
    .Sort()
    .Reverse()
    .ForEach(i =&amp;gt; i + 1)
    .Insert(0, 0);
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Wouldn't that be cool?&amp;nbsp; We can make this possible using extension methods.&amp;nbsp; The problem with this is that you have to write an extension method for every method of every class you want to make chainable.&amp;nbsp; Well, I decided to start with a few of the more useful methods of the List&amp;lt;&amp;gt; class.&amp;nbsp; I quickly realized that they all looked the same and some of the code could be further generalized and reused.&amp;nbsp; Before long, I had a single extension method that all of my extension methods were calling.&amp;nbsp; Here it is:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;The Chain method:&lt;/h2&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;T Chain&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T source, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; operation) &lt;span style="color: blue"&gt;where &lt;/span&gt;T: &lt;span style="color: blue"&gt;class
&lt;/span&gt;{
    operation(source);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;source;
}
&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Simple enough.&amp;nbsp; Remember to put this into a STATIC class as is required for every extension method.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So my Sort() method can now return the result from an internal call to this Chain&amp;lt;&amp;gt; method and the consuming code could look just like the hypothetical code I presented above.&amp;nbsp; At this point, I have written all the extension methods that I expected to write.&amp;nbsp; But given the way the implementation worked out, I can now call ANY method on ANY object as a chained method with just a little effort.&amp;nbsp; This was an unexpected side effect.&amp;nbsp; Anyone who has written a few extension methods probably knows that you could go on forever writing extension methods for every type to do any number of things that you may or may not ever use and still you would never run out of methods to write.&amp;nbsp; Since I can chain anything with this single method, I no longer have to worry about thinking of every contingency I may need to add to the List&amp;lt;&amp;gt; class.&amp;nbsp; For example.&amp;nbsp; If someone else writes an extension method to encrypt list data, for example, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; data = LoadStringsFromDatabase().ToList();
data.Encrypt();
&lt;span style="color: blue"&gt;return &lt;/span&gt;data;
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I would normally have to write an extension method to make it chainable if the method returns void.&amp;nbsp; But this is not necessary if you use the Chain&amp;lt;&amp;gt; method.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;LoadStringsFromDatabase().ToList().Chain(s =&amp;gt; s.Encrypt());&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;While a tad more verbose than ideal, it will work under any circumstance with any void method of any object type.&amp;nbsp; So using just the Chain method, let's take another crack at that first example:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Much better:&lt;/h2&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 5, 4, 3, 2, 1 })
    .ToList()
    .Chain(s =&amp;gt; s.AddRange(&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 6, 7, 8, 9, 10 }))
    .Chain(s =&amp;gt; s.Sort())
    .Chain(s =&amp;gt; s.Reverse())
    .Chain(s =&amp;gt; s.ForEach(i =&amp;gt; i + 1))
    .Chain(s =&amp;gt; s.Insert(0, 0));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is not far off from what I wanted.&amp;nbsp; As before, it is only slightly more verbose than using method-specific chaining extension methods, but not much.&amp;nbsp; You will seldom chain so many things or perform such a mundane operation as my example here, but I have found it very useful in common situations, such as ...&lt;/p&gt;
&lt;h3&gt;Inserting an element into a list at index 0 inline:&lt;/h3&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;drpState.DataSource = Data.GetStates()
    .Chain(lst =&amp;gt; lst.Sort())
    .Chain(lst =&amp;gt; lst.Insert(0, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;State&lt;/span&gt;(&lt;span style="color: #a31515"&gt;" --- "&lt;/span&gt;)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Operating on collections of composite anonymous types:&lt;/h3&gt;
&lt;p&gt;This is useful because you can't reasonably declare a variable or list variable of anonymous types.&amp;nbsp; I know this looks like a bad example because it could be done using Linq-To-Sql, but this is a fair example because TDS has a 2100 element limit which the .Contains() clause can violate if not handled carefully and this is one solution that may be used:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; idList = (
    &lt;span style="color: blue"&gt;from &lt;/span&gt;od &lt;span style="color: blue"&gt;in &lt;/span&gt;db.OrderDetails
    &lt;span style="color: blue"&gt;where &lt;/span&gt;o.OrderID.Equals(orderID)
    &lt;span style="color: blue"&gt;select new &lt;/span&gt;{ ID = od.OrderDetailID, Name = od.Name, Number = od.DetailNumber }
).ToList().Chain(
    lst =&amp;gt; lst.RemoveAll(
        i =&amp;gt; subpoenaIDList.Contains(i)
    )
).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;().ToList();
&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Modifying properties of a data entity when passing it as a parameter:&lt;/h3&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;db.Orders.Add(myOrder.Chain(o =&amp;gt; o.LastUpdated = System.&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now));&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Target ambiguity:&lt;/h2&gt;
&lt;p&gt;An issue that deserves mention is target ambiguity.&amp;nbsp; There are two ways that chaining can be implemented.&amp;nbsp; The first is where the source is modified and then returned (such as is the case in my method), and the second is where the source is duplicated, modified, and the new copy is returned, leaving the original data intact.&amp;nbsp; Both can be implemented using the method I outlined in this post.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The important point to make is that it should be obvious to the user which behavior your chain method is using.&amp;nbsp; I have failed in this regard and encourage you to improve on the concepts I have outlined here.&lt;/p&gt;
&lt;p&gt;Many developers subscribe to a school of thought that one method should be used and the other should not.&amp;nbsp; Feel free to do it either way you like.&amp;nbsp; I personally prefer to return a copy rather than a modified original source and may yet modify my method to do so.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Phil Gilmore ( &lt;a href="http://www.interactiveasp.net"&gt;www.interactiveasp.net&lt;/a&gt; )&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=581" width="1" height="1"&gt;</description></item><item><title>Reusable Fixture in C#</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/03/12/reusable-fixture-in-c.aspx</link><pubDate>Thu, 12 Mar 2009 20:23:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:559</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=559</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/03/12/reusable-fixture-in-c.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/CSharp_5F00_GreenCloud_5F00_oil_5F00_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="114" alt="CSharp_GreenCloud_oil" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/CSharp_5F00_GreenCloud_5F00_oil_5F00_thumb.png" width="99" align="left" border="0" /&gt;&lt;/a&gt;Phil Gilmore&lt;/p&gt;
&lt;p&gt;I was recently working on an application that had had 2 problems.&amp;nbsp; I found that at one level the code repeats itself many times over.&amp;nbsp; Every method in the business layer used the same try / catch / catch structure to wrap calls to the data layer.&amp;nbsp; They were all 95% identical to each other.&amp;nbsp; In another level, some WCF connections were not being closed properly in all instances.&amp;nbsp; Both issues were fixture issues.&amp;nbsp; I found that I could fix them both with a minimum of code repetition&amp;nbsp; when using fixture.&amp;nbsp; Let me show some examples of what was being done with the exception handling structures.&amp;nbsp; There are methods that return a type and some return void.&amp;nbsp; I'll show both.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;ReturnType1 Method1(&lt;span style="color: blue"&gt;long &lt;/span&gt;barcode)
{
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        DataProvider dp = &lt;span style="color: blue"&gt;new &lt;/span&gt;DataProvider();
        &lt;span style="color: blue"&gt;return &lt;/span&gt;dp.Method1();
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ApplicationException &lt;/span&gt;aex)
    {
        &lt;span style="color: #2b91af"&gt;LogWriter&lt;/span&gt;.LogException(aex);
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(aex.Message);
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
    {
        &lt;span style="color: #2b91af"&gt;LogWriter&lt;/span&gt;.LogException(ex);
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"An unhandled server exception was encountered."&lt;/span&gt;);
    }
}

&lt;span style="color: blue"&gt;public void &lt;/span&gt;Method2(&lt;span style="color: blue"&gt;long &lt;/span&gt;id)
{
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        DataProvider dp = &lt;span style="color: blue"&gt;new &lt;/span&gt;DataProvider();
        dp.Method2(id);
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ApplicationException &lt;/span&gt;aex)
    {
        &lt;span style="color: #2b91af"&gt;LogWriter&lt;/span&gt;.LogException(aex);
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(aex.Message);
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
    {
        &lt;span style="color: #2b91af"&gt;LogWriter&lt;/span&gt;.LogException(ex);
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"An unhandled server exception was encountered."&lt;/span&gt;);
    }
}
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You see that in the two methods above, the entire method body is identical except for the content in the actual try { } block.&amp;nbsp; That much repetition is verbose, error-prone, harder to read and unnecessary.&amp;nbsp; It's easy to see how several WCF service calls would end up looking very repetetive too.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;CallWCFMethod()
{
    ServiceClient client = &lt;span style="color: blue"&gt;new &lt;/span&gt;ServiceClient();
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;try &lt;/span&gt;{ 
            client.Method1(); 
        } &lt;span style="color: blue"&gt;catch &lt;/span&gt;Exception(ex) {
            LogError(ex); 
        }
    }
    &lt;span style="color: blue"&gt;finally
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            client.Close();
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(client &lt;span style="color: blue"&gt;is &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDisposable&lt;/span&gt;)
                ((&lt;span style="color: #2b91af"&gt;IDisposable&lt;/span&gt;)client).Dispose();
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;{ Log(&lt;span style="color: #a31515"&gt;"WCF connection closed in a faulted state."&lt;/span&gt;); }
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I realize that many of you will want to argue that WCF client should be instantiated in a Using block.&amp;nbsp; Some others will say that the dispose method will call the Close() method.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;In the code above, the only line that differs between methods is the line that actually calls client.Method1();&amp;nbsp; All other lines would be present in every method that consume that WCF service.&amp;nbsp; There is a lot of wasted code here.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The common thread in these two scenarios is that the functional part of each method differs, but the fixture is the same.&amp;nbsp; The fixture is the code that runs before and after the functional code to set up in preparation for the call and to clean up after the call.&amp;nbsp; This can be done using a callback.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;To solve this, we need a method that contains the fixture, which we need only write once.&amp;nbsp; Inside the fixture, a callback method will be used to execute code you specify.&amp;nbsp; That code can be anything and is determined at the time of the call.&amp;nbsp; The fixture method doesn't have to know what it is.&amp;nbsp; Using generics and anonymous methods, this syntax is also reasonably readable.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Consider this solution for the first scenario, the exception handlers.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;TResult HandleExceptions&amp;lt;TResult&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TResult&amp;gt; operationCallback)
{
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;return &lt;/span&gt;operationCallback();
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ApplicationException &lt;/span&gt;aex)
    {
        &lt;span style="color: #2b91af"&gt;LogWriter&lt;/span&gt;.LogException(aex);
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(aex.Message);
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
    {
        &lt;span style="color: #2b91af"&gt;LogWriter&lt;/span&gt;.LogException(ex);
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"An unhandled server exception was encountered."&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Notice that the only parameter for this method is a callback (also known by many other names- function pointer, delegate, etc.).&amp;nbsp; The point is that you pass it in and it is called inside the try { } block.&amp;nbsp; To consume this fuxture method, you just pass your functional code (which used to be inside the try { }&amp;nbsp; block) as a parameter.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;ReturnType1 Method1(&lt;span style="color: blue"&gt;string &lt;/span&gt;itemCode)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;HandleExceptions(
        &lt;span style="color: blue"&gt;delegate&lt;/span&gt;()
        {
            DataProvider dp = &lt;span style="color: blue"&gt;new &lt;/span&gt;DataProvider();
            &lt;span style="color: blue"&gt;return &lt;/span&gt;dp.Method1;
        }
    );
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Method2(&lt;span style="color: blue"&gt;string &lt;/span&gt;itemCode)
{
    HandleExceptions(
        &lt;span style="color: blue"&gt;delegate&lt;/span&gt;()
        {
            DataProvider dp = &lt;span style="color: blue"&gt;new &lt;/span&gt;DataProvider();
            dp.Method2;
            &lt;span style="color: blue"&gt;return &lt;/span&gt;0;
        }
    );
}
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For those unfamiliar with this syntax, the delegate() { } part says that the code between curly braces { } is a method that will be passed as a parameter to HandleExceptions.&amp;nbsp; In a strict world, I would have to specify the return type as delegate&amp;lt;ReturnType1&amp;gt; or delegate&amp;lt;int&amp;gt;().&amp;nbsp; Likewise, the Method1&amp;lt;ReturnType&amp;gt; and Method2&amp;lt;int&amp;gt; declarations would be required with type parameters.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;As it is, however, our friend Type Inference determines what those type parameters should be and renders them optional.&amp;nbsp; This is not only nice in a time-saving capacity, but in other situations may also lead to further code reuse.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is an example of a provision of reusable fixture for the aforementioned WCF call.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;TResult CallWCFMethod&amp;lt;TResult&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;ServiceClient, TResult&amp;gt; operationCallback)
{
    ServiceClient client = &lt;span style="color: blue"&gt;new &lt;/span&gt;ServiceClient;
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;try &lt;/span&gt;{ 
            &lt;span style="color: blue"&gt;return &lt;/span&gt;operationCallback(client);
        } &lt;span style="color: blue"&gt;catch &lt;/span&gt;Exception(ex) {
            LogError(ex); 
        }

    }
    &lt;span style="color: blue"&gt;finally
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            client.Close();
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(client &lt;span style="color: blue"&gt;is &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDisposable&lt;/span&gt;)
                ((&lt;span style="color: #2b91af"&gt;IDisposable&lt;/span&gt;)client).Dispose();
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;{ }
    }
}&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;All we've done is coded the input func&amp;lt;&amp;gt; parameter with the type of the parameter that will be passed to operationCallback, and then changed the functional try { } block to call the operationCallback function, passing the service client object as a parameter.&amp;nbsp; You can easily put any additional logging, setup or teardown requirements for all WCF calls in just one place.&amp;nbsp; It is now a simple matter to call any method in the WCF service without constantly opening and closing the connection each time, without the ridiculous exception handling that WCF calls require, and without incessant logging calls.&amp;nbsp; Subsequent calls are all very terse and easy to understand.&amp;nbsp; They look like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;WCFMethod1()
{
    CallWCFMethod(
        &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(ServiceClient client)
        {
            client.Method1();
            &lt;span style="color: blue"&gt;return &lt;/span&gt;0;
        }
    );
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Of course, the above example is for a void call.&amp;nbsp; Any call that actually returns something can have the return type inferred and returned as the result of the CallWCFMethod method as already shown in the exception handler example above and in the following example.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="code" style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; padding-left: 30px; margin: 10px;"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;ReturnType1 WCFMethod2()
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;CallWCFMethod(
        &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(ServiceClient client)
        {
            &lt;span style="color: blue"&gt;return &lt;/span&gt;client.Method2();
        }
    );
}&lt;/pre&gt;
&lt;div style="padding-left: 30px;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Phil Gilmore (&amp;nbsp;&lt;a href="http://www.interactiveasp.net"&gt;www.interactiveasp.net&lt;/a&gt;&amp;nbsp;)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=559" width="1" height="1"&gt;</description></item><item><title>Advanced merging - manual alignment with KDIFF3</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/02/23/advanced-merging-manual-alignment-with-kdiff3.aspx</link><pubDate>Mon, 23 Feb 2009 21:41:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:501</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=501</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/02/23/advanced-merging-manual-alignment-with-kdiff3.aspx#comments</comments><description>&lt;p&gt;Phil Gilmore&lt;/p&gt;
&lt;p&gt;Not all merge programs are equal.&amp;nbsp; Where possible, it's nice to have the merge program automatically resolve any conflicts that it can before forcing you to make any manual merge selections.&amp;nbsp; Of the tools that I've used, KDIFF3 always does the best automatic merge.&amp;nbsp; When auto merge fails, it's usually still very easy to select the desired merge source when resolving a conflict.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;On rare occasions, KDiff3 will match common lines in the first place where you wanted to match in the second place.&amp;nbsp; If it matches in a place other than where you want it to, you will need to do a manual alignment.&amp;nbsp; This is easy to do in 2-way diff operations, but can raise some questions when doing a 3-way merge.&amp;nbsp; Here I show you how to do it in an example of why you would want to do it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Sample Sources&lt;/h2&gt;
&lt;p&gt;Here are the sample sources I will use to demonstrate manual alignment.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Merge1.png"&gt;&lt;img border="0" width="224" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Merge1_5F00_thumb.png" alt="Merge1" height="210" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Merge2.png"&gt;&lt;img border="0" width="224" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Merge2_5F00_thumb.png" alt="Merge2" height="210" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Merge3.png"&gt;&lt;img border="0" width="224" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Merge3_5F00_thumb.png" alt="Merge3" height="210" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Load them up into KDIFF3.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeLoad.png"&gt;&lt;img border="0" width="482" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeLoad_5F00_thumb.png" alt="MergeLoad" height="247" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Initial alignments&lt;/h2&gt;
&lt;p&gt;You now see the initial alignments that KDIFF3 comes up with.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign1.png"&gt;&lt;img border="0" width="586" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign1_5F00_thumb.png" alt="MergeAlign1" height="473" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The first line that is common across all 3 sources is the line containing the number "1".&amp;nbsp; But what if we want it to match up lines starting with the letter "A" instead?&amp;nbsp; We have to tell it where to line up the 3 sources.&amp;nbsp; Do this with a manual alignment.&amp;nbsp; You can access it from the menu, but it's easier to do this with the hot key combination, CTRL-Y.&amp;nbsp; If you screw it up, just press CTRL-SHIFT-Y to reset your manual alignments and start over.&amp;nbsp; If you insist on using the menu, you can see the options for manual alignment in the screen shot below.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/ManualAlignment.png"&gt;&lt;img border="0" width="360" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/ManualAlignment_5F00_thumb.png" alt="ManualAlignment" height="213" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Manual alignments are done on 2 sources at a time.&amp;nbsp; This makes it confusing for a 3-way merge.&amp;nbsp; You actually achieve a 3-way alignment as a series of two 2-way alignments.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Sequence for a 2-way manual alignment:&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table border="0" width="554" cellpadding="2" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="26" valign="top"&gt;1&lt;/td&gt;
&lt;td width="526" valign="top"&gt;Click on the line to be aligned in the first source with your mouse.&amp;nbsp; You will not see it receive focus nor a cursor.&amp;nbsp; There will be no indication that the click took place.&amp;nbsp; Have faith that it did take place and move on to step 2.&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="26" valign="top"&gt;2&lt;/td&gt;
&lt;td width="526" valign="top"&gt;Press CTRL-Y to start the manual alignment.&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="26" valign="top"&gt;3&lt;/td&gt;
&lt;td width="526" valign="top"&gt;Click on the line to be aligned in the second source with your mouse.&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="26" valign="top"&gt;4&lt;/td&gt;
&lt;td width="526" valign="top"&gt;Press CTRL-Y to complete the manual alignment.&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;First manual alignment&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Warning:&amp;nbsp; If you wish to save the merge result when finished, you should start a merge before adding manual alignments.&amp;nbsp; Starting a merge will reset any manual alignments.&amp;nbsp; I do not show a merge in these screen shots for the sake of keeping the images as small as I can&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is the first manual alignment.&amp;nbsp; Starting from KDiff's Initial alignment (right after loading the sources), click on the first line, annotated with a "1" in bright red.&amp;nbsp; Press CTRL-Y then click the second source on the line annotated with a "2" in bright red.&amp;nbsp; Press CTRL-Y to complete the alignment.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign1Annotated.png"&gt;&lt;img border="0" width="586" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign1Annotated_5F00_thumb.png" alt="MergeAlign1Annotated" height="473" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;After the completion of the operation, you see the alignment between the first and second sources.&amp;nbsp; The "A" on the first two sources are aligned on the same line.&amp;nbsp; The third source has been realigned with the other sources where possible.&amp;nbsp; The first line is the "1" line.&amp;nbsp; It cannot align with the "1" line in sources 1 and 2 because it would violate the manual alignment to line up all 3.&amp;nbsp; The same applies to the "2" and "3" lines.&amp;nbsp; The first line that can be aligned across all 3 sources is the "4" line.&amp;nbsp; Lines "5" and "6" follow suit and you are left with what you see here.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign2.png"&gt;&lt;img border="0" width="586" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign2_5F00_thumb.png" alt="MergeAlign2" height="473" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Second manual alignment&lt;/p&gt;
&lt;p&gt;Below are the alignment points for the second alignment.&amp;nbsp; Hopefully, at this point you understand the process well enough that you didn't need this screen shot to tell you what's next.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign2Annotated.png"&gt;&lt;img border="0" width="586" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeAlign2Annotated_5F00_thumb.png" alt="MergeAlign2Annotated" height="473" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The second alignment brings all the "A" lines together.&amp;nbsp; Below is the final result.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeFinal_5F00_2.png"&gt;&lt;img border="0" width="579" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MergeFinal_5F00_thumb.png" alt="MergeFinal" height="610" style="border-right: 0px; border-top: 0px; margin: 10px; border-left: 0px; border-bottom: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Remember that if you are doing a merge and you did not start the merge before the alignments, starting a merge at this point will reset your manual alignments.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Happy merging.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Phil Gilmore (&lt;a target="_blank" href="http://interactiveasp.net/blogs/spgilmore/default.aspx"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=501" width="1" height="1"&gt;</description></item><item><title>Lambda expressions tutorial for C# and Visual Studio 2008</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/01/09/lambda-expressions-tutorial-for-c-and-visual-studio-2008.aspx</link><pubDate>Sat, 10 Jan 2009 03:31:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:366</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=366</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/01/09/lambda-expressions-tutorial-for-c-and-visual-studio-2008.aspx#comments</comments><description>&lt;p&gt;&lt;img border="0" align="left" width="99" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/CSharp_5F00_VisualStudio_5F00_3.png" alt="CSharp_VisualStudio" height="114" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 20px; border-right-width: 0px" /&gt; Phil Gilmore&lt;/p&gt;
&lt;p&gt;Lambda expressions are one of the cool new features of .NET 3.5 and are available in the C# compiler inside Visual Studio 2008.&amp;nbsp; They can make code more readable and permit the framework to provide some really neat functionality when mixed with other .NET features like generics and anonymous methods.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Lambda expressions look strange at first, though, and are often hard for newcomers to understand.&amp;nbsp; I saw several presentations of lambda expressions before I saw Nathan Zaugg's presentation which finally got through to me.&amp;nbsp; I will attempt to describe them the way he described them in hopes that many more developers can benefit from this concept.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I will start with a description of lambda expressions and an example of how to compose one.&amp;nbsp; I will then provide some examples of evolution of the C# language to demonstrate how we came to lambda expressions and thus show how you to make the transition from using the old methods to using lambda expressions.&lt;/p&gt;
&lt;p&gt;A lambda expression is a shorthand syntax for an anonymous method.&lt;/p&gt;
&lt;p&gt;An anonymous method is a method body that is defined in place, rather than given a name and called by name elsewhere.&lt;/p&gt;
&lt;p&gt;Here is are some lambda expressions:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;i =&amp;gt; i * i&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;pre class="code"&gt;(i1, i2) =&amp;gt; i1 + i2&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;pre class="code"&gt;parm =&amp;gt; &lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(
    &lt;span style="color: #a31515"&gt;"Do you want to save the file: " &lt;/span&gt;+ parm + &lt;span style="color: #a31515"&gt;"?"&lt;/span&gt;, 
    &lt;span style="color: #a31515"&gt;"Confirm file save"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;MessageBoxButtons&lt;/span&gt;.YesNo)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;In the above expressions, the presence of the =&amp;gt; operator makes it a lambda expression.&amp;nbsp; On the left side of the expression is a parameter list.&amp;nbsp; On the right side is an expression to be evaluated.&amp;nbsp; The expression may contain the parameters that were defined on the other side of the =&amp;gt; operator.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;In essence, lambda expressions define a function's parameters and its body.&amp;nbsp; It has the limitation that the body must be a single expression which returns a single object.&amp;nbsp; They can be used anywhere a delegate would be used.&amp;nbsp; They are callback methods.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is a demonstration of the evolution of C# from callbacks to lambda expressions.&amp;nbsp; In these demonstrations, I will define the following code, which I will reuse in each example:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public delegate int &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IntCallback&lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;parameter1, &lt;span style="color: blue"&gt;int &lt;/span&gt;parameter2);&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected int &lt;/span&gt;AddIntegers(&lt;span style="color: blue"&gt;int &lt;/span&gt;value1, &lt;span style="color: blue"&gt;int &lt;/span&gt;value2)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;value1 + value2;
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;First, I declare a delegate which takes two integer parameters and returns an integer.&amp;nbsp; Then I define a method that matches this delegate signature, AddIntegers().&amp;nbsp; I go on to define a method that uses such a callback.&amp;nbsp; Any code that calls this method must pass a callback of the IntCallback delegate type.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected int &lt;/span&gt;CalculateSum(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; values, &lt;span style="color: #2b91af"&gt;IntCallback &lt;/span&gt;callback)
{
    &lt;span style="color: blue"&gt;int &lt;/span&gt;result = 0;
    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;value &lt;span style="color: blue"&gt;in &lt;/span&gt;values)
        result callback(result, value);
    
    &lt;span style="color: blue"&gt;return &lt;/span&gt;result;
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Finally, I declare a small integer sequence for use in the demonstrations.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; intData = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(
    &lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I will start with a traditional callback.&amp;nbsp; This passes a delegate instance of AddIntegers as the callback to CalculateSum.&amp;nbsp; CalculateSum will repeatedly call AddIntegers and return the sum of all numbers in the intData sequence.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;IntCallback &lt;/span&gt;callback = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IntCallback&lt;/span&gt;(AddIntegers);
&lt;span style="color: blue"&gt;int &lt;/span&gt;total = CalculateSum(intData, callback);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In recent versions of Visual Studio, you do not have to invoke the constructor of the delegate type to create an instance of it when passing as a method parameter.&amp;nbsp; The compiler is smart enough to instantiate it based on the prototype of the parameter in question.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;total = CalculateSum(intData, AddIntegers);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In Visual Studio 2005, Anonymous methods were introduced, allowing you to declare the callback method body in place.&amp;nbsp; Using anonymous methods, the AddIntegers method is no longer used.&amp;nbsp; Instead, its body is declared in place.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;total = CalculateSum(intData, 
    &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;value1, &lt;span style="color: blue"&gt;int &lt;/span&gt;value2) 
    { 
        &lt;span style="color: blue"&gt;return &lt;/span&gt;value1 + value2; 
    }
);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using the delegate keyword here tells the compiler that a parameter list and method body follow.&amp;nbsp; The parameter list and return type of this method must match the signature of the delegate for this parameter (IntCallback), or you will get a compiler error.&amp;nbsp; Notice that this syntax is identical to a normal method except for the return type and the method name are missing.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The next evolution was a lambda expression, which is a shorthand notation for the anonymous delegate shown above.&amp;nbsp; Let's analyze the anonymous method and convert it to a lambda expression.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;delegate&lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;value1, &lt;span style="color: blue"&gt;int &lt;/span&gt;value2) 
{ 
    &lt;span style="color: blue"&gt;return &lt;/span&gt;value1 + value2; 
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the above code, the parameter list is:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;value1, &lt;span style="color: blue"&gt;int &lt;/span&gt;value2) &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This becomes the left side of our lambda expression.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;(value1, value2) =&amp;gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The types are usually omitted.&amp;nbsp; This is because as the lambda expression is passed as a parameter whose type is a delegate, the parameter is already defined as a method with certain parameters.&amp;nbsp; In this case, the parameter is an IntCallback, and IntCallback defines a parameter list of (int value1, int value2).&amp;nbsp; Given that, the compiler uses type inference to determine the types of the parameters in the lambda expression.&amp;nbsp; The return type is dermined in the same way.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Next we extract the method body.&amp;nbsp; This is the right-hand side of the lambda expression.&amp;nbsp; This is sometimes called the predicate.&amp;nbsp; This should not be confused with any parameter whose name is predicate.&amp;nbsp; In such instances, a complete lambda expression is expected.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is the method body, extracted from the anonymous method.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;value1 + value2; &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Given that, we can complete our lambda expression.&amp;nbsp; Remember that the predicate is a single expression rather than a multitude of instructions.&amp;nbsp; Hence, we do not use the return keyword.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;(value1, value2) =&amp;gt; value1 + value2&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When this method is passed to CalculateSum, CalculateSum will call the the method which was passed in as parameter callback.&amp;nbsp; This passes the value1 and value2 parameters to our lambda expression, which plugs them in and evaluates the expression value1 + value2 and returns the result to the caller (CalculateSum).&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;pre style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; padding-left: 30px; margin: 10px;" class="code"&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;total = CalculateSum(intData,
    (value1, value2) =&amp;gt; value1 + value2
);&lt;/pre&gt;
&lt;div style="padding-left: 30px;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;Phil Gilmore (&lt;a target="_blank" href="http://interactiveasp.net/blogs/spgilmore/default.aspx"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: #0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=366" width="1" height="1"&gt;</description></item><item><title>Regular expression for email addresses (replacing the Visual Studio 2008 default).</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/01/09/regular-expression-for-email-addresses-replacing-the-visual-studio-2008-default.aspx</link><pubDate>Fri, 09 Jan 2009 18:19:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:363</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=363</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/01/09/regular-expression-for-email-addresses-replacing-the-visual-studio-2008-default.aspx#comments</comments><description>&lt;p&gt;&lt;img border="0" width="416" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Email-validation-regular-expression_5F00_1.png" alt="Email validation regular expression" height="54" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" /&gt; &lt;/p&gt;
&lt;p&gt;Phil Gilmore&lt;/p&gt;
&lt;p&gt;I recently had a user report that they couldn't register on my client's site.&amp;nbsp; The site's registration page reported that their email address was invalid.&amp;nbsp; The email address was indeed strange but valid nonetheless.&amp;nbsp; We were using a regular expression validator control to check the email address on registration.&amp;nbsp; The problem is that the regular expression that Visual Studio designer put in there for email addresses doesn't recognize it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is the regular expression that the Visual Studio 2008 designer conveniently put into the page on our behalf.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This has the following problems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Rules only apply at a single point in the input.&amp;nbsp; Any character can preceed or follow any valid email address including spaces, punctuation characters, periods, @ symbols, etc.&amp;nbsp; For example, the input &lt;em&gt;~!@#$%^&amp;amp;*()_+=-[]{}';":/.,?&amp;gt;&amp;lt; &lt;/em&gt;&lt;a href="mailto:nobody@nowhere.com~!&amp;nbsp;@#$%^&amp;amp;*()_+=-[]{}';&amp;quot;:/.,?"&gt;&lt;em&gt;nobody@nowhere.com~! @#$%^&amp;amp;*()_+=-[]{}';":/.,?&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&amp;gt;&amp;lt;&lt;/em&gt; would pass this validation. 
&lt;/li&gt;
&lt;li&gt;User name may contain multiple consecutive periods.&amp;nbsp; For example, the input &lt;a href="mailto:nobody..lives........here@nowhere.com"&gt;nobody..lives........here@nowhere.com&lt;/a&gt; would pass this validation. 
&lt;/li&gt;
&lt;li&gt;Domain name may contain multiple consecutive periods after the first single period.&amp;nbsp; For example, the input &lt;a href="mailto:nobody@nowhere.co....uk"&gt;nobody@nowhere.co....uk&lt;/a&gt; would pass this validation. 
&lt;/li&gt;
&lt;li&gt;Many valid email addresses will not pass this validation.&amp;nbsp; For example, the input &lt;a href="mailto:nobody-@nowhere.com"&gt;nobody-@nowhere.com&lt;/a&gt; would fail.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Obviously, there are some flaws here.&amp;nbsp; If you are familiar with regular expressions, you can see immediately that it's missing the ^ and $ restrictions, for example.&amp;nbsp; Rather than try to massage this one into compliance, I started with a new one.&amp;nbsp; There are probably a million of these on the web and no doubt some are better than mine.&amp;nbsp; But I thought I'd blog it anyway since it's done and working.&amp;nbsp; Here is what I came up with.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;^([\w-_]+\.)*[\w-_]+@([\w-_]+\.)*[\w-_]+\.[\w-_]+$&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This regular expression has the following attributes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User name must be one character or more. 
&lt;/li&gt;
&lt;li&gt;User name may contain one ore more periods. 
&lt;/li&gt;
&lt;li&gt;User name must not begin or end with a period. 
&lt;/li&gt;
&lt;li&gt;Double contiguous periods are not allowed in the user name. 
&lt;/li&gt;
&lt;li&gt;User name must only contain characters a-z, A-Z, 0-9, hyphens, underscores and single periods. 
&lt;/li&gt;
&lt;li&gt;Domain name must be three characters or more. 
&lt;/li&gt;
&lt;li&gt;Domain name must contain one or more periods. 
&lt;/li&gt;
&lt;li&gt;Domain name may not begin or end with a period. 
&lt;/li&gt;
&lt;li&gt;Double contiguous periods are not allowed in the domain name. 
&lt;/li&gt;
&lt;li&gt;User name must only contain characters a-z, A-Z, 0-9, hyphens, underscores and single periods. 
&lt;/li&gt;
&lt;li&gt;And single @ symbol is required between the user name and the domain name. 
&lt;/li&gt;
&lt;li&gt;Allowed special characters (hyphen and underscore) are permitted at any frequency anywhere in the user name or domain name.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Of course, you may choose to add more allowed special characters.&amp;nbsp;&amp;nbsp; Although I've never seen one in an email address, you may need to allow the plus (+) character, for example.&amp;nbsp; This is easy.&amp;nbsp; Just change all instances of &lt;strong&gt;\w&lt;/strong&gt; to &lt;strong&gt;\w\+&lt;/strong&gt; and it will permit it.&lt;/p&gt;
&lt;p&gt;I tested this regular expression against all the email addresses in the client's user database (about 5500 addresses).&amp;nbsp; All email addresses that failed were either obviously invalid or outright blank (imported from another process, never validated against a regular expression).&amp;nbsp; Out of 5500 email addresses, only 9 failed which weren't blank and they were all invalid.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Phil Gilmore (&lt;a target="_blank" href="http://interactiveasp.net/blogs/spgilmore/default.aspx"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=363" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>FreePascal Error: Illegal parameter: -Opentium3</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2009/01/05/freepascal-error-illegal-parameter-opentium3.aspx</link><pubDate>Mon, 05 Jan 2009 18:44:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:360</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=360</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2009/01/05/freepascal-error-illegal-parameter-opentium3.aspx#comments</comments><description>&lt;p&gt;&lt;a target="_blank" href="http://www.freepascal.org/" title="FreePascal"&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/Cheetah_5F00_1.png" alt="FreePascal" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="139" align="left" border="0" height="84" /&gt;&lt;/a&gt;Phil Gilmore&lt;/p&gt;
&lt;p&gt;I was trying to build an existing Delphi DLL as a 64-bit DLL using FreePascal 2.2.2.&amp;nbsp; I opened the .dpr file which is as simple as a DLL project can get.&amp;nbsp; I clicked &lt;i&gt;Compile&lt;/i&gt;, then &lt;i&gt;Build&lt;/i&gt;.&amp;nbsp; The build failed on line 0 with the error "Illegal parameter: -Opentium3".&amp;nbsp; This is frustrating to see on a fresh installation with a simple project (simple on the level of Hello-World).&amp;nbsp; I dug just a bit and found a bug tracker entry for it which provides some insight, but didn't really provide a resolution.&amp;nbsp; I have gleaned the resolution from the bug entry and provide a walk-through here.&lt;/p&gt;
&lt;p&gt;Incidentally, if you try to build twice, the second and subsequent times will return a different error: "Unable to open file C:\...\fp.cfg".&amp;nbsp; This is because the first error interrupted the program and left the cfg file open.&amp;nbsp; You will have to close the IDE to release it.&amp;nbsp; Do not edit the cfg file as it states.&amp;nbsp; It is generated every time you build from the IDE.&lt;/p&gt;
&lt;p&gt;Here is the bug entry:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://bugs.freepascal.org/view.php?id=11865"&gt;http://bugs.freepascal.org/view.php?id=11865&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As Marco van de Voort pointed out on that page, changing the -Opentium3 option to contain a double "p" will fix this issue.&amp;nbsp; It also shows that the problem has been resolved in the next version, 2.2.3 (which is not the current stable release as of this writing).&amp;nbsp; It also indicates that the problem was fixed in the previous version, but the fix did not proliferate to version 2.2.2 (I have not double-checked this).&amp;nbsp; Lastly, it indicates that this problem exists when Win32 (Windows XP).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Steps to reproduce:&lt;/h2&gt;
&lt;p&gt;1) Open your FPC IDE.&amp;nbsp; Load a project to build.&lt;/p&gt;
&lt;p&gt;2) From the menu, click &lt;i&gt;Compile&lt;/i&gt;, then &lt;i&gt;Build&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/FPC-Build-Menu_5F00_1.png" alt="FPC Build Menu" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="271" border="0" height="151" /&gt; &lt;/p&gt;
&lt;p&gt;3) You should see the failure in the build status dialog.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/FPC-Build-status_5F00_1.png" alt="FPC Build status" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="453" border="0" height="137" /&gt; &lt;/p&gt;
&lt;p&gt;4) Clear the build status dialog and you can see the error in the messages frame:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/FPC-Compiler-Message_5F00_1.png" alt="FPC Compiler Message" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="646" border="0" height="88" /&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you run this more than once, you will instead see the message:&lt;/p&gt;
&lt;p&gt;"Unable to open file C:\...\fp.cfg"&lt;/p&gt;
&lt;p&gt;See notes above.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Steps to resolution:&lt;/h2&gt;
&lt;p&gt;1) Open your FPC IDE.&amp;nbsp; Load a project to build.&lt;/p&gt;
&lt;p&gt;2) From the menu, click &lt;i&gt;Options&lt;/i&gt;, then &lt;i&gt;Compiler...&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/FPC-Compiler-Options-Menu_5F00_1.png" alt="FPC Compiler Options Menu" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="185" border="0" height="211" /&gt; &lt;/p&gt;
&lt;p&gt;3) In the compiler options dialog, Find the &lt;i&gt;additional compiler args&lt;/i&gt; field at the bottom.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/FPC-Pentium3-setting_5F00_1.png" alt="FPC Pentium3 setting" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="611" border="0" height="291" /&gt; &lt;/p&gt;
&lt;p&gt;Change the -Opentium3 to -Oppentium3 (with two "p"s).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;3) Click the OK button to close the dialog.&lt;/p&gt;
&lt;p&gt;4) Close the IDE.&lt;/p&gt;
&lt;p&gt;5) Repeat the "steps to reproduce" steps 1 and 2.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;6) You should now see a successful build dialog.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/FPC-Build-succeeded_5F00_1.png" alt="FPC Build succeeded" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" width="451" border="0" height="137" /&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Happy coding.&lt;/p&gt;
&lt;p&gt;Phil Gilmore (&lt;a target="_blank" href="http://interactiveasp.net/blogs/spgilmore/default.aspx"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=360" width="1" height="1"&gt;</description></item><item><title>How to configure MKS to use KDiff3</title><link>http://interactiveasp.net/blogs/spgilmore/archive/2008/11/07/how-to-configure-mks-to-use-kdiff3.aspx</link><pubDate>Fri, 07 Nov 2008 22:44:00 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:141</guid><dc:creator>Phil Gilmore</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/spgilmore/rsscomments.aspx?PostID=141</wfw:commentRss><comments>http://interactiveasp.net/blogs/spgilmore/archive/2008/11/07/how-to-configure-mks-to-use-kdiff3.aspx#comments</comments><description>&lt;p&gt;&lt;img border="0" align="left" width="84" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/mks_5F00_kdiff_5F00_final_5F00_1.png" alt="mks_kdiff_final" height="84" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px; border-right-width: 0px" /&gt;Phil Gilmore&lt;/p&gt;
&lt;p&gt;One of my clients is using MKS for source control and task management.&amp;nbsp; I have configured the source control system on my workstation to use KDiff3 for diff and merge operations.&amp;nbsp; I couldn't find any information on the command line to use and could find very little information on the parameters that are made available by MKS to pass to third party tools.&lt;/p&gt;
&lt;p&gt;It took some effort to figure this out and I could find very little information on how to configure MKS to use third party tools so I decided to share this information here.&amp;nbsp; I think it might also be useful to show all this in case anyone wants to repeat the process to configure some other third party tool to integrate with MKS, such as WinDiff or WinMerge.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can find information about configuring KDiff3 here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://kdiff3.sourceforge.net/doc/documentation.html#id2488514"&gt;http://kdiff3.sourceforge.net/doc/documentation.html#id2488514&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is a snippet of the pertinent part:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;To see all available command line options type&lt;/span&gt;&lt;/p&gt;
&lt;pre class="screen"&gt;&lt;span style="font-size: x-small;"&gt;&lt;font face="courier new"&gt;&lt;span xmlns:doc="http://nwalsh.com/xsl/documentation/1.0" class="command"&gt;&lt;span&gt;kdiff3&lt;/span&gt;&lt;/span&gt; --help
&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;Example output:&lt;/span&gt;&lt;/p&gt;
&lt;pre class="screen"&gt;&lt;span style="font-size: x-small;"&gt;Options:
  -m, --merge               Merge the input.
  -b, --base file           Explicit base file. For compatibility with certain tools.
  -o, --output file         Output file. Implies -m. E.g.: -o newfile.txt
  --out file                Output file, again. (For compatibility with certain tools.)
  --auto                    No GUI if all conflicts are auto-solvable. (Needs -o file)
  --qall                    Don't solve conflicts automatically. (For compatibility...)
  --L1 alias1               Visible name replacement for input file 1 (base).
  --L2 alias2               Visible name replacement for input file 2.
  --L3 alias3               Visible name replacement for input file 3.
  -L, --fname alias         Alternative visible name replacement. Supply this once for every input.
  --cs string               Override a config setting. Use once for every setting. E.g.: --cs "AutoAdvance=1"
  --confighelp              Show list of config settings and current values.
  --config file             Use a different config file.&lt;/span&gt;
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Given this, you need only know which MKS variables to substitute for each of the KDiff3 parameters from the table above when you construct your command line to the third party tool.&amp;nbsp; This is the tricky part.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;To specify the command line, open MKS and click File -&amp;gt; Edit preferences, then traverse the navigation tree to the /MKS Source/Diff and Merge Tools node.&amp;nbsp; On that page, you can specify the command line for your preferred diff tool and your preferred 3-way merge tool.&amp;nbsp; There are predefined configurations for several popular tools to choose from here (AraxisMerge and Beyond Compare, for example).&lt;/p&gt;
&lt;p&gt;The only documentation I found about MKS command-line variables is in the context-sensitive help (click the Help button).&amp;nbsp; Here is what it says.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;You can supply the following arguments for the command to control how the third party tool operates:&lt;br /&gt;&amp;diams; {0} to display the title of the common ancestor object of the files being merged. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {1} to display the title of the file being merged from. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {2} to display the title of the file being merged to. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {3} to display the title of the working file. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {4} the path to the common ancestor object of the files being merged. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {5} the path to the file being merged from. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {6} the path to the file being merged to. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {7} the path to the working file. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&amp;diams; {8} the path to the output file. You can use this argument to save the merge results in a file other than the working file. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;Each argument must be quoted. For example, the command line for the ABC Merge tool could be:&lt;br /&gt;abcmerge.exe "{4}" "{7}" "{5}"&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This information raises several questions.&amp;nbsp; For example, what is the difference between parameters 6 and 8?&amp;nbsp; Aren't both of them a target file to contain the merge results?&amp;nbsp; Which one should one use?&amp;nbsp; The same question arises between 5 and 7.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;To determine which parameters I wanted to use, I had to determine what their values would be and how MKS would behave when writing the merge results to various targets.&amp;nbsp; To do this, I wrote a simple program to show me the values that are passed to its command line.&amp;nbsp; I can plug this program in where I would normally use a shelled program such as a diff or merge tool.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Here is the (Delphi) source for it:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;program CommandLineInfo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;{$APPTYPE CONSOLE}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;uses&lt;br /&gt;&amp;nbsp; SysUtils,&lt;br /&gt;&amp;nbsp; Windows,&lt;br /&gt;&amp;nbsp; Classes;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;var&lt;br /&gt;&amp;nbsp; j: integer;&lt;br /&gt;&amp;nbsp; pc: integer;&lt;br /&gt;&amp;nbsp; output: TStringList;&lt;br /&gt;begin&lt;br /&gt;&amp;nbsp; output := TStringList.Create;&lt;br /&gt;&amp;nbsp; try&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pc := ParamCount;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output.Add(paramstr(0));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output.Add('');&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for j := 1 to pc do&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output.Add(IntToStr(j) + ': ' + ParamStr(j));&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageBox(0, pchar(output.Text), pchar('Command-line info'), MB_OK);&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;&amp;nbsp; finally&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output.Free;&lt;br /&gt;&amp;nbsp; end;&lt;br /&gt;end.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you're one of the unfortunate souls who does not use Delphi, download the binary of the aforementioned CommandLineInfo application &lt;a target="_blank" href="http://interactiveasp.net/media/p/133/download.aspx"&gt;here&lt;/a&gt;.&amp;nbsp; You can run the application without any installer prerequisites.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;When plugged in with this command line:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;C:\Archive\Apps\CommandLineInfo.exe "{0}" "{1}" "{2}" "{3}" "{4}" "{5}" "{6}" "{7}" "{8}"&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I get these results:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" width="484" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/CommandLineInfo_5F00_final_5F00_1.png" alt="Command-line info output" height="156" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" /&gt; &lt;/p&gt;
&lt;p&gt;I found that if you do not save the merge result to the working file, MKS assumes that you did not save your changes (it tells you that the working file was not modified).&amp;nbsp; Hence your merge results must be written to the working file.&amp;nbsp; You cannot see it in the screen shot above because I have blurred them out, but item 7 and item 8 (seen above as 8: and 9:) have identical values.&amp;nbsp; They are both pointing to the working file.&amp;nbsp; This way, the merge result is saved in the working file.&amp;nbsp; If the blurs above hinder your purposes, you can retrace the steps I've outlined in this article to see the results for yourself.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Ultimately, these are the command line entries that I constructed.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Diff:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;C:\Program Files\KDiff3\kdiff3.exe "{3}" "{4}" --L1 "{1}" --L2 "{2}"&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;3-Way Merge:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new'; font-size: x-small;"&gt;C:\Program Files\KDiff3\kdiff3.exe "{4}" "{5}" "{7}" -o "{8}" --L1 "{0}" --L2 "{1}" --L3 "{3}"&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;img border="0" width="484" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/spgilmore/MKS_5F00_preferences_5F00_5.png" alt="Configuring the third party diff and merge tools.." height="331" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Phil Gilmore (&lt;a target="_blank" href="http://interactiveasp.net/blogs/spgilmore/default.aspx"&gt;www.interactiveasp.net&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=141" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/spgilmore/archive/tags/Source+Control/default.aspx">Source Control</category></item></channel></rss>