<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8757932313454988957</id><updated>2011-08-04T08:07:21.363-07:00</updated><category term='XML Node specific attribute'/><category term='DataGridView'/><category term='State'/><category term='Picturebox scrollbar'/><category term='localname'/><category term='nth node from a nodelist'/><category term='Remove Duplicate Spaces'/><category term='local-name'/><category term='Adapter'/><category term='Scrolling PictureBox'/><category term='Regular Expressions'/><category term='Template Method'/><category term='XPath Query'/><category term='MeasureItem'/><category term='Singleton'/><category term='different colored listitems'/><category term='ignore namespace'/><category term='XML'/><category term='Combobox'/><category term='Strategy'/><category term='Dynamic ComboBox'/><category term='DrawItem'/><category term='Facade'/><category term='XML Default Namespace'/><title type='text'>Build Better Code Easily - CodeEdifice.com</title><subtitle type='html'>ASP.NET VB.NET SQL Coding Samples</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-3808482022300937601</id><published>2010-11-04T13:09:00.000-07:00</published><updated>2010-11-04T13:17:40.289-07:00</updated><title type='text'>Get last modified stored procedures on SQL 2005 and above</title><content type='html'>If you need to quickly get the list of all last modified stored procedures and the body of the stored procedure to aid in a quick update deployment from pre staging to production environment, try this stored procedure.&lt;br /&gt;&lt;br /&gt;&lt;pre style='line-height:12px;'&gt;&lt;br /&gt;Create proc DevGetLastModifiedItems&lt;br /&gt;(&lt;br /&gt;  @AfterTimeStamp as datetime&lt;br /&gt;)&lt;br /&gt;as&lt;br /&gt;  declare varCur cursor for &lt;br /&gt;    Select sys.objects.name, modify_date,&lt;br /&gt;           sys.all_sql_modules.definition&lt;br /&gt;      from sys.objects&lt;br /&gt;           inner join sys.all_sql_modules on sys.objects.object_id = sys.all_sql_modules.object_id&lt;br /&gt;      where sys.objects.type='P'&lt;br /&gt;        and sys.objects.modify_date &gt;= @AfterTimeStamp&lt;br /&gt;        and sys.objects.name &lt;&gt; 'DevGetLastModifiedItems' -- Cannot see why this SP should ever turn up on a production server :)&lt;br /&gt;      order by modify_date desc&lt;br /&gt;  open varCur&lt;br /&gt;  declare @name as varchar(200),&lt;br /&gt;          @definition as varchar(max),&lt;br /&gt;          @modify_date as datetime&lt;br /&gt;  Fetch next from varCur into @name, @modify_date, @definition&lt;br /&gt;  while @@Fetch_status = 0&lt;br /&gt;  begin&lt;br /&gt;    Print '-- ' + @name + ' last modified on ' + convert(varchar(40), @modify_date, 9)&lt;br /&gt;    Print ''&lt;br /&gt;    Print @definition&lt;br /&gt;    Print 'go'&lt;br /&gt;    Print ''&lt;br /&gt;    Print ''&lt;br /&gt;    Fetch next from varCur into @name, @modify_date, @definition&lt;br /&gt;  end&lt;br /&gt;  close varCur&lt;br /&gt;  deallocate varCur&lt;br /&gt;go&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To get the complete list and body of the stored procedures that have been modified since a date say '2010-11-04', simply fire this on the Management Studio window and check out the contents of the messages window&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;exec DevGetLastModifiedItems '2010-11-04'&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-3808482022300937601?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/3808482022300937601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=3808482022300937601' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/3808482022300937601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/3808482022300937601'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2010/11/get-last-modified-stored-procedures-on.html' title='Get last modified stored procedures on SQL 2005 and above'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-4573959240732294836</id><published>2010-10-13T11:08:00.000-07:00</published><updated>2010-10-13T11:31:57.645-07:00</updated><title type='text'>Expose Child Control In A Custom User Control For Javascript</title><content type='html'>Hit upon a javascript requirement. I have two controls on my page - a textbox control and a custom user control. The custom user control itself hosts two textboxes. The requirement wanted me to set the value of the standalone textbox control on one of the two inside textbox controls. Came up with two solutions&lt;br /&gt;&lt;br /&gt;The unelegant one first - Add an attribute in the hosting page load to handle the blur event of the standalone textbox triggering  a __doPostBack. In the load event of the custom user control, adding code to use the FindControl function on the parent page to retrieve the standalone textbox's value and store this in the inside textbox.&lt;br /&gt;While this did the task, the actual postback of the page was a real spoiler (especially since my page had more than just these two items).&lt;br /&gt;&lt;br /&gt;Relooked at the same issue a while back and thought of another solution.&lt;br /&gt;An elegant one - Expose the inside textbox as a property of the usercontrol. In the hosting page, add an attribute to handle the blur event, this time however use a simple javascript to set the value of the textbox exposed as a property to the value of the standalone textbox.&lt;br /&gt;Consider the standalone textbox is called &lt;span style="color:#3333ff;"&gt;txt1&lt;/span&gt; and the usercontrol &lt;span style="color:#3333ff;"&gt;uc1&lt;/span&gt; has two textboxes - &lt;span style="color:#3333ff;"&gt;txt2&lt;/span&gt; and &lt;span style="color:#3333ff;"&gt;txt3&lt;/span&gt;. We want to set the value of &lt;span style="color:#3333ff;"&gt;txt3&lt;/span&gt; to be the same as &lt;span style="color:#3333ff;"&gt;txt1&lt;/span&gt;. We have now exposed &lt;span style="color:#3333ff;"&gt;txt3&lt;/span&gt; as a property of &lt;span style="color:#3333ff;"&gt;uc1&lt;/span&gt; say &lt;span style="color:#3333ff;"&gt;txt3TextBox&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In code,&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Dim blurJS As String = String.Format(document.getElementByID('{0}').value = document.getElementByID('{1}').value, uc1.txt3TextBox.ClientID, txt1.ClientID)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;txt1.Attributes.Add("OnBlur", blurJS)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;br /&gt;Well, I had more fun than I added here especially with the txt3 control being part of a view of the multiview and hence I needed to add more checks to ensure that the left hand side of the expression was not null and such. But that is another story.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-4573959240732294836?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/4573959240732294836/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=4573959240732294836' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/4573959240732294836'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/4573959240732294836'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2010/10/expose-child-control-in-custom-user.html' title='Expose Child Control In A Custom User Control For Javascript'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-2273494122878072897</id><published>2008-01-15T12:10:00.000-08:00</published><updated>2008-01-15T12:25:40.309-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ignore namespace'/><category scheme='http://www.blogger.com/atom/ns#' term='local-name'/><category scheme='http://www.blogger.com/atom/ns#' term='XPath Query'/><category scheme='http://www.blogger.com/atom/ns#' term='XML Default Namespace'/><category scheme='http://www.blogger.com/atom/ns#' term='localname'/><title type='text'>Is that default namespace in XML bothering you?</title><content type='html'>I had one of my dev team members walk up to me some days back with this really interesting requirement. He had these XML files that contained similar information except that the elements were in different namespaces on those files. If you find yourself caught up in a situation like this, probably this blog entry might be useful. Once again, I will show a few ways of getting this done, pick the right one based on your requirements.&lt;br /&gt;&lt;br /&gt;For starters, I will create an XML file that contains a Person node at the root and three nodes - &lt;em&gt;Name&lt;/em&gt; that lies in the &lt;strong&gt;n1&lt;/strong&gt; namespace, &lt;em&gt;Age&lt;/em&gt; that lies in the &lt;strong&gt;n2&lt;/strong&gt; namespace and Gender that is part of the default namespace. &lt;br /&gt;&lt;br /&gt;A listing of the Sample.xml file&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: maroon;"&gt;xml&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;version&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;1.0&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;encoding&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;utf-8&lt;/span&gt;"&lt;span style="color: blue;"&gt; ?&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Person&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns:n1&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;http://codeedifice.com/N1&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns:n2&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;http://codeedifice.com/N2&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;http://codeedifice.com/N&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;n1:Name&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Alvin&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;n1:Name&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;n2:Age&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;10&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;n2:Age&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Gender&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Male&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;Gender&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;Person&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The example that I am listing here lists the top three approaches taken by developers who are still getting their hands dirty with XML that contains namespaces. The next three approaches are alternatives that you must work out for yourself and pick the one that suits your actual requirement &lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xdoc &lt;span style="color: blue;"&gt;As&lt;/span&gt; XmlDocument = &lt;span style="color: blue;"&gt;New&lt;/span&gt; XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; xdoc.Load(&lt;span style="color: maroon;"&gt;"Sample.xml"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Fails due to lack of namespace&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Person Node(s) without a namespace manager..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst1 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/Person"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst1.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; nsMgr &lt;span style="color: blue;"&gt;As&lt;/span&gt; XmlNamespaceManager = &lt;span style="color: blue;"&gt;New&lt;/span&gt; XmlNamespaceManager(xdoc.NameTable)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; nsMgr.AddNamespace(&lt;span style="color: maroon;"&gt;"n1"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"http://codeedifice.com/N1"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; nsMgr.AddNamespace(&lt;span style="color: maroon;"&gt;"n2"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"http://codeedifice.com/N2"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Fails since the namespaces do not include the default one&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Person Node(s) with the namespace manager not including the default namespace..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst2 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/Person"&lt;/span&gt;, nsMgr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst1.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; nsMgr.AddNamespace(&lt;span style="color: maroon;"&gt;""&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"http://codeedifice.com/N"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Fails since the empty string namespace is not useful (quite contrary to popular vote)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Person Node(s) with the namespace manager including the default namespace as an empty string..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst3 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/Person"&lt;/span&gt;, nsMgr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst3.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; nsMgr.RemoveNamespace(&lt;span style="color: maroon;"&gt;""&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"http://codeedifice.com/N"&lt;/span&gt;)&amp;nbsp; &lt;span style="color: green;"&gt;'take the useless one out&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; nsMgr.AddNamespace(&lt;span style="color: maroon;"&gt;"ALVIN"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"http://codeedifice.com/N"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Finally a working solution&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Person Node(s) with the namespace manager including the default namespace as a unique value - ALVIN..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst4 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/ALVIN:Person"&lt;/span&gt;, nsMgr)&amp;nbsp; &lt;span style="color: green;"&gt;'Note how the dummy namespace is used&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst4.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'A slightly more elegant solution that does not rely on the unique value ALVIN to be created by the developer &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Person Node(s) without the namespace manager using the localName XPath function..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst5 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/*[local-name()='Person']"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst5.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Accessing the Name and Age nodes correctly will require the use of the namespace&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Name Node(s) with the namespace manager using the localName XPath function..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst6 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/*[local-name()='Person']/n1:Name"&lt;/span&gt;, nsMgr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst6.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Accessing the Name and Age nodes in a namespace agnostic fashion&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'This implies that you are assuming that you do not care about the namespace and can be dangerous&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Looking for Name Node(s) without the namespace manager using the localName XPath function..."&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst7 &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xdoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/*[local-name()='Person']/*[local-name()='Name']"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(Space(4) &amp;amp; &lt;span style="color: maroon;"&gt;"Found "&lt;/span&gt; &amp;amp; xNodLst7.Count &amp;amp; &lt;span style="color: maroon;"&gt;" matche(s)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And a look at the results&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R40WZ5AHr-I/AAAAAAAAAHk/JMIJ-P7pfwI/s1600-h/XMLNodesWithoutDefaultNameSpace.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R40WZ5AHr-I/AAAAAAAAAHk/JMIJ-P7pfwI/s400/XMLNodesWithoutDefaultNameSpace.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5155801782462951394" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-2273494122878072897?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/2273494122878072897/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=2273494122878072897' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/2273494122878072897'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/2273494122878072897'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/is-that-default-namespace-in-xml.html' title='Is that default namespace in XML bothering you?'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_MaX5fUFKGdU/R40WZ5AHr-I/AAAAAAAAAHk/JMIJ-P7pfwI/s72-c/XMLNodesWithoutDefaultNameSpace.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-7473213256117671179</id><published>2008-01-10T09:05:00.000-08:00</published><updated>2008-01-10T09:26:06.773-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='XML Node specific attribute'/><title type='text'>Get XML Nodes that do not have a specific attribute</title><content type='html'>Alright after those design patterns, this one might strike as a real lightweight entry. Recently, I needed a method to identify all XML nodes that did not have a specific atrribute. Well, the first approach was a simple IF clause to verify if the attribute object is nothing. This did a fine job till I realized that I could a better approach, call it a result of refactoring or just an afterthought. &lt;br /&gt;&lt;br /&gt;Sample code&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Sample XML island&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; sb &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.Text.StringBuilder = &lt;span style="color: blue;"&gt;New&lt;/span&gt; System.Text.StringBuilder&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; sb.AppendLine(&lt;span style="color: maroon;"&gt;"&amp;lt;items&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; sb.AppendLine(&lt;span style="color: maroon;"&gt;"&amp;nbsp; &amp;lt;item type='Pencil' qty='5' IsCanceled='1' /&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; sb.AppendLine(&lt;span style="color: maroon;"&gt;"&amp;nbsp; &amp;lt;item type='Pen' qty='2' /&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; sb.AppendLine(&lt;span style="color: maroon;"&gt;"&amp;nbsp; &amp;lt;item type='Book' qty='1' /&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; sb.AppendLine(&lt;span style="color: maroon;"&gt;"&amp;nbsp; &amp;lt;item type='Pencil' qty='3' /&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; sb.AppendLine(&lt;span style="color: maroon;"&gt;"&amp;lt;/items&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xDoc &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Xml.XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; xDoc.LoadXml(sb.ToString)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Original XML Content"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(sb.ToString)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Original Strategy&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Active list of items"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodLst &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/items/item"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;For&lt;/span&gt; &lt;span style="color: blue;"&gt;Each&lt;/span&gt; xNod &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNode &lt;span style="color: blue;"&gt;In&lt;/span&gt; xNodLst&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;If&lt;/span&gt; xNod.Attributes(&lt;span style="color: maroon;"&gt;"IsCanceled"&lt;/span&gt;) &lt;span style="color: blue;"&gt;Is&lt;/span&gt; &lt;span style="color: blue;"&gt;Nothing&lt;/span&gt; &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"{0} [{1}]"&lt;/span&gt;, xNod.Attributes(&lt;span style="color: maroon;"&gt;"type"&lt;/span&gt;).Value, xNod.Attributes(&lt;span style="color: maroon;"&gt;"qty"&lt;/span&gt;).Value)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;If&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Alternate Strategy&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Active list of items (Alternate)"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodAltLst &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/items/item[not (@IsCanceled)]"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;For&lt;/span&gt; &lt;span style="color: blue;"&gt;Each&lt;/span&gt; xNod &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNode &lt;span style="color: blue;"&gt;In&lt;/span&gt; xNodAltLst&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"{0} [{1}]"&lt;/span&gt;, xNod.Attributes(&lt;span style="color: maroon;"&gt;"type"&lt;/span&gt;).Value, xNod.Attributes(&lt;span style="color: maroon;"&gt;"qty"&lt;/span&gt;).Value)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;In this example, I have eliminated nodes that do not have the IsCanceled attribute. The exact value of the attribute was not relevant to me, its existence was reason enough to have it eliminated from my result list. &lt;br /&gt;&lt;br /&gt;A look at the execution result&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R4ZUKpAHr9I/AAAAAAAAAHc/Ivb8IVVE3mw/s1600-h/XMLNodeSpecificAttribute.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R4ZUKpAHr9I/AAAAAAAAAHc/Ivb8IVVE3mw/s400/XMLNodeSpecificAttribute.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5153899365353893842" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Of course, if you wanted to include only nodes that had a specific attribute, you guessed right, just take the 'not' keyword out of the query  -&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; xNodAltLst &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlNodeList = xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/items/item[@IsCanceled]"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-7473213256117671179?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/7473213256117671179/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=7473213256117671179' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/7473213256117671179'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/7473213256117671179'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/get-xml-nodes-that-do-not-have-specific.html' title='Get XML Nodes that do not have a specific attribute'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_MaX5fUFKGdU/R4ZUKpAHr9I/AAAAAAAAAHc/Ivb8IVVE3mw/s72-c/XMLNodeSpecificAttribute.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-9009090268713790116</id><published>2008-01-03T11:49:00.000-08:00</published><updated>2008-01-18T12:32:06.157-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Template Method'/><title type='text'>Template Method Design Pattern</title><content type='html'>Consider a business application where your application receives data in different representations – XML, Text file, SQL data table etc. You need to print this data in a tabular format with a common header, footer, page size settings among other things. We can quickly cook up a couple of designs to accomplish this. At the simplest level, we can create a single module that prints the header, then based on the appropriate type of data, extracts the information from the data representations and prints individual rows of data, then prints the footer. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;PrintFormatter Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Enum&lt;/span&gt; DataRepresentation&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; TextFormat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; XMLFormat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; SQLDataTableFormat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Enum&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _TextData &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _XMLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _SQLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _DataRepresentation &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataRepresentation&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; PrintDataRepresentation &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataRepresentation)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _DataRepresentation = PrintDataRepresentation&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;WriteOnly&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; TextData() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; value &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; _TextData = value&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;WriteOnly&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; XMLData() &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; value &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; _XMLData = value&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;WriteOnly&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; SQLData() &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; value &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; _SQLData = value&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _PrintHeader()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _DataRepresentation&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; DataRepresentation.TextFormat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _PrintTextData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; DataRepresentation.XMLFormat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _PrintXMLData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; DataRepresentation.SQLDataTableFormat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _PrintSQLData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _PrintFooter()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _PrintHeader()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the page header"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _PrintFooter()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the page footer"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _PrintTextData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the formatted text data - "&lt;/span&gt; &amp;amp; _TextData)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _PrintXMLData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the formatted XML data - "&lt;/span&gt; &amp;amp; _XMLData.OuterXml)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _PrintSQLData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the formatted SQL data - "&lt;/span&gt; &amp;amp; _SQLData.TableName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Not much of a class diagram hierarchy to show &lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R308mZAHr3I/AAAAAAAAAGM/hW9W96MCpK4/s1600-h/TemplateMethod00.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R308mZAHr3I/AAAAAAAAAGM/hW9W96MCpK4/s400/TemplateMethod00.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5151340179025801074" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;modMain Module Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Sample Data - one of each kind&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; TextData &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt; = &lt;span style="color: maroon;"&gt;"&amp;lt;&amp;lt;TextData&amp;gt;&amp;gt;"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; XMLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Xml.XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; XMLData.LoadXml(&lt;span style="color: maroon;"&gt;"&amp;lt;xMLData /&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; SQLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataTable(&lt;span style="color: maroon;"&gt;"SQLDataTable"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oPrintFormatter &lt;span style="color: blue;"&gt;As&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; PrintFormatter(PrintFormatter.DataRepresentation.TextFormat)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.TextData = TextData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; PrintFormatter(PrintFormatter.DataRepresentation.XMLFormat)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.XMLData = XMLData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; PrintFormatter(PrintFormatter.DataRepresentation.SQLDataTableFormat)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.SQLData = SQLData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Let’s put our application to the test&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R30-hZAHr7I/AAAAAAAAAHM/FRBbsLamtXA/s1600-h/TemplateMethod01.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R30-hZAHr7I/AAAAAAAAAHM/FRBbsLamtXA/s400/TemplateMethod01.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5151342292149710770" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Alright, things look good for a one off application requirement. However, extending this design to support additional data formats would require modifying the PrintFormatter class by adding additional data representations and modifying the PrintFormattedData method. A template method pattern can be effectively used in this example to eliminate the need to modify the base class and extending the base class to support additional data formats.&lt;br /&gt;The primary idea behind the template method is to use a method to perform the actions that are common across representations – printing the header and footer while leaving the task of printing specific data representations to abstract methods. The PrintFormatter class can then be extended by the TextPrintFormatter to provide the implementation for printing text data. Similarly the XMLPrintFormatter can implement the abstract PrintSpecificData method to print XML Data while the SQLPrintFormatter can provide the SQL data printing logic.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;PrintFormatter Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'Our Template method&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; PrintHeader()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; PrintSpecificData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; PrintFooter()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'Some methods that provide a default implementation&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintHeader()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the page header"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintFooter()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the page footer"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'Abstract methods&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustOverride&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintSpecificData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;TextPrintFormatter Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; TextPrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _TextData &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; TextData &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _TextData = TextData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintSpecificData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the formatted text data - "&lt;/span&gt; &amp;amp; _TextData)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;XMLPrintFormatter Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; XMLPrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _XMLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; XMLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XMLData = XMLData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintSpecificData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the formatted XML data - "&lt;/span&gt; &amp;amp; _XMLData.OuterXml)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;SQLDataPrintFormatter Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; SQLDataPrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _SQLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; SQLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _SQLData = SQLData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; PrintSpecificData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"This represents the formatted SQL data - "&lt;/span&gt; &amp;amp; _SQLData.TableName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The Template method pattern class hierarchy&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R30805AHr5I/AAAAAAAAAGc/1Pv_7sDt4hs/s1600-h/TemplateMethod02.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R30805AHr5I/AAAAAAAAAGc/1Pv_7sDt4hs/s400/TemplateMethod02.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5151340428133904274" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;modMain Module Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Sample Data - one of each kind&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; TextData &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt; = &lt;span style="color: maroon;"&gt;"&amp;lt;&amp;lt;TextData&amp;gt;&amp;gt;"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; XMLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; Xml.XmlDocument = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Xml.XmlDocument&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; XMLData.LoadXml(&lt;span style="color: maroon;"&gt;"&amp;lt;xMLData /&amp;gt;"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; SQLData &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataTable(&lt;span style="color: maroon;"&gt;"SQLDataTable"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oPrintFormatter &lt;span style="color: blue;"&gt;As&lt;/span&gt; PrintFormatter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; TextPrintFormatter(TextData)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; XMLPrintFormatter(XMLData)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SQLDataPrintFormatter(SQLData)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oPrintFormatter.PrintFormattedData()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And a look at the results&lt;br /&gt; &lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R30-rpAHr8I/AAAAAAAAAHU/rKlzMkAV8aw/s1600-h/TemplateMethod03.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R30-rpAHr8I/AAAAAAAAAHU/rKlzMkAV8aw/s400/TemplateMethod03.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5151342468243369922" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-9009090268713790116?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/9009090268713790116/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=9009090268713790116' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/9009090268713790116'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/9009090268713790116'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/template-method-design-pattern.html' title='Template Method Design Pattern'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_MaX5fUFKGdU/R308mZAHr3I/AAAAAAAAAGM/hW9W96MCpK4/s72-c/TemplateMethod00.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-933575112246399849</id><published>2008-01-02T11:43:00.000-08:00</published><updated>2008-01-18T12:31:08.055-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Singleton'/><title type='text'>Singleton Design Pattern</title><content type='html'>For this example, we will design an application to remotely monitor the temperature and memory usage of the PC. Each monitoring function runs in a separate thread and monitors corresponding elements to produce a value every 1 second. This information needs to be relayed across to the remote device connected via the Serial communication interface of the PC. &lt;br /&gt;Well with requirements as simple as this, let’s dig in&lt;br /&gt;&lt;br /&gt;&lt;b&gt;TemperatureMonitor Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; TemperatureMonitor&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _IsExitSignalled &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _IsExitSignalled = &lt;span style="color: blue;"&gt;False&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; StartMonitoring()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; th &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.Threading.Thread = &lt;span style="color: blue;"&gt;New&lt;/span&gt; System.Threading.Thread(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt; _MonitoringThread)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; th.Start(oSerialCommunicator)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _MonitoringThread(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oSC &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator = &lt;span style="color: blue;"&gt;CType&lt;/span&gt;(oSerialCommunicator, SerialCommunicator)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Do&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Add steps here to perform actual temperatur monitoring,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; oSC.SendMonitoredInfo(&lt;span style="color: maroon;"&gt;"Memory Temperature Message"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Threading.Thread.Sleep(2000) &lt;span style="color: green;"&gt;'Sleep for 2 seconds&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Loop&lt;/span&gt; &lt;span style="color: blue;"&gt;Until&lt;/span&gt; _IsExitSignalled&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; SignalExit()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _IsExitSignalled = &lt;span style="color: blue;"&gt;True&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;MemoryMonitor Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; MemoryMonitor&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _IsExitSignalled &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _IsExitSignalled = &lt;span style="color: blue;"&gt;False&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; StartMonitoring()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; th &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.Threading.Thread = &lt;span style="color: blue;"&gt;New&lt;/span&gt; System.Threading.Thread(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt; _MonitoringThread)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; th.Start(oSerialCommunicator)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _MonitoringThread(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oSC &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator = &lt;span style="color: blue;"&gt;CType&lt;/span&gt;(oSerialCommunicator, SerialCommunicator)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Do&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Add steps here to perform actual memory monitoring,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; oSC.SendMonitoredInfo(&lt;span style="color: maroon;"&gt;"Memory Monitor Message"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Threading.Thread.Sleep(2000) &lt;span style="color: green;"&gt;'Sleep for 2 seconds&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Loop&lt;/span&gt; &lt;span style="color: blue;"&gt;Until&lt;/span&gt; _IsExitSignalled&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; SignalExit()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _IsExitSignalled = &lt;span style="color: blue;"&gt;True&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;SerialCommunicator Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Open the communication port&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Opening Serial Port"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; SendMonitoredInfo(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; Message &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Sending message "&lt;/span&gt; &amp;amp; Message)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And here is what our classes look like&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vpkJAHrzI/AAAAAAAAAFs/72CT9oUTXaw/s1600-h/Singleton01.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vpkJAHrzI/AAAAAAAAAFs/72CT9oUTXaw/s400/Singleton01.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150967405929279282" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So what else, well the test harness to test our little application&lt;br /&gt;&lt;br /&gt;&lt;b&gt;modMain Module Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oTemperatureMonitor &lt;span style="color: blue;"&gt;As&lt;/span&gt; TemperatureMonitor = &lt;span style="color: blue;"&gt;New&lt;/span&gt; TemperatureMonitor&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oMemoryMonitor &lt;span style="color: blue;"&gt;As&lt;/span&gt; MemoryMonitor = &lt;span style="color: blue;"&gt;New&lt;/span&gt; MemoryMonitor&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oTemperatureMonitor.StartMonitoring()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oMemoryMonitor.StartMonitoring()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Press Enter to stop monitoring"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oTemperatureMonitor.SignalExit()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oMemoryMonitor.SignalExit()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Monitoring stopped"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And a first look at the results&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3vpw5AHr0I/AAAAAAAAAF0/ZsMeNI2057Q/s1600-h/Singleton02.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3vpw5AHr0I/AAAAAAAAAF0/ZsMeNI2057Q/s400/Singleton02.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150967624972611394" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;All this looks pretty simple on the face of it; however, as any experienced programmer who has used the COM port of a PC will tell you, you cannot concurrently open the same port more than once. This implies that the information that the Temperature Monitoring thread will produce will need to be sent over the serial interface in a timeshared fashion along with the information produced by the Memory Monitoring thread. This also implies that the highlighted portion in the output which indicates that the port has been opened concurrently is an issue and this application cannot be used for sending messages out.&lt;br /&gt;So, how do we fix this port sharing issue? Put the Singleton pattern to use. We transform our SerialCommunicator class into a singleton so that it returns a single instance of itself to all requesting methods. With only a single instance of the SerialCommunicator, we eliminate the issue of opening the serial port concurrently. To transform this class into a Singleton, the first thing we need to do is to prevent its external instantiation using the New keyword. We can achieve this by changing the scope of the constructor to Private. &lt;br /&gt;Hold on, won’t that prevent object instantiation? In the conventional sense, Yes! However a private constructor implies that the object can be instantiated within the class. What good is that? How will our TemperatureMonitor and MemoryMonitor acquire this instance? Ok, Enough beating around the bush. Just add a shared method that creates and returns an instance of the class. Ensure that it creates an instance only if an instance of the class does not exist. If an instance exists, it simply returns that instance. &lt;br /&gt;Well, here is the new and improved Singleton SerialCommunicator class –&lt;br /&gt;&lt;br /&gt;&lt;b&gt;SerialCommunicator Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Shared&lt;/span&gt; _oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Open the communication port&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Opening Serial Port"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Shared&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; GetSerialCommunicator() &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;If&lt;/span&gt; _oSerialCommunicator &lt;span style="color: blue;"&gt;Is&lt;/span&gt; &lt;span style="color: blue;"&gt;Nothing&lt;/span&gt; &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; _oSerialCommunicator = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;If&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Return&lt;/span&gt; _oSerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; SendMonitoredInfo(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; Message &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;SyncLock&lt;/span&gt; &lt;span style="color: blue;"&gt;Me&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Sending message "&lt;/span&gt; &amp;amp; Message)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;SyncLock&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;We need to change our TemperatureMonitor and MemoryMonitor classes as well since the public constructor of the SerialCommunicator is no longer available. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;TemperatureMonitor StartMonitoring Method Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; TemperatureMonitor&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'No change in code here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; StartMonitoring()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator = SerialCommunicator.GetSerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; th &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.Threading.Thread = &lt;span style="color: blue;"&gt;New&lt;/span&gt; System.Threading.Thread(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt; _MonitoringThread)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; th.Start(oSerialCommunicator)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'No change in code or here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;MemoryMonitor StartMonitoring Method Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; MemoryMonitor&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'No change in code here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; StartMonitoring()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oSerialCommunicator &lt;span style="color: blue;"&gt;As&lt;/span&gt; SerialCommunicator = SerialCommunicator.GetSerialCommunicator&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; th &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.Threading.Thread = &lt;span style="color: blue;"&gt;New&lt;/span&gt; System.Threading.Thread(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt; _MonitoringThread)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; th.Start(oSerialCommunicator)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'No change in code here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Our class diagram is identical to our original. The only difference being the scope of the SerialCommunicator constructor and an additional method in the same class to create and return an instance of the SerialCommunicator  class.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R3vp4pAHr1I/AAAAAAAAAF8/wbld3IXVkPk/s1600-h/Singleton03.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R3vp4pAHr1I/AAAAAAAAAF8/wbld3IXVkPk/s400/Singleton03.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150967758116597586" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;With no change in our test harness, let’s put our application to the test&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3vqBZAHr2I/AAAAAAAAAGE/L5XkMUajvqE/s1600-h/Singleton04.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3vqBZAHr2I/AAAAAAAAAGE/L5XkMUajvqE/s400/Singleton04.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150967908440452962" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Notice that we only have one “Opening Serial Port” message indicating our Singleton pattern is in effect.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-933575112246399849?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/933575112246399849/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=933575112246399849' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/933575112246399849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/933575112246399849'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/singleton-design-pattern.html' title='Singleton Design Pattern'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vpkJAHrzI/AAAAAAAAAFs/72CT9oUTXaw/s72-c/Singleton01.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-7792454049073684224</id><published>2008-01-02T11:22:00.000-08:00</published><updated>2008-01-18T12:31:22.256-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Adapter'/><title type='text'>Adapter Design Pattern</title><content type='html'>Let us extend our simulation game example and introduce some animals in addition to the Human characters. For example, Trained elephants or Elephants for short. These trained elephants share a common behavior with other animals, say, Sleep, Drink, Eat and Graze. These elephants, being trained for warfare can also Move and Fight similar to Human characters. For this example, I will consider the Soldier who implements the Eat, Talk, Fight and Move behavior from the GameCharacter interface. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Interface Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Soldier Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Soldier&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Eat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Soldier is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Fight&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Soldier is fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Move&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Soldier is moving slowly"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Talk&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Soldier is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Let us assume that the Animal class and the inheriting sub-classes for specific animals were created by another developer on the team. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Animal Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Animal&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Sleep()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Animal is sleeping"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Drink()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Animal is drinking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Animal is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Graze()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Animal is grazing"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; MakeSound()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Animal is making some noise"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The TrainedElephant class extends the Animal class by adding the Move and Attack methods &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;TrainedElephant Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; TrainedElephant&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; Animal&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"TrainedElephant is moving"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Attack()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"TrainedElephant is attack"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Let us have a quick look at our class and interface hierarchy&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3vly5AHrvI/AAAAAAAAAFM/piuGa7FP23c/s1600-h/Adapter01.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3vly5AHrvI/AAAAAAAAAFM/piuGa7FP23c/s400/Adapter01.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150963261285838578" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;And finally, lets list our requirement. With an existing design that resembles this, we would like to maintain a list of all character instances in the game including humans as well as animals that can fight and move. A quick solution is to use a generic list that holds GameCharacter objects. Since Soldiers natively implement the GameCharacter interface, it is quite easy to add a Soldier to this list. However, there is no relation between the Animal class and the GameCharacter interface and neither does the TrainedElephant class implement the Animal interface. It, therefore makes it impossible to add the TrainedElephant as a GameCharacter to the list.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vmYJAHrwI/AAAAAAAAAFU/Uf_WFOuDSCY/s1600-h/Adapter02.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vmYJAHrwI/AAAAAAAAAFU/Uf_WFOuDSCY/s400/Adapter02.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150963901235965698" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;One solution is to modify the code for the TrainedElephant class so that it implements the GameCharacter Interface. Although this seems easy from the looks of it, this may not be practical in a real life application where this portion of the code is built by another developer or when the source code for this class is not available for modification. In such scenarios, we can employ the services of the Adapter pattern to adapt the functionality of the TrainedElephant class to map to the GameCharacter class.&lt;br /&gt;The TrainedElephantAdapter class simply wraps around a TrainedElephant object while implementing the GameCharacter interface. Thus, we do not need to modify the source code for the TrainedElephant or the Animal class and can use this TrainedElephantAdapter object interchangeably as a GameCharacter.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;TrainedElephantAdapter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; TrainedElephantAdapter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _oElephant &lt;span style="color: blue;"&gt;As&lt;/span&gt; TrainedElephant&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oElephant &lt;span style="color: blue;"&gt;As&lt;/span&gt; TrainedElephant)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oElephant = oElephant&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Eat&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oElephant.Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Fight&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oElephant.Attack()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Move&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oElephant.Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; GameCharacter.Talk&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oElephant.MakeSound()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Our class diagram now contains our new TrainedElephantAdapter which encapsulates the TrainedElephant object while implementing the GameCharacter interface&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3vmfZAHrxI/AAAAAAAAAFc/IO1NJpF4-uM/s1600-h/Adapter03.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3vmfZAHrxI/AAAAAAAAAFc/IO1NJpF4-uM/s400/Adapter03.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150964025790017298" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Finally, a little test harness using the TrainedElephantAdapter to do the magic for us&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;modMain Module Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; GameCharacters &lt;span style="color: blue;"&gt;As&lt;/span&gt; List(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; GameCharacter) = &lt;span style="color: blue;"&gt;New&lt;/span&gt; List(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; GameCharacter)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; GameCharacters.Add(&lt;span style="color: blue;"&gt;New&lt;/span&gt; Soldier)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'And here is the Wrapped up TrainedElephant&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; GameCharacters.Add(&lt;span style="color: blue;"&gt;New&lt;/span&gt; TrainedElephantAdapter(&lt;span style="color: blue;"&gt;New&lt;/span&gt; TrainedElephant))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;For&lt;/span&gt; &lt;span style="color: blue;"&gt;Each&lt;/span&gt; oGC &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameCharacter &lt;span style="color: blue;"&gt;In&lt;/span&gt; GameCharacters&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; oGC.Eat() : oGC.Fight() : oGC.Move() : oGC.Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And the results of using the Adapter pattern&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3voOZAHryI/AAAAAAAAAFk/SfMB1HYfmjo/s1600-h/Adapter04.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3voOZAHryI/AAAAAAAAAFk/SfMB1HYfmjo/s400/Adapter04.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150965932755496738" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-7792454049073684224?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/7792454049073684224/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=7792454049073684224' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/7792454049073684224'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/7792454049073684224'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/adapter-design-pattern.html' title='Adapter Design Pattern'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_MaX5fUFKGdU/R3vly5AHrvI/AAAAAAAAAFM/piuGa7FP23c/s72-c/Adapter01.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-5955715586252877872</id><published>2008-01-02T11:09:00.000-08:00</published><updated>2008-01-18T12:31:29.759-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Facade'/><title type='text'>Facade Design Pattern</title><content type='html'>The Facade pattern is probably one of the most frequently used design patterns. When a class requires the functionality derived from different classes, it is often useful to use a façade class that encapsulates the calls to the various classes. This class can then be effectively used instead of making calls to the individual classes and improves code reuse. Additionally, if we decide to make changes to the encapsulated classes, all we need to alter are the method invocations in the façade class.&lt;br /&gt;Consider a business application for a mortgage company. The Mortgage Company uses multiple services that are both internal to the company as well as require interaction with other parties to determine if a loan can be sanctioned. Using the Facade pattern here is easy, create a class say LoanHelper. We can then create shared method unless you prefer to create an instance of the LoanHelper. Add the method calls to the different internal and external services. This LoanHelper method can then be used anywhere within your code to validate a prospect before allotting a loan and determining the rate of interest.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;LoanHelper Class Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; LoanHelper&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Const&lt;/span&gt; DEFAULT_LOAN_PERCENT &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt; = 10.0&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Shared&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; GetEligbleLoanCriteria(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; SSN &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; RequiredLoanAmount &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt;, _&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;  &lt;span style="color: blue;"&gt;ByRef&lt;/span&gt; EligibleLoanAmount &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByRef&lt;/span&gt; EligibleLoanPercent &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt;) &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Add the method calls to various systems - internal and external to determine &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'the values of EligibleLoanAmount and the EligibleLoanAmount&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; EligibleLoanAmount = 100000&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; EligibleLoanPercent = 8.25&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Fa&amp;#231;ade class LoanHelper was invoked to determine Eligible Loan Criteria"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Return&lt;/span&gt; &lt;span style="color: blue;"&gt;True&lt;/span&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'Return false if SSN failed to match&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;modMain Module Listing&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; SSN &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt; = &lt;span style="color: maroon;"&gt;"SampleSSN"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; RequiredLoanAmount &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt; = 100000&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; EligibleLoanAmount &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt; = 0&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; EligibleLoanPercent &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt; = LoanHelper.DEFAULT_LOAN_PERCENT&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; IsSSNMatch &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt; = LoanHelper.GetEligbleLoanCriteria(SSN, RequiredLoanAmount, _&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp;  EligibleLoanAmount, EligibleLoanPercent)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;A look at the classes&lt;/b&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vj_JAHrtI/AAAAAAAAAE8/vhk2dMR7TrU/s1600-h/Facade0.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vj_JAHrtI/AAAAAAAAAE8/vhk2dMR7TrU/s400/Facade0.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150961272715980498" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;And the results&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R3vkPpAHruI/AAAAAAAAAFE/op4pxST8bSY/s1600-h/Facade1.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R3vkPpAHruI/AAAAAAAAAFE/op4pxST8bSY/s400/Facade1.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150961556183822050" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-5955715586252877872?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/5955715586252877872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=5955715586252877872' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5955715586252877872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5955715586252877872'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/facade-design-pattern.html' title='Facade Design Pattern'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R3vj_JAHrtI/AAAAAAAAAE8/vhk2dMR7TrU/s72-c/Facade0.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-5877031020335309419</id><published>2008-01-02T00:51:00.001-08:00</published><updated>2008-01-18T12:31:39.274-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='State'/><title type='text'>State Design Pattern</title><content type='html'>For this example, let us assume that we would like to design an application that simulates the interaction in a single character game. The character, in this game, can be moving on the ground, swimming under water, flying an airplane or driving a car. Each gaming episode may involve the character in any one of these conditions. The character is expected to navigate in these different environments using the same navigation keys on the keyboard. Additionally, the character may use the Action Key (say Control key on the keyboard) to perform actions that are relevant to each environment. While on ground, the Action Key is used to talk; while in water, it is used to float up for a breath of air, while in an airplane, to get directions and while in the car, to use the GPS.&lt;br /&gt;A map of the keys versus environments is listed here&lt;br /&gt;&lt;br /&gt;&lt;table cellspacing="0" cellpadding="0" border="0"&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Key&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Ground&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Underwater&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Airplane&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Car&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Front Direction&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Move Forward&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Swim Forward&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Increase Throttle&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Accelerate&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Back Direction&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Move Backward&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Swim Backward&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Decrease Throttle&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Decelerate&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Right Direction&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Move Angular Right&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Swim Angular Right&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Change Direction to the Right&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Turn Car to the Right&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Left Direction&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Move Angular Left&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Swim Angular Left&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Change Direction to the Left&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Turn Car to the Left&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Action&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Talk&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Surface for Air&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Get directions&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;Use GPS&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;A first take at this design would be by starting off creating a GameCharacter class. The interactions that are possible would be based on the key strokes that the GameCharacter supports. Let me map them to the normal gaming keys on the keyboard – UpArrowPressed, DownArrowPressed, RightPressed and LeftPressed to cover the navigation areas and the ActionPressed method for supporting the character’s action feature. The State of the environment can be stored in an instance value that holds an enumeration. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Enum&lt;/span&gt; EnvironmentStates&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Enum&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _EnvironmentState &lt;span style="color: blue;"&gt;As&lt;/span&gt; EnvironmentStates&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'All episodes start with the character on Land&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _EnvironmentState = EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; UpArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _EnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving forward"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is swimming forward"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is increasing throttle"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is accelerating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; DownArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _EnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving backwards"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is swimming backwards"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is decreasing throttle"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is decelerating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; RightArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _EnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving at an angular right"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is swimming at an angular right"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is changing directionto the right"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is turning car to the right"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; LeftArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _EnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving at an angular left"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is swimming at an angular left"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is changing directionto the left"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is turning car to the left"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; ActionPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _EnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is surfacing for air"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is getting directions"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is using the GPS"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Additionally, while in water, if the character reaches the edge of the water body, user will jump out of water and be on solid ground. Similarly, reaching the landing area, whilst in an airplane, will move the character on the ground. In a similar fashion, the character will change environments when he reaches the periphery of that environment. Ground is compatible with all the three environments and changes are triggered in both directions on reaching appropriate boundaries. Environment changes between other boundaries are not possible. &lt;br /&gt;&lt;br /&gt;&lt;table cellspacing="0" cellpadding="0" border="0"&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Environment&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Water boundary&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Airport boundary&lt;/b&gt;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color: #c0504d; color: white;"&gt;&lt;b&gt;Parking Lot boundary&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Land&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Jump underwater&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Jump inside Airplane&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Jump inside Car&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Underwater&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Jump on Land*&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Not Applicable&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Not Applicable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Airplane&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Not Applicable&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Jump on Land*&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Not Applicable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Car&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Not Applicable&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Not Applicable&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td style="border-bottom: #cf7b79 1px solid; background-color:#EFD3D2;"&gt;Jump on Land*&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;* Next corresponding boundary&lt;br /&gt;&lt;br /&gt;We can extend our design to add instance variables that can help define the boundary parameters. Note that while on land – a two dimensional co-ordinate system can be used to map the boundary, however while in water or in air, we need a three dimensional co-ordinate system to store the depth and height respectively in addition to the x and y co-ordinates.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Additional Instance variables&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _XCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _YCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _DepthCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _HeightCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _OpenLandXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _OpenLandYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _WaterXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _WaterYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _WaterDepthDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _AirplaneXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _AirplaneYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _AirplaneHeightDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _RoadXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _RoadYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Modified constructor&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'All episodes start with the character on Land&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _EnvironmentState = EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;' ... at location 0,0; isn't that nice.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XCoord = 0 : _YCoord = 0 : _DepthCoord = 0 : _HeightCoord = 0&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Define the dimensions of each environment&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _OpenLandXDimension = 100 : _OpenLandYDimension = 100&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _WaterXDimension = 100 : _WaterYDimension = 100 : _WaterDepthDimension = 50&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _AirplaneXDimension = 100 : _AirplaneYDimension = 100 : _AirplaneHeightDimension = 200&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _RoadXDimension = 50 : _RoadYDimension = 200&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Boundary determining functions&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; _IsWaterBoundaryEncountered() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Use the layout map and the current X and Y coordinates and the Depth coordinate to determine if the water boundary is encountered&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'the depth coordinate is useful when the current state is in water&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; _IsAirportBoundaryEncountered() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Use the layout map and the current X and Y coordinates as well as the Height coordinate to determine if the airport boundary is encountered&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'the height coordindate is useful when the current state is in the airplane&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; _IsParkingLotBoundaryEncountered() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Use the layout map and the current X and Y coordinates as well as the coordinate to determine if the parking lot boundary is encountered&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Partially modified listing of the UpArrowPressed method&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; UpArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _EnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Land&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving forward"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _XCoord += 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; &lt;span style="color: blue;"&gt;True&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _IsWaterBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _EnvironmentState = EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _XCoord = 0 : _YCoord = 0 : _DepthCoord = 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _IsAirportBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _EnvironmentState = EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _XCoord = 0 : _YCoord = 0 : _HeightCoord = 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _IsParkingLotBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _EnvironmentState = EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _XCoord = 0 : _YCoord = 0&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Underwater&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is swimming forward"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Repeat similar code here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Airplane&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is increasing throttle"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Repeat similar code here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; EnvironmentStates.Car&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is accelerating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Repeat similar code here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Of course, besides navigating in these environments, the character performs a number of actions that are not relevant in the context of this example and I have deliberately avoided them (at least for now)&lt;br /&gt;Well, we have a working design and things look good. A closer look at the code brings out the following issues – &lt;br /&gt;&lt;ul&gt;&lt;li&gt;The instance variable definition shows that a lot of variables only have contextual meaning based on the state of the character. For example, the _DepthCoord and the _HeightCoord have no meaning when the GameCharacter is on ground or while he is driving a car.&lt;/li&gt; &lt;br /&gt;&lt;li&gt;Each supported keystroke handler has the same “Select Case” code blocks to determine their action based on the state of the GameCharacter.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;State transitions are interspersed in code making it almost impossible to understand when a transition takes place.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Adding a new state – say Sailing in a boat will require rework of each keystroke handler and additional rework on maintaining the correct state when the boundary is encountered. This implies that the entire GameCharacter class will need to be checked for changes and at least five methods will need to be altered in addition to adding instance variables to represent state specific behavior.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;So how do we fix this? Quite easily, as a matter of fact, all we need to do is build a state interface that provides methods to address the keystrokes and implement each required state to handle the keystrokes as it sees fit in its environment. The GameCharacter simply holds the instance of the current state and delegates all keystrokes to the current state instance.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;IEnvironmentState Interface Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt; IEnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; UpArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; DownArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; RightArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; LeftArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; ActionPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Move the responsibility of holding and determining the environment boundaries to the GameEnvironmentManager class&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameEnvironmentManager Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameEnvironmentManager&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _OpenLandXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _OpenLandYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _WaterXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _WaterYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _WaterDepthDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _AirplaneXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _AirplaneYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _AirplaneHeightDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _RoadXDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _RoadYDimension &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Define the dimensions of each environment&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _OpenLandXDimension = 100 : _OpenLandYDimension = 100&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _WaterXDimension = 100 : _WaterYDimension = 100 : _WaterDepthDimension = 50&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _AirplaneXDimension = 100 : _AirplaneYDimension = 100 : _AirplaneHeightDimension = 200&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _RoadXDimension = 50 : _RoadYDimension = 200&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; IsWaterBoundaryEncountered(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; XCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; YCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;) &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Use the X and Y coord to determine if the Water boundary is encountered&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; IsWaterBoundaryEncountered(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; XCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; YCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; DepthCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;) &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Use the X and Y coord alongwith the depth to determine if the Water boundary is encountered&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'Other functions to determine the other boundaries&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Implement each state and control the state of the GameCharacter appropriately by creating classes for WalkingLand, SwimmingUnderWater, FlyingAirplane and DrivingCar&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;WalkingLand Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; WalkingLand&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IEnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _oGameCharacter &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _oGEM &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameEnvironmentManager&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _XCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _YCoord &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oGameCharacter &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameCharacter, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oGEM &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameEnvironmentManager)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oGameCharacter = oGameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oGEM = oGEM&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XCoord = 0&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _YCoord = 0&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; UpArrowPressed() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IEnvironmentState.UpArrowPressed&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving forward"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XCoord += 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _HandleIfBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; DownArrowPressed() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IEnvironmentState.DownArrowPressed&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving backwards"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XCoord -= 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _HandleIfBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; RightArrowPressed() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IEnvironmentState.RightArrowPressed&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving at an angular right"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XCoord += 1 : _YCoord -= 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _HandleIfBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; LeftArrowPressed() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IEnvironmentState.LeftArrowPressed&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving at an angular left"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _XCoord += 1 : _YCoord += 1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _HandleIfBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; ActionPressed() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IEnvironmentState.ActionPressed&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; _HandleIfBoundaryEncountered()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; &lt;span style="color: blue;"&gt;True&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; _oGEM.IsWaterBoundaryEncountered(_XCoord, _YCoord)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _oGameCharacter.EnvironmentState = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SwimmingUnderwater(_oGameCharacter, _oGEM)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Other boundary encountered statements go here&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _oEnvironmentState &lt;span style="color: blue;"&gt;As&lt;/span&gt; IEnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; _oGEM &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameEnvironmentManager&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oGEM = &lt;span style="color: blue;"&gt;New&lt;/span&gt; GameEnvironmentManager&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'All episodes start with the character on Land&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oEnvironmentState = &lt;span style="color: blue;"&gt;New&lt;/span&gt; WalkingLand(&lt;span style="color: blue;"&gt;Me&lt;/span&gt;, _oGEM)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; UpArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oEnvironmentState.UpArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; DownArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oEnvironmentState.DownArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; RightArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oEnvironmentState.RightArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; LeftArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oEnvironmentState.DownArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; ActionPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; _oEnvironmentState.ActionPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;WriteOnly&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; EnvironmentState() &lt;span style="color: blue;"&gt;As&lt;/span&gt; IEnvironmentState&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; value &lt;span style="color: blue;"&gt;As&lt;/span&gt; IEnvironmentState)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; _oEnvironmentState = value&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;I have ignored the details of the other classes to reduce the code bulk in this example. This design completely separates the GameCharacter from its constituent states and the behavior of the GameCharacter is delegated to the individual states. Interestingly, each state may need to be aware of other related transitioning states in the system so that a state transition can be effectively addressed.  An alternative to our design is to have these states predefined in the GameCharacter class and simply trigger the GameCharacter class via public methods to set its next state appropriately. This alternate design will eliminate any need for the implementing EnvironmentStates to know about each other and the entire controlling mechanism is built in the GameCharacter class. &lt;br /&gt;Selecting an appropriate design choice depends on how best you feel about handling additional states or packaging components. &lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3tSnJAHrrI/AAAAAAAAAEs/2lQ0ed6LzvA/s1600-h/State01.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3tSnJAHrrI/AAAAAAAAAEs/2lQ0ed6LzvA/s400/State01.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150801431213092530" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I have built a test harness to test our GameCharacter and its reaction based on Keystrokes. For the sake of keeping the example short, I have forced a return value of true for the boundary condition tests when the GameCharacter moves forward and when it surfaces for air after swimming backward.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;modMain Module Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oGameCharacter &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameCharacter = &lt;span style="color: blue;"&gt;New&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oGameCharacter.UpArrowPressed()&amp;nbsp; &lt;span style="color: green;"&gt;'This will trigger Swimming in water&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oGameCharacter.DownArrowPressed()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oGameCharacter.ActionPressed() &lt;span style="color: green;"&gt;'This will trigger Jumping on Land&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oGameCharacter.UpArrowPressed()&amp;nbsp; &lt;span style="color: green;"&gt;'This will trigger Swimming in water again, but we wont test it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.ReadLine()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And the results&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3tSrZAHrsI/AAAAAAAAAE0/BaVPmPNQHEA/s1600-h/State02.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3tSrZAHrsI/AAAAAAAAAE0/BaVPmPNQHEA/s400/State02.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150801504227536578" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-5877031020335309419?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/5877031020335309419/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=5877031020335309419' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5877031020335309419'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5877031020335309419'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/state-design-pattern.html' title='State Design Pattern'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R3tSnJAHrrI/AAAAAAAAAEs/2lQ0ed6LzvA/s72-c/State01.gif' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-4986587425787265149</id><published>2008-01-01T10:45:00.000-08:00</published><updated>2008-01-18T12:31:51.395-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Strategy'/><title type='text'>Strategy Design Pattern</title><content type='html'>Consider the following example – We need to develop the framework for a game design (Age of Empires is a good example) that requires simulation of individual characters. Let us start this design by considering a couple of character types – peasants and soldiers with the following characteristics for these characters&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Both peasants and soldiers can eat and talk&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Peasants can move slowly (well walk) while soldiers can move fast&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Peasants cannot fight whereas soldiers can fight&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;A person well conversant with OOPS would start out by building this design based on a GameCharacter abstract class that will provide the default implementations for the common methods say eat and talk while leaving the implementation of methods such as move and fight to the sub classes.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustOverride&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustOverride&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The Peasant class and the Soldier class can then inherit from the GameCharacter class providing concrete implementations of the Move and the Fight methods&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Peasant Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Peasant&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character cannot fight"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving slowly"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Soldier Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Soldier&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is Fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving fast"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Let us now consider the introduction of a new character – the Cavalry man with the following characteristics&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Cavalry man can fight&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Cavalry man moves by riding a horse&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Using our existing design, this is fairly easy to accomplish by creating another class – CavalryMan which implements the GameCharacter class and providing appropriate implementations for the Move and Fight methods. For sake of simplicity, I am ignoring the Horse at this point of time since it does not provide any independent behavior of its own.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;CavalryMan Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; CavalryMan&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is riding a horse"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;A quick view of the class diagram shows a fairly decent class hierarchy and the design seems to fit the requirements quite aptly.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R3qOQ5AHrlI/AAAAAAAAAD8/5crP9WXqEsA/s1600-h/Strategy01.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R3qOQ5AHrlI/AAAAAAAAAD8/5crP9WXqEsA/s400/Strategy01.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150585544681958994" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you review the code, you will find that both the Soldier and the CavalryMan provide the same implementation for the Fight method. Although, my example uses a single line of code to provide the appropriate implementation, real world classes will have complex logic that would be repeated in both these classes to provide this implementation. An alternative to this design might inspire a few programmers to suggest that we move the Fight method implementation to the GameCharacter abstract class and override this behavior in the Peasant sub-class. We can then consolidate the implementation at one location without need to rewrite the same implementation in the Soldier and CavalryMan sub-classes.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is Fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustOverride&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Soldier Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Soldier&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving fast"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;CavalryMan Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; CavalryMan&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is riding a horse"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Clearly, the Peasant class requires no change and the original overriding of the Fight method provides its default behavior. The Class diagram is identical to our original class diagram barring the default method implementation of the Fight method and the missing implementations in the Soldier and the CavalryMan class.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3qPpJAHrmI/AAAAAAAAAEE/99GWipFgdc0/s1600-h/Strategy02.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3qPpJAHrmI/AAAAAAAAAEE/99GWipFgdc0/s400/Strategy02.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150587060805414498" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now, if we have to introduce another character – the Mason who exhibits the following characteristics&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Mason cannot fight, a behavior that he shares with the peasant&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Mason moves fast, a behavior that he shares with the soldier&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Ah, we have a challenge at hand now. If we were to use our last idea, we could simply provide a default implementation of the Move method as well in the GameCharacter class, allowing the Mason and Soldier to move fast and therefore, requiring no special implementation of these methods in the Soldier and Mason classes. The Peasant and CavalryMan classes already provide their implementation of the Move method. The Fight method, would however pose a challenge of sorts. We would need to implement the behavior in the Mason class to override the default implementation in the GameCharacter class.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is Fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving fast"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Soldier Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Soldier&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'Wow no implementation, &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: green;"&gt;'does this mean the GameCharacter is a soldier by default. Hmm!&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Mason Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Mason&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character cannot fight"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Well, looks like we have a working solution to our requirement. &lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3qQypAHrnI/AAAAAAAAAEM/SOQatHu1vK0/s1600-h/Strategy03.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R3qQypAHrnI/AAAAAAAAAEM/SOQatHu1vK0/s400/Strategy03.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150588323525799538" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;However, this design suffers from certain flaws that are readily visible. One of most obvious flaws is the GameCharacter representing the Soldier class by default. In a truly object oriented design, this could be read as GameCharacter and Soldier class being the same. Extending this statement to the implementing classes implies that &lt;br /&gt;&lt;ul&gt;&lt;li&gt;Peasant is a type of Soldier who walks slowly and does not fight&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Mason is a type of Soldier who walks fast and does not fight&lt;/li&gt;&lt;br /&gt;&lt;li&gt;CavalryMan is a type of Soldier who rides instead of walking&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Barring the last one pertaining to the CavalryMan, the other two definitions do not fit our logical definition of these characters. &lt;br /&gt;The OOPS solution to this would be to increase the depth of inheritance by introducing an additional sub-class that can act as a super-type for characters that fight. The Soldier and CavalryMan could then extend from this FightingCharacter class while the Peasant and the Mason could extend from the GameCharacter Class.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character cannot fight"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustOverride&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;FightingCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;MustInherit&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; FightingCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Soldier Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Soldier&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; FightingCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving fast"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;CavalryMan Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; CavalryMan&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; FightingCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is riding a horse"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Peasant Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Peasant&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving slowly"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Mason Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Mason&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving fast"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The default Fight implementation of the GameCharacter class would prevent the character from fighting and therefore, the Peasant and Mason class would not need to provide any implementation for the Fight method. The default Fight implementation of the FightingCharacter class would provide the necessary Fighting ability to the class overriding the implementation of the GameCharacter class. The Soldier and CavalryMan class would not be required to provide any implementation of the Fight method simply inheriting the implementation of the FightingCharacter.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3qSvZAHroI/AAAAAAAAAEU/mCsefzK9goI/s1600-h/Strategy04.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3qSvZAHroI/AAAAAAAAAEU/mCsefzK9goI/s400/Strategy04.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150590466714480258" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;However, we would still need to resolve the Move implementation where classes in different hierarchies – Soldier from the FightingCharacter-&gt;GameCharacter hierarchy and Mason from the GameCharacter hierarchy need to provide the same implementation. &lt;br /&gt;This approach would therefore not work out for us. Even complicating the design by creating a FastMovingCharacter class which extends the Gaming character and provides a “is moving fast” implementation of the Move method, then extending the Mason class and trying to extend the Soldier from the same class is not possible. This is primarily because multiple inheritance is not available to us and even using a language permitting this would complicate the application since it won’t be clear about the exact nature of implementation. &lt;br /&gt;This brings us to revealing the second flaw in the design. The use of overriding is a cautious exercise and overriding without understanding its impact might complicate the understanding of the classes and their exact implementation nature. &lt;br /&gt;Well, so what is the best design for this little requirement? Please welcome the Strategy pattern. It helps address requirements of our characters – Peasant, Mason, Soldier and Cavalryman by translating their Move and Fight methods into strategies. Each Strategy is declared within an interface and an unlimited number of implementations are then possible for this strategy. In our example, we can declare …&lt;br /&gt;&lt;ul&gt;&lt;li&gt;A couple of strategies for Fighting – Is Fighting and Cannot Fight.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Three strategies for Moving – Moving Fast, Moving Slowly, Riding a horse&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Each strategy interface provides the methods required for the strategy. Let us consider these interfaces - FightStrategy interface for providing the Fight functionality and the MoveStrategy interface for providing the Move functionality.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;FightStrategy Interface Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt; FightStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;MoveStrategy Interface Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt; MoveStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Interface&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;We can then implement the actual Fight strategies using the RealFighter and the WimpyFella classes where each class implements the FightStrategy providing a unique implementation for the Fight method.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;RealFighter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; RealFighter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; FightStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; FightStrategy.Fight&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is fighting"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;WimpyFella Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; WimpyFella&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; FightStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; FightStrategy.Fight&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character cannot fight"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;In the same fashion, let us create the three Move strategies based on our requirement – FastMover, SlowMover and the RidingMover classes that implement the Move method of the MoveStrategy &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;FastMover Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; FastMover&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; MoveStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; MoveStrategy.Move&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving fast"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;SlowMover Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; SlowMover&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; MoveStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; MoveStrategy.Move&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is moving slowly"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;RidingMover Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; RidingMover&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; MoveStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move() &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; MoveStrategy.Move&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is riding a horse"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;We can then declare instance variables in the original GameCharacter class to represent the Fight and Move strategies. I generally use the naming convention that prefixes class names with the lower case letter o to represent objects. I will declare two variables – oMoveStrategy and oFightStrategy in my GameCharacter class. The default implementation of the Eat and Talk method can remain unchanged while the Move and Fight methods will delegate the responsibility of using the appropriate strategy to the instance variables. Although, I have left the instance variables at a Protected access scope, you might decide to use a Private scope and restrict the access via get/set methods. For sake of simplicity,  I have defined a constructor that requires the two strategies. This will prevent invocation of the object’s Fight and Move methods without setting the two strategies.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;GameCharacter Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Protected&lt;/span&gt; oFightStrategy &lt;span style="color: blue;"&gt;As&lt;/span&gt; FightStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Protected&lt;/span&gt; oMoveStrategy &lt;span style="color: blue;"&gt;As&lt;/span&gt; MoveStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oFightStrategy &lt;span style="color: blue;"&gt;As&lt;/span&gt; FightStrategy, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; oMoveStrategy &lt;span style="color: blue;"&gt;As&lt;/span&gt; MoveStrategy)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;With&lt;/span&gt; &lt;span style="color: blue;"&gt;Me&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; .oFightStrategy = oFightStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; .oMoveStrategy = oMoveStrategy&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Eat()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is eating"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Talk()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Character is talking"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oFightStrategy.Fight()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; oMoveStrategy.Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;We can now redefine our characters to extend the Gamecharacter class and all we need to provide is the correct instantiation parameters for the instance of the child class.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Peasant Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Peasant&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;MyBase&lt;/span&gt;.New(&lt;span style="color: blue;"&gt;New&lt;/span&gt; WimpyFella, &lt;span style="color: blue;"&gt;New&lt;/span&gt; SlowMover)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Soldier Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Soldier&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;MyBase&lt;/span&gt;.New(&lt;span style="color: blue;"&gt;New&lt;/span&gt; RealFighter, &lt;span style="color: blue;"&gt;New&lt;/span&gt; FastMover)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;CavalryMan Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; CavalryMan&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;MyBase&lt;/span&gt;.New(&lt;span style="color: blue;"&gt;New&lt;/span&gt; RealFighter, &lt;span style="color: blue;"&gt;New&lt;/span&gt; RidingMover)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Mason Class Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; Mason&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;MyBase&lt;/span&gt;.New(&lt;span style="color: blue;"&gt;New&lt;/span&gt; WimpyFella, &lt;span style="color: blue;"&gt;New&lt;/span&gt; FastMover)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Well, that’s all there is to it.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3qVWJAHrpI/AAAAAAAAAEc/DaN1CSlP1Ng/s1600-h/Strategy05.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R3qVWJAHrpI/AAAAAAAAAEc/DaN1CSlP1Ng/s400/Strategy05.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150593331457666706" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To demonstrate the functionality, I built a console application around these classes.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;modMain Module Listing&lt;/strong&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;Module&lt;/span&gt; modMain&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; oGC &lt;span style="color: blue;"&gt;As&lt;/span&gt; GameCharacter&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Peasant"&lt;/span&gt;) : oGC = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Peasant : oGC.Fight() : oGC.Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Soldier"&lt;/span&gt;) : oGC = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Soldier : oGC.Fight() : oGC.Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"CavalryMan"&lt;/span&gt;) : oGC = &lt;span style="color: blue;"&gt;New&lt;/span&gt; CavalryMan : oGC.Fight() : oGC.Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Console.WriteLine(&lt;span style="color: maroon;"&gt;"Mason"&lt;/span&gt;) : oGC = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Mason : oGC.Fight() : oGC.Move()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;And the results of our sample test application&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3qV0ZAHrqI/AAAAAAAAAEk/SPf6RmRis5A/s1600-h/Strategy06.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R3qV0ZAHrqI/AAAAAAAAAEk/SPf6RmRis5A/s400/Strategy06.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5150593851148709538" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-4986587425787265149?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/4986587425787265149/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=4986587425787265149' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/4986587425787265149'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/4986587425787265149'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2008/01/strategy-design-pattern.html' title='Strategy Design Pattern'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_MaX5fUFKGdU/R3qOQ5AHrlI/AAAAAAAAAD8/5crP9WXqEsA/s72-c/Strategy01.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-1115987643044591485</id><published>2007-12-17T21:45:00.000-08:00</published><updated>2007-12-17T22:16:38.620-08:00</updated><title type='text'>Application Launch Directory</title><content type='html'>A lot of Windows based applications generally require one or more files that are consumed by the application to be placed in the same folder hierarchy as the application. For example, if I need a couple of XML files, I generally create a folder called &lt;em&gt;XMLData&lt;/em&gt; and place the files inside it.&lt;br /&gt;&lt;br /&gt;Although accessing these file paths in code is pretty straightforward, there is a small caveat to it. A lot of users use the System.Environment.CurrentDirectory&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2dibJAHriI/AAAAAAAAADg/lzQA5maDgVs/s1600-h/SystemCurrDirectory.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2dibJAHriI/AAAAAAAAADg/lzQA5maDgVs/s400/SystemCurrDirectory.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5145189317706493474" /&gt;&lt;/a&gt;&lt;br /&gt;This works well in most situations. Note how the tooltip states - "the directory from which this process starts". &lt;br /&gt;&lt;br /&gt;However, if your application contains any piece of code that can alter the current working directory - say, you use a OpenFileDialog control to pick a file on the file system but do not restore the working directory by setting the RestoreDirectory property, this results in changing the current working directory and the quoted portion of the tooltip no longer holds good.&lt;br /&gt;&lt;br /&gt;I would recommend using the Application.StartupPath instead of the System.Environment.CurrentDirectory. The folder path returned by this function is not altered during the course of execution of the application.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2djepAHrjI/AAAAAAAAADo/GhzM1dtDXMg/s1600-h/AppStartDirectory.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2djepAHrjI/AAAAAAAAADo/GhzM1dtDXMg/s400/AppStartDirectory.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5145190477347663410" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Console applications do not have access to the Application object and hence you might need to use the System.Reflection.Assembly.GetExecutingAssembly.Location method to get the full path of the console application. You can then use the IO.Path.GetDirectoryName method to extract the application launch directory.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2dlY5AHrkI/AAAAAAAAADw/D2LxbOtlZv4/s1600-h/ConsAppDir.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2dlY5AHrkI/AAAAAAAAADw/D2LxbOtlZv4/s400/ConsAppDir.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5145192577586671170" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-1115987643044591485?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/1115987643044591485/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=1115987643044591485' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/1115987643044591485'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/1115987643044591485'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/application-launch-directory.html' title='Application Launch Directory'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R2dibJAHriI/AAAAAAAAADg/lzQA5maDgVs/s72-c/SystemCurrDirectory.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-6463468986192188763</id><published>2007-12-16T11:50:00.001-08:00</published><updated>2007-12-16T12:11:01.689-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Combobox'/><category scheme='http://www.blogger.com/atom/ns#' term='DataGridView'/><category scheme='http://www.blogger.com/atom/ns#' term='Dynamic ComboBox'/><title type='text'>Dynamic ComboBox in a DataGridView</title><content type='html'>And here is the second installment. It is generally convenient to bind the grid column to a single datasource such that all dropdowns have identical items. However, certain applications require that the items in the dropdown depend upon other attributes of the row.&lt;br /&gt; &lt;br /&gt;For this example, I will create a three column grid with the first column containing the Employee Name, the second one containing the Department and third one being the dynamic combobox column that lists the positions for the particular department.&lt;br /&gt;&lt;br /&gt;Once again, I have created the datasources at runtime to provide a complete example. To add that dynamic feel, I have decided to handle a DataGridView event - CellValueChanged. You can of course set this value after binding the original gridview source as well.&lt;br /&gt; &lt;br /&gt;The Setup code that declares two dummy datasources and binds the Employee datasource with the gridview.&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Create the sample list of names&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dtNames &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtNames.Columns.Add(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;String&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtNames.Columns.Add(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;String&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dr &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataRow&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Alvin Menezes"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;""&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Saikumar Chettiar"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;""&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Sample Name 1"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;""&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Sample Name 2"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;""&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'create the sample department - position choices&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtChoices = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtChoices.Columns.Add(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;String&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtChoices.Columns.Add(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;String&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtChoices.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"IT"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Tech Architect"&lt;/span&gt; : dtChoices.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtChoices.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"IT"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Project Manager"&lt;/span&gt; : dtChoices.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtChoices.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"IT"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Project Leader"&lt;/span&gt; : dtChoices.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtChoices.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"IT"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Developer"&lt;/span&gt; : dtChoices.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtChoices.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"HR"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Manager"&lt;/span&gt; : dtChoices.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtChoices.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Department"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"HR"&lt;/span&gt; : dr.Item(&lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Executive"&lt;/span&gt; : dtChoices.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Bind the datagridview&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dgvSampleEx.AutoGenerateColumns = &lt;span style="color: blue;"&gt;False&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dgvSampleEx.DataSource = dtNames&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The code listing for the dynamic ComboBox rendering&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; dgvSampleEx_CellValueChanged(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.Windows.Forms.DataGridViewCellEventArgs) &lt;span style="color: blue;"&gt;Handles&lt;/span&gt; dgvSampleEx.CellValueChanged&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;If&lt;/span&gt; e.RowIndex &amp;lt; 0 &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Exit&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;If&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;If&lt;/span&gt; e.ColumnIndex = 1 &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; Department &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt; = &lt;span style="color: blue;"&gt;CStr&lt;/span&gt;(dgvSampleEx.Rows(e.RowIndex).Cells(1).Value)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dv &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataView = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataView(dtChoices, &lt;span style="color: maroon;"&gt;"Department = '"&lt;/span&gt; &amp;amp; Department &amp;amp; &lt;span style="color: maroon;"&gt;"'"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;""&lt;/span&gt;, DataViewRowState.CurrentRows)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dgvComboCell &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataGridViewComboBoxCell = &lt;span style="color: blue;"&gt;CType&lt;/span&gt;(dgvSampleEx.Rows(e.RowIndex).Cells(2), DataGridViewComboBoxCell)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;With&lt;/span&gt; dgvComboCell&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .DisplayMember = &lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .ValueMember = &lt;span style="color: maroon;"&gt;"Position"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .DataSource = dv&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;If&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;Guess the trick is to know that binding a DataGridView column sets all dropdown elements to be identical whereas binding the DataGridViewCell gives you more control over the exact elements that need to be displayed in the dropdown choices.&lt;br /&gt;&lt;br /&gt;And the results for all to see&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2WE2pAHrfI/AAAAAAAAADI/2JiKbGvarbs/s1600-h/DynamicComboGridResult0.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2WE2pAHrfI/AAAAAAAAADI/2JiKbGvarbs/s400/DynamicComboGridResult0.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144664223594819058" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2WE9ZAHrgI/AAAAAAAAADQ/fRiW8I1sqEQ/s1600-h/DynamicComboGridResult1.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2WE9ZAHrgI/AAAAAAAAADQ/fRiW8I1sqEQ/s400/DynamicComboGridResult1.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144664339558936066" /&gt;&lt;/a&gt;&lt;br /&gt;Note how the dropdown values differ - depending on the Department that has been entered. Ideally, the Department could also be rendered as a combobox. I decided to go with plain text to keep this example simple.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-6463468986192188763?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/6463468986192188763/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=6463468986192188763' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/6463468986192188763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/6463468986192188763'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/dynamic-combobox-in-datagridview.html' title='Dynamic ComboBox in a DataGridView'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R2WE2pAHrfI/AAAAAAAAADI/2JiKbGvarbs/s72-c/DynamicComboGridResult0.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-5514992119190135483</id><published>2007-12-16T11:29:00.000-08:00</published><updated>2007-12-16T11:49:56.600-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Combobox'/><category scheme='http://www.blogger.com/atom/ns#' term='DataGridView'/><title type='text'>ComboBox in a DataGridView</title><content type='html'>It is often useful to add a dropdown box in a DataGridView column. I will list a couple of methods that can be used to achieve this. Of course, your real world applications will differ in certain areas related to the source of data. For the sake of this example, I have created these datasources at runtime.&lt;br /&gt;The first example deals with displaying a two column grid with the first column containing the Employee Name and the second column that can be used to collect the Employee Gender&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Drag a DataGridView on the form&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Expand the DataGridView tasks and add a couple of columns&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2V_npAHrbI/AAAAAAAAACo/tddXXzzaUc8/s1600-h/Column0.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2V_npAHrbI/AAAAAAAAACo/tddXXzzaUc8/s400/Column0.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144658468338642354" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2V_spAHrcI/AAAAAAAAACw/1IVDG7YLQrs/s1600-h/Column1.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2V_spAHrcI/AAAAAAAAACw/1IVDG7YLQrs/s400/Column1.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144658554237988290" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;li&gt;Set the DataPropertyName for the first column appropriately, Since my data source deals with Employee Names, I have selected 'Name' which corresponds to the column name in my datasource.&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R2WAV5AHrdI/AAAAAAAAAC4/jky8WnrTxtw/s1600-h/Column0_Change.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R2WAV5AHrdI/AAAAAAAAAC4/jky8WnrTxtw/s400/Column0_Change.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144659262907592146" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/li&gt; &lt;br /&gt;&lt;li&gt;Finally, bind the data source with the grid and another datasource with the second column so that the dropdown choices are rendered correctly.&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Create the sample list of names&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dtNames &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtNames.Columns.Add(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;String&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dr &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataRow&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Alvin Menezes"&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Saikumar Chettiar"&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Sample Name 1"&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtNames.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Sample Name 2"&lt;/span&gt; : dtNames.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Create the sample list of dropdown choices &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dtGenders &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataTable = &lt;span style="color: blue;"&gt;New&lt;/span&gt; DataTable&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtGenders.Columns.Add(&lt;span style="color: maroon;"&gt;"Gender"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;String&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dtGenders.Columns.Add(&lt;span style="color: maroon;"&gt;"GenderId"&lt;/span&gt;, &lt;span style="color: blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue;"&gt;Integer&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtGenders.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"GenderId"&lt;/span&gt;) = 1 : dr.Item(&lt;span style="color: maroon;"&gt;"Gender"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Male"&lt;/span&gt; : dtGenders.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dr = dtGenders.NewRow : dr.Item(&lt;span style="color: maroon;"&gt;"GenderId"&lt;/span&gt;) = 2 : dr.Item(&lt;span style="color: maroon;"&gt;"Gender"&lt;/span&gt;) = &lt;span style="color: maroon;"&gt;"Female"&lt;/span&gt; : dtGenders.Rows.Add(dr)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Bind the datagridview&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dgvSample.AutoGenerateColumns = &lt;span style="color: blue;"&gt;False&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; dgvSample.DataSource = dtNames&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: green;"&gt;'Setup the Combobox column&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; dgvComboCol &lt;span style="color: blue;"&gt;As&lt;/span&gt; DataGridViewComboBoxColumn = &lt;span style="color: blue;"&gt;CType&lt;/span&gt;(dgvSample.Columns(1), DataGridViewComboBoxColumn)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;With&lt;/span&gt; dgvComboCol&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; .DisplayMember = &lt;span style="color: maroon;"&gt;"Gender"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; .ValueMember = &lt;span style="color: maroon;"&gt;"GenderId"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; .DataSource = dtGenders&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;And that's it, we are good to go&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2WA-JAHreI/AAAAAAAAADA/JV-2VSwvWkE/s1600-h/ComboInaGridResult0.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2WA-JAHreI/AAAAAAAAADA/JV-2VSwvWkE/s400/ComboInaGridResult0.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144659954397326818" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-5514992119190135483?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/5514992119190135483/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=5514992119190135483' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5514992119190135483'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5514992119190135483'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/combobox-in-datagridview.html' title='ComboBox in a DataGridView'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R2V_npAHrbI/AAAAAAAAACo/tddXXzzaUc8/s72-c/Column0.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-5640965759697483977</id><published>2007-12-15T10:25:00.000-08:00</published><updated>2007-12-15T10:53:33.054-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Scrolling PictureBox'/><category scheme='http://www.blogger.com/atom/ns#' term='Picturebox scrollbar'/><title type='text'>Scrollbar on a PictureBox, Nah!</title><content type='html'>The quickest way to add a scrollbar to a PictureBox is not to add one to it. Instead drag a Panel on the Form. Set its AutoScroll property to True.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2QhG5AHrZI/AAAAAAAAABk/Q8tOuWxxgao/s1600-h/PanelPictureBoxSample.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2QhG5AHrZI/AAAAAAAAABk/Q8tOuWxxgao/s400/PanelPictureBoxSample.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144273076628204946" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now, drag a PictureBox on this panel, such that the panel becomes the parent container of this PictureBox control. Set the SizeMode property of the PictureBox to AutoSize.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2QepZAHrXI/AAAAAAAAABU/YjidGf1anvs/s1600-h/PictureBoxSample.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2QepZAHrXI/AAAAAAAAABU/YjidGf1anvs/s400/PictureBoxSample.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144270370798808434" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Ensure that you set the location of the PictureBox to 0,0. This will align the PictureBox to the top left of the Panel.&lt;br /&gt;&lt;br /&gt;And... well nothing. That's it. You can load any image in the PictureBox at Design / Run time and the panel will do the rest for you.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2QhP5AHraI/AAAAAAAAABs/_MHCAzeuxy0/s1600-h/ScrollingPicture.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2QhP5AHraI/AAAAAAAAABs/_MHCAzeuxy0/s400/ScrollingPicture.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144273231247027618" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-5640965759697483977?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/5640965759697483977/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=5640965759697483977' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5640965759697483977'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/5640965759697483977'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/scrollbar-on-picturebox-nah.html' title='Scrollbar on a PictureBox, Nah!'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R2QhG5AHrZI/AAAAAAAAABk/Q8tOuWxxgao/s72-c/PanelPictureBoxSample.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-6182422902984358208</id><published>2007-12-15T00:44:00.000-08:00</published><updated>2007-12-15T01:00:16.416-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='XPath Query'/><category scheme='http://www.blogger.com/atom/ns#' term='XML'/><category scheme='http://www.blogger.com/atom/ns#' term='nth node from a nodelist'/><title type='text'>Basic XPath Query</title><content type='html'>Consider the following the XML segment&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;tests&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Sample Test 1&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Passed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;0&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Failed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;0&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Sample Test 2&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Passed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;0&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Failed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;0&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Sample Test 3&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Passed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;1&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Failed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;0&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Sample Test 4&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Passed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;0&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;result&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Failed&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;status&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;1&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;test&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;tests&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Note: xdoc in the following example refers to an instance of Xml.XmlDocument  that contains this XML document&lt;br /&gt;&lt;ul&gt;&lt;li&gt;To retrieve all test nodes from this XML Block, we can use the basic XPath query -&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/tests/test"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;To retrieve all result nodes that have the type attribute set to "Passed", &lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/tests/test/result[@type='Passed'"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;To retrieve all result nodes that have the type attribute set to "Failed" and having a status of "0",&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/tests/test/result[@type='Failed' and @status='0'"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;To retrieve the second test node, we can use the following query&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;&lt;p style="margin: 0px;"&gt;xDoc.SelectNodes(&lt;span style="color: maroon;"&gt;"/tests/test[position()=2]"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;The last one is especially useful if you do not have unique node identifier attributes and need to access the nth item from the node list. Of course, the other alternative is to use a &lt;em&gt;for each&lt;/em&gt; loop and skip n-1 items from the top. But this is a far more elegant solution.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-6182422902984358208?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/6182422902984358208/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=6182422902984358208' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/6182422902984358208'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/6182422902984358208'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/basic-xpath-query.html' title='Basic XPath Query'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-7332891303435350007</id><published>2007-12-14T20:09:00.000-08:00</published><updated>2007-12-14T21:43:08.541-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='DrawItem'/><category scheme='http://www.blogger.com/atom/ns#' term='Combobox'/><category scheme='http://www.blogger.com/atom/ns#' term='MeasureItem'/><category scheme='http://www.blogger.com/atom/ns#' term='different colored listitems'/><title type='text'>Multi Colored ComboboxListItems</title><content type='html'>&lt;h5&gt;Requirement&lt;/h5&gt;&lt;br /&gt;    Consider this, a list of Students and their overall performance. For the sake of this example, I will simply rate the performance at two levels&lt;br /&gt;    &lt;ul&gt;&lt;li&gt;80% and above&lt;/li&gt;&lt;li&gt;below 80%&lt;/li&gt;&lt;/ul&gt;To easily discern between students at these performance levels, we can display the student listitems in different colors using the Combobox control.&lt;br /&gt;    &lt;h5&gt;A little about the Combobox Item rendering&lt;/h5&gt;Listitems in a combobox are rendered in one of two ways - an unselected listitem and a selected listitem.&lt;br /&gt;    &lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NX5JAHrPI/AAAAAAAAAAU/uQ4-U9d8ByY/s1600-h/NormalComboboxItems.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NX5JAHrPI/AAAAAAAAAAU/uQ4-U9d8ByY/s400/NormalComboboxItems.gif" border="0" alt=""id="Img1" /&gt;&lt;/a&gt;&lt;br /&gt;    Since we would like to render the Student Listitems based on the performance factor at two levels, we will require a total of four color combinations - &lt;br /&gt;    &lt;ul&gt;&lt;li&gt;Students whose overall performance meets or exceeds 80%&lt;table cellspacing="0" cellpadding="0" border="0"&gt;&lt;br /&gt;    &lt;tr&gt;&lt;td&gt;Normal unselected&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2NZw5AHrQI/AAAAAAAAAAc/zCIGbHY0FKo/s1600-h/UnselectedTypeAListItem.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2NZw5AHrQI/AAAAAAAAAAc/zCIGbHY0FKo/s400/UnselectedTypeAListItem.gif" border="0" alt=""id="Img5" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;    &lt;tr&gt;&lt;td&gt;Selected&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NZ5JAHrSI/AAAAAAAAAAs/DauuIab9lxQ/s1600-h/SelectedTypeAListItem.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NZ5JAHrSI/AAAAAAAAAAs/DauuIab9lxQ/s400/SelectedTypeAListItem.gif" border="0" alt=""id="Img3" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;    &lt;/table&gt;&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Students whose overall performance is below 80%&lt;table cellspacing="0" cellpadding="0" border="0"&gt;&lt;br /&gt;    &lt;tr&gt;&lt;td&gt;Normal unselected&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;a href="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NZ2JAHrRI/AAAAAAAAAAk/Hu-JiCb-CDU/s1600-h/UnselectedTypeBListItem.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NZ2JAHrRI/AAAAAAAAAAk/Hu-JiCb-CDU/s400/UnselectedTypeBListItem.gif" border="0" alt=""id="Img4" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;    &lt;tr&gt;&lt;td&gt;Selected&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2Ncb5AHrUI/AAAAAAAAAA8/fe1hp9pnBtM/s1600-h/SelectedTypeBListitem.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_MaX5fUFKGdU/R2Ncb5AHrUI/AAAAAAAAAA8/fe1hp9pnBtM/s400/SelectedTypeBListitem.gif" border="0" alt=""id="Img2" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;    &lt;/table&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;  &lt;h5&gt;Lets dig in&lt;/h5&gt;&lt;br /&gt;  Having defined the colors that we can use for rendering the listitems, here are the steps that are required&lt;br /&gt;  &lt;ol&gt;&lt;li&gt;Change the DrawMode property of the Combobox to OwnerDrawVariable. The default value is Normal.&lt;br /&gt;    &lt;a href="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2NWIpAHrOI/AAAAAAAAAAM/QXulivBirYE/s1600-h/ComboboxDrawMode.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_MaX5fUFKGdU/R2NWIpAHrOI/AAAAAAAAAAM/QXulivBirYE/s400/ComboboxDrawMode.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5144049905832537314" /&gt;&lt;/a&gt;&lt;br /&gt;   &lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Handle the MeasureItem event of the Combobox. This allows for manipulating the size of each Listitem. In this current example, I have simply set a fixed height and width, however based on the value of e.Index and its corresponding Listitem element, you can change these values if you wish to render the Listitems differently.&lt;br /&gt;    &lt;div style="font-family:Courier New; font-size:10pt; background-color:#dcdcdc;"&gt;&lt;br /&gt;     &lt;span style="color:Blue;"&gt;Private Sub&lt;/span&gt; cmbStudents_MeasureItem(&lt;span style="color:Blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:Blue;"&gt;As Object&lt;/span&gt;, &lt;span style="color:Blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:Blue;"&gt;As&lt;/span&gt; System.Windows.Forms.MeasureItemEventArgs) _&lt;br /&gt;     &lt;span style="color:Blue;"&gt;&amp;nbsp;Handles&lt;/span&gt; cmbStudents.MeasureItem&lt;br /&gt;     &lt;br /&gt;     &amp;nbsp;&amp;nbsp;e.ItemHeight = 18 &lt;span style="color: green"&gt;'Pick this based on desired height of the listitem&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;e.ItemWidth = cmbStudents.Width&lt;br /&gt;     &lt;span style="color:Blue;"&gt;End Sub&lt;/span&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;   &lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Handle the DrawItem event of the Combobox. Like the event name suggests, this event allows you to perform a custom draw of the Listitem every time the Listitem is displayed. For our example, it is sufficient to know that this event is fired each time a user expands the Combobox to view a list of items as well runs his mouse over the expanded list resulting in a hover selection of that listitem.&lt;br /&gt;    &lt;div style="font-family:Courier New; font-size:10pt; background-color:#dcdcdc;"&gt;&lt;br /&gt;     &lt;span style="color:Blue;"&gt;Private Sub&lt;/span&gt; cmbStudents_DrawItem(&lt;span style="color:Blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:Blue;"&gt;As Object&lt;/span&gt;, &lt;span style="color:Blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:Blue;"&gt;As&lt;/span&gt; System.Windows.Forms.DrawItemEventArgs) _&lt;br /&gt;     &amp;nbsp;&lt;span style="color:Blue;"&gt;Handles&lt;/span&gt; cmbStudents.DrawItem&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;If&lt;/span&gt; e.Index &lt; 0 &lt;span style="color:Blue;"&gt;Then&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Exit Sub&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End If&lt;/span&gt;&lt;br /&gt;     &lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:green;"&gt;'Get the Student object that needs to be rendered using the e.Index&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Dim&lt;/span&gt; oStudent &lt;span style="color:Blue;"&gt;As&lt;/span&gt; Student = &lt;span style="color:Blue;"&gt;CType&lt;/span&gt;(cmbStudents.Items(e.Index), Student)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Dim&lt;/span&gt; DisplayStr &lt;span style="color:Blue;"&gt;As String&lt;/span&gt; = oStudent.Name&lt;br /&gt;     &lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Dim&lt;/span&gt; r &lt;span style="color:Blue;"&gt;As&lt;/span&gt; RectangleF = &lt;span style="color:Blue;"&gt;New&lt;/span&gt; RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)&lt;br /&gt;     &lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;If&lt;/span&gt; (e.State &lt;span style="color:Blue;"&gt;And&lt;/span&gt; DrawItemState.Selected) = DrawItemState.Selected &lt;span style="color:Blue;"&gt;Then&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;If&lt;/span&gt; oStudent.OverallPerformanceScore &gt; 80 &lt;span style="color:Blue;"&gt;Then&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.FillRectangle(Brushes.Lavender, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(DisplayStr, e.Font, System.Drawing.Brushes.Blue, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Else&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.FillRectangle(Brushes.AntiqueWhite, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(DisplayStr, e.Font, System.Drawing.Brushes.Red, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End If&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Else&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;If&lt;/span&gt; oStudent.OverallPerformanceScore &gt; 80 &lt;span style="color:Blue;"&gt;Then&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.FillRectangle(Brushes.GhostWhite, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(DisplayStr, e.Font, System.Drawing.Brushes.Blue, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Else&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.FillRectangle(Brushes.SeaShell, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(DisplayStr, e.Font, System.Drawing.Brushes.Red, r)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End If&lt;/span&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End If&lt;/span&gt;&lt;br /&gt;     &lt;br /&gt;     &amp;nbsp;&amp;nbsp;' Draw the focus rectangle if mouse hovers over the item.&lt;br /&gt;     &amp;nbsp;&amp;nbsp;e.DrawFocusRectangle()&lt;br /&gt;     &lt;span style="color:Blue;"&gt;End Sub&lt;/span&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;   &lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;  &lt;h5&gt;The Result&lt;/h5&gt;&lt;br /&gt;  &lt;a href="http://2.bp.blogspot.com/_MaX5fUFKGdU/R2NldZAHrVI/AAAAAAAAABE/YpskZPuX7zU/s1600-h/MulticoloredComboboxListitems.gif"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_MaX5fUFKGdU/R2NldZAHrVI/AAAAAAAAABE/YpskZPuX7zU/s400/MulticoloredComboboxListitems.gif" border="0" alt=""id="Img6" /&gt;&lt;/a&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;  &lt;h5&gt;Student Class Listing&lt;/h5&gt;&lt;br /&gt;  &lt;div style="font-family:Courier New; font-size:10pt; background-color:#dcdcdc;"&gt;&lt;br /&gt;   &lt;span style="color:Blue;"&gt;Private Class&lt;/span&gt; Student&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Private&lt;/span&gt; _Name &lt;span style="color:Blue;"&gt;As String&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Private&lt;/span&gt; _OverallPerformanceScore &lt;span style="color:Blue;"&gt;As Double&lt;/span&gt;&lt;br /&gt;   &lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Public Sub New&lt;/span&gt;(&lt;span style="color:Blue;"&gt;ByVal&lt;/span&gt; Name &lt;span style="color:Blue;"&gt;As String&lt;/span&gt;, &lt;span style="color:Blue;"&gt;ByVal&lt;/span&gt; OverallPerformanceScore &lt;span style="color:Blue;"&gt;As Double&lt;/span&gt;)&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_Name = Name&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_OverallPerformanceScore = OverallPerformanceScore&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End Sub&lt;/span&gt;&lt;br /&gt;   &lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Public ReadOnly Property&lt;/span&gt; Name() &lt;span style="color:Blue;"&gt;As String&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Get&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Return&lt;/span&gt; _Name&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End Get&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End Property&lt;/span&gt;&lt;br /&gt;   &lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Public ReadOnly Property&lt;/span&gt; OverallPerformanceScore() &lt;span style="color:Blue;"&gt;As Double&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Get&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Return&lt;/span&gt; _OverallPerformanceScore&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End Get&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End Property&lt;/span&gt;&lt;br /&gt;   &lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Public Overrides Function&lt;/span&gt; ToString() &lt;span style="color:Blue;"&gt;As String&lt;/span&gt;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;Return&lt;/span&gt; Name&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&lt;span style="color:Blue;"&gt;End Function&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:Blue;"&gt;End Class&lt;/span&gt;&lt;br /&gt;  &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-7332891303435350007?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/7332891303435350007/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=7332891303435350007' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/7332891303435350007'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/7332891303435350007'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/multi-colored-comboboxlistitems.html' title='Multi Colored ComboboxListItems'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_MaX5fUFKGdU/R2NX5JAHrPI/AAAAAAAAAAU/uQ4-U9d8ByY/s72-c/NormalComboboxItems.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8757932313454988957.post-995503921536624420</id><published>2007-12-13T23:46:00.000-08:00</published><updated>2007-12-18T14:46:23.395-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Regular Expressions'/><category scheme='http://www.blogger.com/atom/ns#' term='Remove Duplicate Spaces'/><title type='text'>Remove Duplicate Whitespaces using Regular Expressions</title><content type='html'>A quick way to eliminate duplicate white spaces from a string using regular expressions using VB.Net&lt;br /&gt;&lt;br /&gt;    &lt;div style="font-family:Courier New; font-size:10pt; background-color:#dcdcdc;"&gt;&lt;br /&gt;   &lt;span style="color:Blue;"&gt;Dim&lt;/span&gt; DirtySample &lt;span style="color:Blue;"&gt;As String&lt;/span&gt; = &lt;span style="color:Maroon;"&gt;"The quick   brown fox "&lt;/span&gt; &amp; ControlChars.CrLf &amp; &lt;span style="color:Maroon;"&gt;" jumped"&lt;/span&gt; &amp; ControlChars.Tab &amp; ControlChars.Tab &amp; &lt;span style="color:Maroon;"&gt;" over lazy dog"&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:Blue;"&gt;Dim&lt;/span&gt; CleanSample &lt;span style="color:Blue;"&gt;As String&lt;/span&gt; = System.Text.RegularExpressions.Regex.Replace(DirtySample, &lt;span style="color:Maroon;"&gt;"\s+"&lt;/span&gt;, &lt;span style="color:Maroon;"&gt;" "&lt;/span&gt;)&lt;br /&gt;   &lt;br /&gt;   Debug.WriteLine(&lt;span style="color:Maroon;"&gt;"DirtySample : "&lt;/span&gt;)&lt;br /&gt;   Debug.WriteLine(DirtySample)&lt;br /&gt;   &lt;br /&gt;   Debug.WriteLine(&lt;span style="color:Maroon;"&gt;"CleanSample : "&lt;/span&gt;)&lt;br /&gt;   Debug.WriteLine(CleanSample)&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;    Output&lt;br /&gt;    &lt;div style="font-family:Courier New; font-size:10pt; background-color:#ffffcc;"&gt;&lt;br /&gt;   DirtySample :&lt;br /&gt;   The quick   brown fox &lt;br /&gt;   &amp;nbsp;jumped   over lazy dog&lt;br /&gt;   CleanSample : &lt;br /&gt;   The quick brown fox jumped over lazy dog&lt;br /&gt;    &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8757932313454988957-995503921536624420?l=codeedifice.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeedifice.blogspot.com/feeds/995503921536624420/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8757932313454988957&amp;postID=995503921536624420' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/995503921536624420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8757932313454988957/posts/default/995503921536624420'/><link rel='alternate' type='text/html' href='http://codeedifice.blogspot.com/2007/12/remove-duplicate-white-spaces-using.html' title='Remove Duplicate Whitespaces using Regular Expressions'/><author><name>Alvin Menezes</name><uri>http://www.blogger.com/profile/08095249989109672573</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_MaX5fUFKGdU/R45fNJAHsAI/AAAAAAAAAHw/Ug_laPf8G88/S220/AlvinMenezes.jpg'/></author><thr:total>0</thr:total></entry></feed>
