<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Deborah's Developer MindScape : OOP</title><link>http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx</link><description>Tags: OOP</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Collection Initializers</title><link>http://msmvps.com/blogs/deborahk/archive/2010/04/29/collection-initializers.aspx</link><pubDate>Thu, 29 Apr 2010 22:08:07 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1764572</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1764572</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2010/04/29/collection-initializers.aspx#comments</comments><description>&lt;p&gt;One of the new features in VB 10.0 is collection initializers. Collection initializers allow you to initialize an array or list in a single line of code.&lt;/p&gt;  &lt;p&gt;(C# has had collection initializers since C# 3.0. The examples here show both C# and VB for completeness.)&lt;/p&gt;  &lt;p&gt;This example uses the customer class &lt;a href="http://msmvps.com/blogs/deborahk/archive/2010/04/11/auto-implemented-properties.aspx"&gt;defined here&lt;/a&gt; and builds a list of customers using collection initializers.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim custList As New List(Of Customer) &lt;strong&gt;From&lt;/strong&gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {New Customer() With       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {.CustomerId = 1,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Bilbo&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Baggins&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;bb@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; New Customer() With       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {.CustomerId = 2,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Frodo&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Baggins&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;fb@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; New Customer() With       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {.CustomerId = 3,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Samwise&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Gamgee&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;sg@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; New Customer() With       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {.CustomerId = 4,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Rosie&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Cotton&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;rc@hob.me&amp;quot;}}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;List&amp;lt;Customer&amp;gt; custList = new List&amp;lt;Customer&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 1,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Bilbo&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Baggins&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;bb@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 2,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Frodo&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Baggins&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;fb@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 3,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Samwise&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Gamgee&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;sg@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 4,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Rosie&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Cotton&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;&lt;/font&gt;&lt;a href="mailto:rc@hob.me"&gt;&lt;font color="#65402e" face="Consolas"&gt;rc@hob.me&lt;/font&gt;&lt;/a&gt;&amp;quot;&lt;font color="#65402e" face="Consolas"&gt;}};&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Notice how these two code snippets are very similar. VB even has the same curly braces!&lt;/p&gt;  &lt;p&gt;The key differences are that VB requires the From keyword when initializing the list, the With keyword when initializing each customer in the list, and the &amp;quot;.&amp;quot; in front of the property names.&lt;/p&gt;  &lt;p&gt;Compare this VB example to the one shown for VB 9 (VS 2008) &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Use this technique whenever you need to initialize a list or array of objects.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1764572" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Generics/default.aspx">Generics</category></item><item><title>Understanding Object Binding</title><link>http://msmvps.com/blogs/deborahk/archive/2009/09/08/understanding-object-binding.aspx</link><pubDate>Tue, 08 Sep 2009 19:26:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1721653</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1721653</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/09/08/understanding-object-binding.aspx#comments</comments><description>&lt;p&gt;Before going through the details of how to use object binding, it is important to understand exactly what it is—and what it is not. Object binding is binding your business object properties to user interface elements. Object binding is not database binding in the strict sense of the term. It does not directly collect or bind any data from your database.&lt;/p&gt;  &lt;p&gt;When you are using business object classes &lt;em&gt;without&lt;/em&gt; object binding, the flow of data from the database to your user interface and back again requires these steps:&lt;/p&gt;  &lt;p&gt;1. The &lt;strong&gt;business object calls the data access component&lt;/strong&gt; to get the data from the database and sets the business object properties using that data.&lt;/p&gt;  &lt;p&gt;For example, the Product class calls the data access component, which uses a query or stored procedure to fill a DataTable from the Product table. The data access component returns the DataTable to the Product class, which assigns each field from the table to a property of the object. To illustrate, the line of code required to get the ProductName field from the DataTable and set the    &lt;br /&gt;ProductName property is as follows:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;myProduct.ProductName = dt.Rows[0][&amp;quot;ProductName&amp;quot;];&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;myProduct.ProductName = dt.Rows(0).Item(&amp;quot;ProductName&amp;quot;)&lt;/font&gt;     &lt;br /&gt;    &lt;br /&gt;2. The &lt;strong&gt;user interface component accesses the business object&lt;/strong&gt; properties to fill the values of the controls on the form.&lt;/p&gt;  &lt;p&gt;For example, each control on the ProductWin form is assigned to the value of the appropriate Product business object property. To illustrate, the line of code required to set the Text property of the Name TextBox control to the ProductName property of the Product business object is as follows:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;NameTextBox.Text = myProduct.ProductName&lt;/font&gt;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;NameTextBox.Text = myProduct.ProductName&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;3. After the user makes any changes, the user interface component assigns the current &lt;strong&gt;values in the controls back to the business object&lt;/strong&gt; properties.&lt;/p&gt;  &lt;p&gt;For example, the value in each control on the ProductWin form is assigned back to its associated Product business object property. To illustrate, the line of code required to set the ProductName property to the current value in the Name TextBox control is as follows:&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;myProduct.ProductName = NameTextBox.Text&lt;/font&gt;;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;myProduct.ProductName = NameTextBox.Text&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;4. The &lt;strong&gt;business object component updates the DataTable&lt;/strong&gt; using the property values and passes it back to the data access component, which updates the database with the changed data. &lt;/p&gt;  &lt;p&gt;For example, the value of each Product business object property is assigned to the associated field in the DataTable, and the result is passed to the data access component, which updates the Product table. To illustrate, the line of code required to set the ProductName field in the DataTable to the value of the ProductName business object property is as follows:&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;dt.Rows[0][&amp;quot;ProductName&amp;quot;] = myProduct.ProductName;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;dt.Rows(0).item(&amp;quot;ProductName&amp;quot;) = myProduct.ProductName&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Using object binding allows you to skip steps 2 and 3. Object binding automatically populates the controls on the user interface from the business object properties. As the users change the contents of the controls, object binding updates the associated business object properties, keeping them in synchronization.&lt;/p&gt;  &lt;p&gt;That still leaves steps 1 and 4 for you. This &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-data-access-layer.aspx" target="_blank"&gt;link&lt;/a&gt; provides information on building a data access component to handle steps 1 and 4.&lt;/p&gt;  &lt;p&gt;In summary, object binding is the process of binding control properties directly to properties of your business objects. For example, you could bind the Text property of a TextBox control to the ProductName property of a Product business object. When the form is displayed, the runtime automatically displays the value of the ProductName property in the TextBox. And if the user changes the text in the TextBox control, the runtime modifies the ProductName property accordingly. This saves you from writing the code required to transfer data back and forth between the controls on the user interface and the business object properties.&lt;/p&gt;  &lt;p&gt;Visual Studio provides design-time tools for working with your business objects as data sources for your user interface, making it easy to bind each control to its associated business object property. The only requirement for your business objects to work with these tools is that the business object class needs at least one public property. No specific constructors, interfaces, or attributes are needed.&lt;/p&gt;  &lt;h2&gt;Object Binding Versus Data Binding&lt;/h2&gt;  &lt;p&gt;Don’t confuse the term object binding with the more generalized term data binding. Data binding is the broad term for binding control properties to data from any data source. Object binding is just one type of data binding. Some common types of data binding are as follows:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Binding to tables in a database (Visual Studio generates code to      &lt;br /&gt;define a typed DataSet and TableAdapters) &lt;/li&gt;    &lt;li&gt;Binding to stored procedures in a database (Visual Studio generates      &lt;br /&gt;code to define a typed DataSet and TableAdapters) &lt;/li&gt;    &lt;li&gt;Binding to a business object (object binding does not generate code;      &lt;br /&gt;it just sets control properties) &lt;/li&gt;    &lt;li&gt;Binding to an array or collection of data &lt;/li&gt;    &lt;li&gt;Binding to a Web service &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;When binding to a database, Visual Studio generates a significant amount of code and then binds the user interface to that generated code. Object binding binds to &lt;strong&gt;your&lt;/strong&gt; code. That gives you much more control and greatly simplifies the maintenance of your application.     &lt;br /&gt;&lt;/p&gt;  &lt;h2&gt;Using Object Binding&lt;/h2&gt;  &lt;p&gt;You use object binding by following these steps:&lt;/p&gt;  &lt;p&gt;1. Build the business objects for your application.&lt;/p&gt;  &lt;p&gt;2. Define a business object data source in the Windows Application    &lt;br /&gt;project containing your user interface.&lt;/p&gt;  &lt;p&gt;3. Bind properties of the controls on the form to business object    &lt;br /&gt;properties.&lt;/p&gt;  &lt;p&gt;Although it is much easier to think about object binding as a direct binding of a control’s property to a specific business object’s property, object binding frequently uses a BindingSource component as an intermediary. A &lt;strong&gt;BindingSource&lt;/strong&gt; is a component on a form that binds the controls on the form to the business object. Each control is bound to the BindingSource component, which in turn is bound to the business object. This makes it much easier to change the binding for all controls by changing the BindingSource without having to separately rebind each control.&lt;/p&gt;  &lt;p&gt;You set the BindingSource to an individual business object instance in your code. The runtime then binds all the properties associated with that instance to the controls, thereby displaying the business object property values in the controls. And as the user changes the content of any controls, the business object property values are changed accordingly.&lt;/p&gt;  &lt;p&gt;A form can contain multiple BindingSource components. For example, a ProductWin form can contain product data and display a drop-down list of product types. You can define a BindingSource component for the product data and a second BindingSource component for the product type data.&lt;/p&gt;  &lt;p&gt;(Based on an except from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.)&lt;/p&gt;  &lt;p&gt;For more information on object binding, see these links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/08/using-object-binding.aspx" target="_blank"&gt;Using Object Binding&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/16/binding-control-properties-to-business-object-properties.aspx"&gt;Binding Control Properties to Business Object Properties&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1721653" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Binding/default.aspx">Binding</category></item><item><title>Basic Pillars of an Object-Oriented System</title><link>http://msmvps.com/blogs/deborahk/archive/2009/09/01/basic-pillars-of-an-object-oriented-system.aspx</link><pubDate>Tue, 01 Sep 2009 17:16:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719927</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1719927</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/09/01/basic-pillars-of-an-object-oriented-system.aspx#comments</comments><description>&lt;p&gt;The four basic elements of an object-oriented system are &lt;strong&gt;abstraction&lt;/strong&gt;,     &lt;br /&gt;&lt;strong&gt;encapsulation&lt;/strong&gt;, &lt;strong&gt;inheritance&lt;/strong&gt;, and &lt;strong&gt;polymorphism&lt;/strong&gt;. This post defines     &lt;br /&gt;these terms and describes why they are important to software design and     &lt;br /&gt;development.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of OO, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;h2&gt;Abstraction: Focusing on What is Important&lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;Abstraction&lt;/strong&gt; is a technique that we all use to manage the complexity of the     &lt;br /&gt;information we collect every day. It allows us to recognize how things are     &lt;br /&gt;similar and ignore how they are different, to think about generalities and     &lt;br /&gt;not specifics, and to see what things are without thinking about what makes     &lt;br /&gt;them that way. You can abstract important characteristics at any given time     &lt;br /&gt;and for any particular purpose and ignore all other aspects.&lt;/p&gt;  &lt;p&gt;How you develop an abstraction depends on both its purpose and your    &lt;br /&gt;perspective. For example, on a warm summer day I look at a tree and     &lt;br /&gt;abstract it as a shade provider. A young child abstracts it as a place to     &lt;br /&gt;climb. One tree, two different abstractions.&lt;/p&gt;  &lt;p&gt;Abstraction is used to identify the objects involved with a particular    &lt;br /&gt;application. In developing a payroll system, you would think about Jessica     &lt;br /&gt;Jones and abstract her as an employee, thinking only about her salary and     &lt;br /&gt;how she gets paid. When working on the company softball league application, you would abstract Jessica as a player and be more concerned with her position and batting average. One object, two completely different abstractions.&lt;/p&gt;  &lt;p&gt;Scenarios (or use cases) can ensure that the correct abstraction is used in a particular application. They provide context for the objects, giving them a purpose. &lt;/p&gt;  &lt;p&gt;Using abstraction, you can focus on the objects and not on the implementation. This lets you think about what needs to be done and not how the computer will do it.&lt;/p&gt;  &lt;h2&gt;Encapsulation: Hiding Your Private Parts&lt;/h2&gt;  &lt;p&gt;Most organizations have independent units, usually called departments.    &lt;br /&gt;The sales department makes the sales, the production department produces     &lt;br /&gt;the item, the shipping department ships it, and so on. Each department     &lt;br /&gt;is responsible for its own procedures and internal information. For example, the sales department has procedures for calling prospects, evaluating     &lt;br /&gt;the opportunity, sending sales materials, following up with current     &lt;br /&gt;customers, maintaining prospect information, and so on.&lt;/p&gt;  &lt;p&gt;You could say the departments are &lt;strong&gt;encapsulated&lt;/strong&gt; because the internal     &lt;br /&gt;information (properties) and standard operating procedures (methods)     &lt;br /&gt;are contained within the department. These are figuratively hidden from     &lt;br /&gt;other departments except through defined interfaces. Anyone who needs     &lt;br /&gt;the department’s procedures or information goes to that department to     &lt;br /&gt;ask for it.&lt;/p&gt;  &lt;p&gt;If a shipping clerk acquires the name of a prospect, the clerk knows    &lt;br /&gt;better than to handle the prospect directly. Instead, the clerk collaborates     &lt;br /&gt;with the sales department by using the publicly accessible defining prospects     &lt;br /&gt;feature and sends the name to them.&lt;/p&gt;  &lt;p&gt;The same principles are used in object-oriented applications to encapsulate    &lt;br /&gt;each object’s properties and methods. When an object needs to perform     &lt;br /&gt;a procedure that is encapsulated in another class, it does not perform     &lt;br /&gt;the procedure directly. Instead, it collaborates with an object belonging to     &lt;br /&gt;the other class to perform the procedure.&lt;/p&gt;  &lt;p&gt;Encapsulation aids in abstraction by hiding the internal implementation    &lt;br /&gt;of an object within the class. You then can use an object without     &lt;br /&gt;understanding how its class is implemented. So the shipping clerk can     &lt;br /&gt;define a prospect to the sales department without knowing how the sales     &lt;br /&gt;department actually handles the prospect.&lt;/p&gt;  &lt;h2&gt;Inheritance: Attaining Reuse&lt;/h2&gt;  &lt;p&gt;Things that are similar still have some differences, and things that are    &lt;br /&gt;different often have some similarities. Reviewing the inheritance     &lt;br /&gt;example (from my post &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-inheritance.aspx" target="_blank"&gt;here&lt;/a&gt;), both your desk phone and your cell phone can be classified as phones. Looking at their differences, your desk phone can transfer calls, and your cell phone can take pictures.&lt;/p&gt;  &lt;p&gt;Say you are asked to create phone emulator software. You could implement    &lt;br /&gt;all properties and methods for a cell phone into a cell phone class. This class would include volume and picture count properties and dial, answer, disconnect, and take picture methods. Likewise, you could implement all properties and methods for a desk phone into a desk phone class.&lt;/p&gt;  &lt;p&gt;This class would include volume and line status properties and dial,    &lt;br /&gt;answer, disconnect, and transfer call methods. This duplicates the common     &lt;br /&gt;phone information and functionality in both classes.&lt;/p&gt;  &lt;p&gt;You can remove the property and method redundancy and attain reuse    &lt;br /&gt;by using &lt;strong&gt;inheritance&lt;/strong&gt;. You can remove the common phone properties and     &lt;br /&gt;methods from the specialized classes and put them in a higher-level phone     &lt;br /&gt;class. The cell phone class and desk phone class can then inherit from the     &lt;br /&gt;phone class, attaining all its properties and methods. This “is a” relationship     &lt;br /&gt;is depicted &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-inheritance.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;If a new type of phone class, such as a Web phone, were added, objects of the new class would automatically have all the properties and methods defined for the base phone class, greatly simplifying the development of the new class.    &lt;br /&gt;Inheritance is a powerful tool for code reuse.&lt;/p&gt;  &lt;h2&gt;Polymorphism: Same Behavior, Different Implementation&lt;/h2&gt;  &lt;p&gt;Two or more classes can have methods that are named the same and have    &lt;br /&gt;the same basic purpose but different implementations. This is &lt;strong&gt;polymorphism&lt;/strong&gt;.     &lt;br /&gt;The Text property of the .NET Framework is an example of polymorphism.     &lt;br /&gt;Although the basic purpose of the Text property is the same, how the property works depends on whether you are setting the Text property of a Form, a Label, or a TextBox.&lt;/p&gt;  &lt;p&gt;In the phone example, say the implementation of the Dial method    &lt;br /&gt;needed to be different in the cell phone class and the desk phone class.     &lt;br /&gt;The cell phone class would then need its own Dial method, and a desk     &lt;br /&gt;phone class would need its own Dial method. You can request the Dial     &lt;br /&gt;method by an object from either class without knowing how either class     &lt;br /&gt;plans to implement that request. Both the cell phone class and the desk     &lt;br /&gt;phone class have the Dial method, but the implementation of that behavior     &lt;br /&gt;can be completely different.&lt;/p&gt;  &lt;p&gt;Polymorphism can be implemented using inheritance. The base phone    &lt;br /&gt;class would retain a Dial method, but the implementation would be empty     &lt;br /&gt;(or would provide a default implementation). Each derived class would     &lt;br /&gt;then override the base class Dial method by implementing the method.     &lt;br /&gt;The result is that the desk phone class and cell phone class each have a     &lt;br /&gt;Dial method, but the implementations are different.&lt;/p&gt;  &lt;p&gt;Polymorphism can also be implemented using an interface. In the    &lt;br /&gt;phone example, the Dial method would be removed from the base phone     &lt;br /&gt;class. An IDial interface would define the properties and methods for     &lt;br /&gt;handling dialing. Both the desk phone class and cell phone class implement     &lt;br /&gt;this interface so that they both have the same properties and methods     &lt;br /&gt;for dialing but different implementations. (See &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-an-interface.aspx" target="_blank"&gt;this link&lt;/a&gt; for more information on interfaces.)&lt;/p&gt;  &lt;p&gt;The benefit of polymorphism is that you don’t need to know the    &lt;br /&gt;object’s class to execute the polymorphic behavior. For example, you may     &lt;br /&gt;have many classes in an application, each with its own save method. When     &lt;br /&gt;the application is saved, each object knows the class it belongs to and automatically calls the correct save routine.&lt;/p&gt;  &lt;p&gt;(Based on an except from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719927" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>What is an Interface?</title><link>http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-an-interface.aspx</link><pubDate>Tue, 01 Sep 2009 16:29:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719921</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1719921</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-an-interface.aspx#comments</comments><description>&lt;p&gt;When talking about OO, the term “interface” has nothing to do with your   &lt;br /&gt;user interface. An &lt;strong&gt;interface&lt;/strong&gt; defines a list of properties and methods that    &lt;br /&gt;a class can implement. But if the class implements a particular interface, it    &lt;br /&gt;must implement all properties and methods defined by that interface.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of OO, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;For example, you can think of a phone as implementing a dial interface,   &lt;br /&gt;IDial. (By convention, interfaces begin with the letter I.) This interface defines Connect and MakeTone methods but does not implement them. This allows each phone to define how it connects and makes tones. Every phone that implements the IDial interface must provide an implementation    &lt;br /&gt;for both the connect and make tone methods.&lt;/p&gt;  &lt;p&gt;An interface is different from a base class in that an interface defines   &lt;br /&gt;the list of properties and methods with no implementation. The implementation    &lt;br /&gt;is left to the class that implements the interface. A base class contains both the list of properties and methods and their implementation. The class that inherits from the base class does not need to provide any implementation.&lt;/p&gt;  &lt;p&gt;In your application, you can implement interfaces provided in the   &lt;br /&gt;.NET Framework or define and implement your own interfaces. For    &lt;br /&gt;example, if you want to ensure that every MDI child form in your application    &lt;br /&gt;provides a standard set of methods, you could define an IMDIChild interface as follows:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public interface IMDIChild     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; bool ProcessDelete();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; bool ProcessNew();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; bool ProcessSave();      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Interface IMDIChild     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Function ProcessDelete() As Boolean      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Function ProcessNew() As Boolean      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Function ProcessSave() As Boolean      &lt;br /&gt;End Interface&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In this example, the interface is comprised of three methods: ProcessDelete, ProcessNew, and ProcessSave. Each of these methods must be implemented in any class that implements this interface.&lt;/p&gt;  &lt;p&gt;To implement an interface in a class, specify the interface when defining the class. For example, if the TimeSheetWin class implements the IMDIChild   &lt;br /&gt;interface, it would look like this:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class TimeSheetWin : IMDIChild&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class TimeSheetWin     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Implements IMDIChild&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Each class that implements this interface must provide an implementation   &lt;br /&gt;for all three of the interface methods. &lt;/p&gt;  &lt;p&gt;[For an example of implementing the .NET IDataErrorInfo and INotifyPropertyChanged interfaces, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx" target="_blank"&gt;this link&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;Implementing an interface allows a class to be more formal about the   &lt;br /&gt;behavior it promises to provide. Interfaces form a contract between the    &lt;br /&gt;class and the rest of the application, and the compiler enforces this contract.    &lt;br /&gt;If your class claims to implement an interface, all methods defined by that    &lt;br /&gt;interface must be implemented before the class successfully compiles.&lt;/p&gt;  &lt;p&gt;(Based on an except from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719921" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>What is Inheritance?</title><link>http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-inheritance.aspx</link><pubDate>Tue, 01 Sep 2009 15:58:51 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719911</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1719911</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-inheritance.aspx#comments</comments><description>&lt;p&gt;In object-oriented (OO) terms, inheritance defines an “is a” relationship between two or more classes. A beagle &lt;em&gt;is a&lt;/em&gt; dog, and a poodle &lt;em&gt;is a&lt;/em&gt; dog, so both beagle and poodle inherit from dog. Both beagle and poodle have dog attributes and exhibit dog behaviors. A dog class in this example is called the &lt;strong&gt;parent class&lt;/strong&gt; or &lt;strong&gt;base class&lt;/strong&gt;, and the classes that inherit from it (beagle and poodle) are called &lt;strong&gt;child classes&lt;/strong&gt; or &lt;strong&gt;derived classes&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of OO, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx" target="_blank"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;Likewise, think about your phone. All types of phones have basic    &lt;br /&gt;phone attributes and behaviors, such as volume, dial, answer, and disconnect.     &lt;br /&gt;Your desk phone may have additional, specialized behaviors, such     &lt;br /&gt;as transfer features. Your cell phone has amazing features such as taking     &lt;br /&gt;pictures and playing movies. &lt;/p&gt;  &lt;p&gt;Even though each type of phone may have specialized behaviors, your    &lt;br /&gt;desk phone&lt;em&gt; is a&lt;/em&gt; phone, and your cell phone &lt;em&gt;is a&lt;/em&gt; phone, so both desk phone     &lt;br /&gt;and cell phone inherit their basic functionality from phone. Phone is the     &lt;br /&gt;parent (or base) class, and desk phone and cell phone are the child (or     &lt;br /&gt;derived) classes. You could draw this relationship as shown below.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/2063.image_5F00_491EF075.png"&gt;&lt;img style="border-right-width:0px;margin:0px 0px 0px 20px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/5710.image_5F00_thumb_5F00_13A8BF01.png" width="363" height="401" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;When you inherit from a class, all the properties and methods in the     &lt;br /&gt;base class are available to the derived class, just as if the properties and     &lt;br /&gt;methods were in the derived class. So the desk phone can perform the     &lt;br /&gt;answer method, as can the cell phone, even though the implementation     &lt;br /&gt;for answer is defined in the base phone class.&lt;/p&gt;  &lt;p&gt;Any class in the .NET Framework that is not intentionally sealed    &lt;br /&gt;(marked as not inheritable) can be a used as a base class. So you can inherit     &lt;br /&gt;the functionality of the .NET Framework classes in your application.     &lt;br /&gt;You can also use any class in your application as a base class and inherit     &lt;br /&gt;from it.&lt;/p&gt;  &lt;p&gt;You can see inheritance in action as soon as you create your first form.    &lt;br /&gt;When you create a form, Visual Studio generates the following code:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#&lt;/strong&gt; (in the TimeSheetWin.cs file):&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public partial class TimeSheetWin: Form&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB&lt;/strong&gt; (in the TimeSheetWin.Designer.vb file):&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Partial Class TimeSheetWin      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Inherits Windows.Forms.Form&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB, the &lt;strong&gt;Inherits&lt;/strong&gt; keyword specifies that your form inherits from the .NET     &lt;br /&gt;Framework’s Windows.Forms.Form class. In C#, a colon separates the class name from the base class, which again is the Form class. This Form class provides all the common code required to make your form work like a Windows form.&lt;/p&gt;  &lt;p&gt;NOTE: In VB, the designer file is, by default, hidden from view in Solution Explorer. To see the designer files for a VB project, select the project in Solution Explorer and then click on the Show All Files button in the Solution Explorer toolbar. Repeat the process to hide the files.    &lt;br /&gt;    &lt;br /&gt;As you define the classes for your application, you may find that some     &lt;br /&gt;classes have a number of the same properties and methods but also have     &lt;br /&gt;some properties and methods that are different. You can extract the common     &lt;br /&gt;properties and methods into another class and then use that class as     &lt;br /&gt;a base class.&lt;/p&gt;  &lt;p&gt;For example, you may find that each of your business object classes    &lt;br /&gt;(such as Employee and TimeSheet) requires a property to keep track of     &lt;br /&gt;the dirty state (added, updated, deleted). You can build a business object     &lt;br /&gt;base class that contains this property instead of adding it to every class.     &lt;br /&gt;Every business object class can then inherit from this base class and therefore have this property.&lt;/p&gt;  &lt;p&gt;[For a code example of a business object base class, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx" target="_blank"&gt;this link&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;Using inheritance can minimize the amount of repeated code in your    &lt;br /&gt;application. Common code is written only once in the base class and is     &lt;br /&gt;reused by every derived class. Inheritance also makes your derived classes     &lt;br /&gt;easier to build and maintain, because the derived classes focus exclusively     &lt;br /&gt;on the features that make them unique. And if you ever need to change     &lt;br /&gt;that common code, you have to change it in only one place.&lt;/p&gt;  &lt;p&gt;(Based on an except from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719911" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>What is a Class?</title><link>http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-a-class.aspx</link><pubDate>Mon, 31 Aug 2009 22:48:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719745</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1719745</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-a-class.aspx#comments</comments><description>&lt;p&gt;Humans like to classify things, to find similarities in things, and to group    &lt;br /&gt;them accordingly. Things with similar attributes (&lt;strong&gt;properties&lt;/strong&gt;) and behaviors     &lt;br /&gt;(&lt;strong&gt;methods&lt;/strong&gt;) are grouped. In object-oriented terminology, the definition of     &lt;br /&gt;the properties and methods that describe a particular classification is called     &lt;br /&gt;a &lt;strong&gt;class&lt;/strong&gt;. A class defines a particular &lt;strong&gt;object type&lt;/strong&gt;. You can think of a class as a     &lt;br /&gt;blueprint, providing the details of how objects of that type are made.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of OO, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx" target="_blank"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;A cookie cutter is another analogy often used to illustrate the relationship    &lt;br /&gt;between objects and a class. The cookie cutter is the class and     &lt;br /&gt;provides the definition; it specifies the size and shape. The cookies are the     &lt;br /&gt;objects created from the cookie cutter class.&lt;/p&gt;  &lt;p&gt;For example, an employee class can have name, address, and occupation    &lt;br /&gt;properties. Sam Smith and Jessica Jones are both objects from the     &lt;br /&gt;employee class. Likewise, a time sheet class can have employee, date, and hours properties and a calculate pay method. Each person’s weekly time     &lt;br /&gt;sheet is then an object from the time sheet class.&lt;/p&gt;  &lt;p&gt;A specific object created from a class is called an &lt;strong&gt;instance&lt;/strong&gt; of the     &lt;br /&gt;class. Each instance can have values for the defined set of properties and     &lt;br /&gt;can perform the defined methods. So Sam is an instance of the employee     &lt;br /&gt;class, and Jessica’s time sheet for last week is an instance of the time sheet     &lt;br /&gt;class.&lt;/p&gt;  &lt;p&gt;The set of properties and methods described by a class are often called    &lt;br /&gt;class &lt;strong&gt;members&lt;/strong&gt;. A class defines the members, including the actual code to     &lt;br /&gt;maintain the property values and perform the methods. Each object that     &lt;br /&gt;is an instance of that class can execute the methods and retain values for     &lt;br /&gt;each property.&lt;/p&gt;  &lt;p&gt;A class itself normally does not maintain state, meaning that it normally    &lt;br /&gt;does not have any property values. Nor does it normally execute the class     &lt;br /&gt;methods. Instead, the class defines the properties and contains the implementation of the methods that are used by each &lt;em&gt;object&lt;/em&gt; created from the     &lt;br /&gt;class. Each object has values for the properties and performs the methods.     &lt;br /&gt;You do not eat the cookie cutter or fill out a time sheet class. You eat each     &lt;br /&gt;cookie and fill out each individual time sheet.&lt;/p&gt;  &lt;p&gt;The .NET Framework itself is composed of a set of classes. For    &lt;br /&gt;example, the button in the Visual Studio Toolbox represents a Button     &lt;br /&gt;class. Each time you add a button to a form, you create an instance of     &lt;br /&gt;that Button class. The Button class has specific defined methods, such     &lt;br /&gt;as Focus, and properties, such as Name and Text. The Button class     &lt;br /&gt;itself does not have a value for the Name or Text properties. Nor can it     &lt;br /&gt;have focus. Instead, it contains the implementation of the Name and Text     &lt;br /&gt;properties and the Focus method. The Button objects that you create as     &lt;br /&gt;instances of that Button class have values for the Name and Text properties     &lt;br /&gt;and perform the Focus method.&lt;/p&gt;  &lt;p&gt;Code you write normally resides in a class. If you type some    &lt;br /&gt;code in a form, your code is in the form’s class. Even if you write code in a     &lt;br /&gt;Visual Basic module, the code compiles as a class!&lt;/p&gt;  &lt;p&gt;If you have never created a class, the following example may help you    &lt;br /&gt;visualize the code for a TimeSheet class.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class TimeSheet      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Employee CurrentEmployee { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public decimal TotalHours { get; set; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; private DateTime _WeekEndingDate;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public DateTime WeekEndingDate       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { return _WeekEndingDate; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (value &amp;gt; DateTime.Now.Date)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new Exception(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Date must be on or before today&amp;#39;s date&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _WeekEndingDate = value;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; public decimal CalculatePay()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return CurrentEmployee.HourlyRate * TotalHours;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:      &lt;br /&gt;&lt;/strong&gt;    &lt;br /&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class TimeSheet      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private _CurrentEmployee As Employee       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property CurrentEmployee() As Employee       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _CurrentEmployee       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Employee)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _CurrentEmployee = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _TotalHours As Decimal      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property TotalHours() As Decimal       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _TotalHours       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Decimal)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _TotalHours = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _WeekEndingDate As DateTime      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property WeekEndingDate() As DateTime       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _WeekEndingDate       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As DateTime)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If value &amp;gt; Now.Date Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Throw New Exception( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Date must be on or before today&amp;#39;s date&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _WeekEndingDate = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Public Function CalculatePay() As Decimal      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return CurrentEmployee.HourlyRate * TotalHours       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Function       &lt;br /&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;The first line defines the class’s scope and name. In this case, the class     &lt;br /&gt;is named TimeSheet and is public.&lt;/p&gt;  &lt;p&gt;The next set of code defines the private data, called &lt;strong&gt;backing fields&lt;/strong&gt;, and public accessors that provide access to that data. This follows the concept of encapsulation. For example, the private _WeekEndingDate backing field variable contains the value of the time period property. Since this variable is private, it is hidden and cannot be accessed except from within this class. The public WeekEndingDate Property statement implements the get time period and set time period functionality. Any code that needs to set or get the time period uses this public property.&lt;/p&gt;  &lt;p&gt;NOTE: The C# code uses &lt;strong&gt;auto-implemented properties&lt;/strong&gt; for the TotalHours and CurrentEmployee. Auto-implemented properties are a short-cut for defining a backing field with its associated accessor. The compiler defines the private backing field for you. You can use auto-implemented properties any time you don&amp;#39;t need extra code, such as validation, in your property statement. (VB is getting auto-implemented properties in VB 10 that is coming with VS 2010.)&lt;/p&gt;  &lt;p&gt;By implementing properties using private backing fields and public    &lt;br /&gt;Property statements, you can associate code with the setting or retrieval     &lt;br /&gt;of the data value. In the WeekEndingDate example, the code that sets the     &lt;br /&gt;date validates the date before assigning it.&lt;/p&gt;  &lt;p&gt;The code at the end of the class in the preceding example demonstrates    &lt;br /&gt;a method—in this case, a function that calculates the pay. In Visual     &lt;br /&gt;Basic, methods are implemented with functions or subroutines. In C# the syntax is the same for both functions and subroutines.&lt;/p&gt;  &lt;p&gt;To access the properties and use the methods of a class, you normally    &lt;br /&gt;begin by creating an instance of the class and storing a reference to that     &lt;br /&gt;instance in an object variable. You then use that object variable to get or     &lt;br /&gt;set properties and execute methods on the resulting object.     &lt;br /&gt;The following code uses the &lt;strong&gt;new&lt;/strong&gt; keyword to create a new object from     &lt;br /&gt;the TimeSheet class. It then sets the properties on the object and executes     &lt;br /&gt;the CalculatePay method on the object.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;TimeSheet ts = new TimeSheet();      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;decimal totalPay;      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;ts.CurrentEmployee = Jessica; //Jessica is an Employee object      &lt;br /&gt;ts.WeekEndingDate = new DateTime(2009, 8, 20);       &lt;br /&gt;ts.TotalHours = 51;       &lt;br /&gt;totalPay = ts.CalculatePay();&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim ts As New TimeSheet      &lt;br /&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim totalPay As Decimal        &lt;br /&gt;&lt;/font&gt;ts.CurrentEmployee = Jessica &amp;#39;Jessica is an Employee object       &lt;br /&gt;ts.WeekEndingDate = #8/20/2009#       &lt;br /&gt;ts.TotalHours = 51       &lt;br /&gt;totalPay = ts.CalculatePay&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;new&lt;/strong&gt; keyword creates a new instance of the TimeSheet class, and     &lt;br /&gt;a reference to that instance is stored in the defined object variable (ts).     &lt;br /&gt;Using the object variable and a period, the remainder of the code sets the     &lt;br /&gt;object’s properties and calls the method to calculate the pay.&lt;/p&gt;  &lt;p&gt;As stated earlier in this section, objects normally maintain state (property    &lt;br /&gt;values) and execute methods. The class itself normally does not maintain     &lt;br /&gt;state or execute methods. However, .NET does support static     &lt;br /&gt;class data and static class methods.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Static class data&lt;/strong&gt; (also called &lt;strong&gt;shared data&lt;/strong&gt;) is data that the class     &lt;br /&gt;retains, independent of any object. Use static class data any time you want     &lt;br /&gt;to retain data that is shared between all objects, such as to keep a count of     &lt;br /&gt;the total objects created from a class.&lt;/p&gt;  &lt;p&gt;A &lt;strong&gt;static class method&lt;/strong&gt; (also called a &lt;strong&gt;shared method&lt;/strong&gt;) is a method     &lt;br /&gt;that can be executed independent of any object. Static class methods are     &lt;br /&gt;good for utility functions when you don’t need to perform a process on     &lt;br /&gt;any particular object. The MessageBox and Debug classes in the .NET     &lt;br /&gt;Framework both have static class methods, so you don’t need to create an     &lt;br /&gt;instance of the class to use the methods.&lt;/p&gt;  &lt;p&gt;NOTE: If you write code in a Visual Basic module instead of a class, all the properties and methods in the module are static. You cannot create an instance from a module.&lt;/p&gt;  &lt;p&gt;Building code as individual classes logically separates the code, with    &lt;br /&gt;each class defining its own data and implementing its own methods.     &lt;br /&gt;Having code organized into logical classes makes finding code for maintenance and enhancements effortless and produces a system that is much easier to extend. It also simplifies testing and debugging, because you can test each class as an independent unit. By using OO, you can manage the complexity of your application.&lt;/p&gt;  &lt;p&gt;(Based on an except from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719745" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>What Are Objects?</title><link>http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-are-objects.aspx</link><pubDate>Mon, 31 Aug 2009 20:42:14 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719736</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1719736</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-are-objects.aspx#comments</comments><description>&lt;p&gt;Objects are things. People, companies, employees, time sheets, and ledger    &lt;br /&gt;entries are all types of objects. In object-oriented terms, the word &lt;strong&gt;object&lt;/strong&gt;     &lt;br /&gt;is used to describe one specific thing, such as Sam Smith the carpenter at     &lt;br /&gt;3322 Main Street and the May 15th time sheet for Jessica Jones.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of OO, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;Objects have data associated with them called &lt;strong&gt;properties&lt;/strong&gt;. Sam Smith     &lt;br /&gt;has a name, occupation, and address. The time sheet has an employee     &lt;br /&gt;name, time period, and hours worked.     &lt;br /&gt;    &lt;br /&gt;NOTE: In object-oriented literature, properties are sometimes called &lt;strong&gt;attributes&lt;/strong&gt;, &lt;strong&gt;resources&lt;/strong&gt;, or even just &lt;strong&gt;data&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Objects also &lt;em&gt;do&lt;/em&gt; things. The time sheet is filled out, validated, and     &lt;br /&gt;submitted for payment. In real life, we fill out time sheets, validate them,     &lt;br /&gt;and submit them for payment. In a computer system, the time sheet can     &lt;br /&gt;perform these operations for itself. The things an object can do are defined     &lt;br /&gt;with &lt;strong&gt;methods&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;NOTE: In object-oriented literature, methods are also called &lt;strong&gt;behaviors&lt;/strong&gt;,     &lt;br /&gt;&lt;strong&gt;services&lt;/strong&gt;, &lt;strong&gt;operations&lt;/strong&gt;, or &lt;strong&gt;responsibilities&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Objects can be real-world things, such as an employee or time sheet.    &lt;br /&gt;Objects can be conceptual things, such as an engineering process or payroll.     &lt;br /&gt;Objects can also be implementation-specific things, such as forms,     &lt;br /&gt;controls, and DataSets. The same object-oriented concepts apply regardless     &lt;br /&gt;of whether the object is based on the real world, on a concept, or on     &lt;br /&gt;the implementation.&lt;/p&gt;  &lt;p&gt;Since objects are fundamental to understanding object orientation,    &lt;br /&gt;it is important that you can recognize objects and define their appropriate     &lt;br /&gt;properties and methods. So take a moment to think about the things     &lt;br /&gt;around you. What objects do you see? How would you define their properties     &lt;br /&gt;and methods?&lt;/p&gt;  &lt;p&gt;Your phone has properties such as color, volume, and mute. It has    &lt;br /&gt;methods such as increase volume, decrease volume, turn on mute, turn off     &lt;br /&gt;mute, dial, answer, and transfer. An employee has properties such as name,     &lt;br /&gt;address, and occupation. An electronic time sheet has properties such as     &lt;br /&gt;employee name, date, and hours. It has methods such as calculate pay.&lt;/p&gt;  &lt;p&gt;An object’s property values should not be directly accessed by something outside the object. For example, the time period should not be adjusted except through a get time period method, and the employee name would not be retrieved except through a get employee name method. This concept of hiding the internal data is a part of what is called &lt;strong&gt;encapsulation&lt;/strong&gt; and is a key premise of object-oriented programming. &lt;/p&gt;  &lt;p&gt;Every object is of a specific type. For example, the object defined as    &lt;br /&gt;Sam Smith the carpenter at 3322 Main Street is an employee object type,     &lt;br /&gt;and the May 15th time sheet for Jessica Jones is a time sheet object type.     &lt;br /&gt;All the objects of a particular type have the same set of properties and     &lt;br /&gt;methods.&lt;/p&gt;  &lt;p&gt;From a programming perspective, you can define your own object    &lt;br /&gt;types using classes. Or you can use the object types provided in the .NET Framework. In either case, you first create an object of the desired type, and then you can set or get the object’s properties and call its methods.&lt;/p&gt;  &lt;p&gt;The following code creates an object using the .NET Framework    &lt;br /&gt;&lt;strong&gt;Timer&lt;/strong&gt; object type. It then sets the object’s &lt;strong&gt;Interval&lt;/strong&gt; property to define     &lt;br /&gt;how often the timer goes off and executes its &lt;strong&gt;Start&lt;/strong&gt; method to start the     &lt;br /&gt;timer.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Timer myTimer = new Timer();      &lt;br /&gt;myTimer.Interval = 1000;       &lt;br /&gt;myTimer.Start();&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim myTimer As New Timer      &lt;br /&gt;myTimer.Interval = 1000       &lt;br /&gt;myTimer.Start()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;new&lt;/strong&gt; keyword in the first line of code creates a new Timer object     &lt;br /&gt;and assigns a reference to that new object to the myTimer object variable.     &lt;br /&gt;To access a property or method of the new Timer object, use the object     &lt;br /&gt;variable and a period (.) and then the name of the property or method.&lt;/p&gt;  &lt;p&gt;NOTE: When you type the object variable name and a period in Visual Studio,    &lt;br /&gt;you see a list of the properties, methods, and events that are appropriate for that object. This demonstrates the List Members feature of Intellisense. &lt;strong&gt;Intellisense&lt;/strong&gt; provides auto-completion and display of class documentation within Visual Studio as you are coding. &lt;/p&gt;  &lt;p&gt;Everything in .NET can be accessed as an object: forms, strings,    &lt;br /&gt;even integers. Check out this code:&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;int i = new int();      &lt;br /&gt;string s;       &lt;br /&gt;i = 5;       &lt;br /&gt;s = i.ToString();&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim i As New Integer      &lt;br /&gt;Dim s As String       &lt;br /&gt;i = 5       &lt;br /&gt;s = i.ToString&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The first line creates a new Integer object and assigns it to i. The variable    &lt;br /&gt;i is then assigned a value of 5. The last line calls the ToString method of     &lt;br /&gt;the Integer object to convert the number to a string.&lt;/p&gt;  &lt;p&gt;NOTE: Even though you can use the &lt;strong&gt;new&lt;/strong&gt; keyword to create integers, it is not     &lt;br /&gt;necessary and is not commonly done in practice. It is shown in this example to illustrate that you can work with simple data types as objects.&lt;/p&gt;  &lt;p&gt;In .NET, an integer and other primitive data types, such as Boolean    &lt;br /&gt;and decimal, are called &lt;strong&gt;value types&lt;/strong&gt;. Value types store and pass their contents     &lt;br /&gt;by their actual value. Technically speaking, value types are allocated     &lt;br /&gt;either on the stack or inline in a structure. A standard set of value types     &lt;br /&gt;are built into the .NET Framework, such as integer and decimal. You can     &lt;br /&gt;also create your own value types. You do not need to use the new keyword     &lt;br /&gt;for any value type.&lt;/p&gt;  &lt;p&gt;More complex data types in .NET, such as strings and arrays, are    &lt;br /&gt;&lt;strong&gt;reference types&lt;/strong&gt;. Reference types store and pass their contents as a reference to the value’s memory location. Technically speaking, reference types     &lt;br /&gt;are allocated on the memory heap. The .NET Framework provides a set of     &lt;br /&gt;built-in reference types, such as string and array. Classes that you create are reference types.&lt;/p&gt;  &lt;p&gt;.NET automatically creates a boxed value type for each value type when necessary. Boxed value types allow value types to be accessed like reference types (such as the preceding integer example). In addition, boxed value types    &lt;br /&gt;allow you to convert value types to reference types. For example, adding     &lt;br /&gt;to the preceding code example, you could assign the integer to an object.     &lt;br /&gt;This converts the integer value type to an object reference type.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;object o;      &lt;br /&gt;o = i;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim o As Object      &lt;br /&gt;o = i&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This conversion of value types to reference types and vice versa is    &lt;br /&gt;called &lt;strong&gt;boxing&lt;/strong&gt;. Boxing has a performance hit, so you want to minimize the     &lt;br /&gt;amount of boxing in your application wherever possible.&lt;/p&gt;  &lt;p&gt;Variables that are value types each have their own copy of the data.    &lt;br /&gt;Therefore, operations on one variable do not affect other variables.     &lt;br /&gt;Variables that are reference types, such as object variables, can refer to the     &lt;br /&gt;same object. Therefore, operations on one variable can affect the same     &lt;br /&gt;object referenced by another variable. Check this out:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Timer myTimer = new Timer();      &lt;br /&gt;myTimer.Interval = 1000; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Timer myTimer2;      &lt;br /&gt;myTimer2 = myTimer;       &lt;br /&gt;myTimer2.Interval = 500; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(myTimer.Interval); // Displays 500      &lt;br /&gt;Debug.WriteLine(myTimer2.Interval); // Displays 500&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim myTimer As New Timer      &lt;br /&gt;myTimer.Interval = 1000       &lt;br /&gt;      &lt;br /&gt;Dim myTimer2 As Timer       &lt;br /&gt;myTimer2 = myTimer       &lt;br /&gt;myTimer2.Interval = 500       &lt;br /&gt;      &lt;br /&gt;Debug.WriteLine(myTimer.Interval) &amp;#39; Displays 500       &lt;br /&gt;Debug.WriteLine(myTimer2.Interval) &amp;#39; Displays 500&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;This code creates a new Timer object, assigns it to the myTimer object     &lt;br /&gt;variable, and sets its Interval property to 1000. It then defines a second     &lt;br /&gt;myTimer2 object variable, assigns it to the same Timer object, and sets the     &lt;br /&gt;Interval property to 500.&lt;/p&gt;  &lt;p&gt;Since myTimer and myTimer2 are object variables, they are reference    &lt;br /&gt;types. Assigning one object variable to another object variable sets     &lt;br /&gt;both variables to reference the same object. Changing the properties     &lt;br /&gt;using either object variable makes the changes to the underlying object.     &lt;br /&gt;Hence, the sample code results in 500 for both myTimer.Interval and     &lt;br /&gt;myTimer2.Interval.&lt;/p&gt;  &lt;p&gt;The one exception to this behavior is strings. Even though strings are    &lt;br /&gt;a reference type, assigning one string to another string copies the data so     &lt;br /&gt;that both strings do not reference the same data. This provides a more     &lt;br /&gt;natural use of strings in your application. For example:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;string employeeName ;      &lt;br /&gt;employeeName = &amp;quot;Jessica Jones&amp;quot;; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;string employeeName2;      &lt;br /&gt;employeeName2 = employeeName;       &lt;br /&gt;employeeName2 = &amp;quot;Sam Smith&amp;quot;; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(employeeName); // Displays &amp;quot;Jessica Jones&amp;quot;      &lt;br /&gt;Debug.WriteLine(employeeName2); // Displays &amp;quot;Sam Smith&amp;quot;&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim employeeName As String      &lt;br /&gt;employeeName = &amp;quot;Jessica Jones&amp;quot;       &lt;br /&gt;      &lt;br /&gt;Dim employeeName2 As String       &lt;br /&gt;employeeName2 = employeeName       &lt;br /&gt;employeeName2 = &amp;quot;Sam Smith&amp;quot;       &lt;br /&gt;      &lt;br /&gt;Debug.WriteLine(employeeName) &amp;#39; Displays &amp;quot;Jessica Jones&amp;quot;       &lt;br /&gt;Debug.WriteLine(employeeName2) &amp;#39; Displays &amp;quot;Sam Smith&amp;quot;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Even though employeeName2 is assigned to employeeName and then    &lt;br /&gt;employeeName2 is changed, employeeName remains unchanged. The     &lt;br /&gt;sample code results in the display of Jessica Jones and then Sam     &lt;br /&gt;Smith.&lt;/p&gt;  &lt;p&gt;Notice also that the New keyword is not required for strings. Strings    &lt;br /&gt;were designed as a special case to make it easier and more natural to work with string variables.     &lt;br /&gt;    &lt;br /&gt;Understanding how to create an object and call its properties and     &lt;br /&gt;methods is crucial to any development with .NET.&lt;/p&gt;  &lt;p&gt;(Based on an except from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719736" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>What is OO?</title><link>http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx</link><pubDate>Mon, 31 Aug 2009 19:27:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719726</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1719726</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx#comments</comments><description>&lt;p&gt;Today’s world of software design and development is all about managing    &lt;br /&gt;complexity. Computer-savvy users want more and more features. Software     &lt;br /&gt;products, such as Microsoft Word and Excel, set high expectations. The     &lt;br /&gt;business environment requires software to react quickly to shifting corporate     &lt;br /&gt;needs. And tools and technologies are changing faster than ever. It is     &lt;br /&gt;easy to become overwhelmed by the complexity.&lt;/p&gt;  &lt;p&gt;The key to successful software is managing this complexity—and managing    &lt;br /&gt;complexity is one of the goals of object orientation (OO).&amp;#160; &lt;strong&gt;Object-oriented&lt;/strong&gt;     &lt;br /&gt;means looking at a software system in terms of the things, or     &lt;br /&gt;objects, that are relevant to that system and how those objects interact. As     &lt;br /&gt;you design and then build your application, you can focus on one object at     &lt;br /&gt;a time, temporarily ignoring the complexities of the rest of the system.&lt;/p&gt;  &lt;p&gt;OO concepts are used in many professions. For example, when    &lt;br /&gt;designing an office, an architect thinks about working spaces, foundations,     &lt;br /&gt;frameworks, and plumbing systems. These are the real-world objects. The     &lt;br /&gt;architect does not concentrate on the process of pouring the foundation,     &lt;br /&gt;hammering nails, or connecting the plumbing, nor on the details of the     &lt;br /&gt;data, such as how much concrete or how many nails. These lower-level     &lt;br /&gt;processes and details are important but not applicable to the high-level     &lt;br /&gt;design of an office building. And without the high-level design, the processes     &lt;br /&gt;and data details are irrelevant.&lt;/p&gt;  &lt;p&gt;Object orientation does not ignore the data or the process. It combines    &lt;br /&gt;the best of a procedure-oriented view (where the focus is on the     &lt;br /&gt;process) and a data-centric view (where the focus is on the data) and adds     &lt;br /&gt;productivity concepts such as reuse, testability, and, of course, managing     &lt;br /&gt;complexity.&lt;/p&gt;  &lt;p&gt;Consider a time sheet. Using a data-centric view, the key data elements    &lt;br /&gt;are the employee name, date, and hours worked. But just looking at the     &lt;br /&gt;data does not provide the full picture of time sheet processing. Using a     &lt;br /&gt;procedure-oriented view, the focus is on the process of generating the time     &lt;br /&gt;sheet. But this does not consider the bigger picture of how the time sheet     &lt;br /&gt;fits into an overall system.&lt;/p&gt;  &lt;p&gt;From an object-oriented perspective, the time sheet has data (called    &lt;br /&gt;&lt;strong&gt;properties&lt;/strong&gt;) and processes (called &lt;strong&gt;methods&lt;/strong&gt;). It also has relationships to     &lt;br /&gt;other objects in the system, such as an employee object, a logging object,     &lt;br /&gt;a data access object, and so on.&lt;/p&gt;  &lt;p&gt;Thinking about an application in an object-oriented way makes it    &lt;br /&gt;easier to break the application into its parts (objects), focus on the most     &lt;br /&gt;important aspects of each part, and look at the relationships between those     &lt;br /&gt;parts. And since Visual Basic is now a fully object-oriented programming     &lt;br /&gt;language, using an object-oriented approach to thinking about your application makes it easier to map these thoughts into object-oriented code.&lt;/p&gt;  &lt;p&gt;Excerpt from &amp;quot;Doing Objects in Visual Basic 2005&amp;quot;.&lt;/p&gt;  &lt;p&gt;For more information on using object-oriented techniques, see the following links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-are-objects.aspx" target="_blank"&gt;What are Objects?&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-a-class.aspx" target="_blank"&gt;What is a Class?&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-inheritance.aspx" target="_blank"&gt;What is Inheritance?&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/01/what-is-an-interface.aspx" target="_blank"&gt;What is an Interface?&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/09/01/basic-pillars-of-an-object-oriented-system.aspx" target="_blank"&gt;Basic Pillars of an Object-Oriented System&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-games-mastermind.aspx" target="_blank"&gt;Applying OOP to Simple Games: MasterMind&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx" target="_blank"&gt;Building a Business Object Base Class&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719726" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Building an Alarm Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/08/27/building-an-alarm-class.aspx</link><pubDate>Thu, 27 Aug 2009 17:21:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1718734</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1718734</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/08/27/building-an-alarm-class.aspx#comments</comments><description>&lt;p&gt;The specifics of this class demonstrate how to build an alarm. You can use this class to build an alarm clock application, or to add an alarm feature into your application, similar to the Reminder feature in Outlook.&lt;/p&gt;  &lt;p&gt;OR you can just use this class as an example of raising events from a business object or displaying sounds asynchronously.&lt;/p&gt;  &lt;p&gt;Build the Alarm class as follows.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Declare a delegate     &lt;br /&gt;public delegate void AlarmWentOffHandler(object sender, EventArgs e);      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;     &lt;br /&gt;public class Alarm      &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Declare an event      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public event AlarmWentOffHandler AlarmWentOff; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; public DateTime? AlarmTime { get; set; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private Timer AlarmTimer { get; set; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int SnoozeInterval { get; set; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; public Alarm() : this(null, 5)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; }      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Alarm(DateTime? timeForAlarm): this(timeForAlarm, 5)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; }      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Alarm(DateTime? timeForAlarm, int minutesToSnooze)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Set the properties      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTime = timeForAlarm;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SnoozeInterval = minutesToSnooze; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Start the timer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer = new Timer() {Interval = 10000,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Enabled = true};      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer.Tick += CheckAlarm;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void Snooze()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Reset the alarm by the snooze interval      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTime = DateTime.Now.AddMinutes(SnoozeInterval); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Reset the timer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer.Enabled = true;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; protected virtual void OnAlarmWentOff(EventArgs e)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (AlarmWentOff != null)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmWentOff(this, e);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private void CheckAlarm(object sender,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventArgs e)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Check whether alarm time has been reached      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (AlarmTime &amp;lt;= DateTime.Now)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Disable the timer      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer.Enabled = false; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Raise the event     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OnAlarmWentOff(new EventArgs());      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class Alarm     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Event AlarmWentOff(ByVal sender As Object, ByVal e As EventArgs) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _AlarmTime As DateTime?     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property AlarmTime() As DateTime?      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _AlarmTime      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As DateTime?)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _AlarmTime = value      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _AlarmTimer As Timer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Property AlarmTimer() As Timer      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _AlarmTimer      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Timer)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _AlarmTimer = value      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _SnoozeInterval As Integer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property SnoozeInterval() As Integer      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _SnoozeInterval      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Integer)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _SnoozeInterval = value      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Public Sub New()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Set Defaults      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Me.New(Nothing, 5)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Public Sub New(ByVal timeForAlarm As DateTime?)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Set Defaults      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Me.New(timeForAlarm, 5)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Public Sub New(ByVal timeForAlarm As DateTime?, _     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal minutesToSnooze As Integer)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Set the properties      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTime = timeForAlarm      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SnoozeInterval = minutesToSnooze &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Start the timer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer = New Timer With {.Interval = 10000, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Enabled = True}      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddHandler AlarmTimer.Tick, AddressOf CheckAlarm      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Public Sub Snooze()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Reset the alarm by the snooze interval      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTime = Now.AddMinutes(SnoozeInterval) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Reset the timer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer.Enabled = True      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private Sub CheckAlarm(ByVal sender As Object, _     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal e As EventArgs)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Check whether alarm time has been reached      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If AlarmTime &amp;lt;= Now Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Disable the timer      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AlarmTimer.Enabled = False &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Raise the event     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RaiseEvent AlarmWentOff(Me, New EventArgs())      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; End Sub     &lt;br /&gt;End Class&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The C# code begins by declaring a delegate for its event. This is not required in the VB code since the delegate is handled for you.&lt;/p&gt;  &lt;p&gt;Within the class, the C# and VB code declare an event called &lt;strong&gt;AlarmWentOff&lt;/strong&gt;. This is the event that is generated when the alarm goes off.&lt;/p&gt;  &lt;p&gt;The class has three properties:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;AlarmTime&lt;/strong&gt;: Time that the alarm should go off.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;AlarmTimer&lt;/strong&gt;: A private property that manages the timer for checking the alarm time.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;SnoozeInterval&lt;/strong&gt;: The alarm can optionally be set to snooze. This is the amount of snooze time in minutes.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The C# code uses auto-implemented properties and the VB code uses the expanded property syntax. (VB is getting auto-implemented properties in VB 10.)&lt;/p&gt;  &lt;p&gt;The constructors for the class optionally define the alarm time and minutes to snooze. The constructor then starts the timer that goes off every 10 seconds. (This can be adjusted as desired. Or the timer interval could be defined as a property of this Alarm class.)&lt;/p&gt;  &lt;p&gt;The class has one public method: &lt;strong&gt;Snooze&lt;/strong&gt;. The Snooze method adds the snooze interval amount to the AlarmTime to reset the alarm. It also ensures that the timer in enabled.&lt;/p&gt;  &lt;p&gt;Each time the timer goes off, the code checks the current time against the alarm time. If the alarm time has been reached, the code disables the timer and raises the &lt;strong&gt;AlarmWentOff&lt;/strong&gt; event.&lt;/p&gt;  &lt;p&gt;This class can be used whenever you need to add an alarm, notification, or reminder to your application. Here is an example of how this class is accessed.&lt;/p&gt;  &lt;p&gt;NOTE: It uses a .wav file for the alarm and plays it using the built-in SoundPlayer.&lt;/p&gt;  &lt;p&gt;Start by adding this to the top of your code file.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;using System.Media;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Imports System.Media&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Then add the following code somewhere in your user interface.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Alarm myAlarm;     &lt;br /&gt;private void SetTheAlarm()      &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Create an alarm object      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myAlarm = new Alarm(DateTime.Now.AddMinutes(1), 1);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myAlarm.AlarmWentOff +=&amp;#160; AlarmWentOff;      &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;private void AlarmWentOff(object sender, EventArgs e)     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var player = new SoundPlayer(&amp;quot;myFile.wav&amp;quot;);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; player.PlayLooping();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; DialogResult replay =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBox.Show(&amp;quot;Wake up&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Environment.NewLine +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Yes to wake up.&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Environment.NewLine +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;No to snooze.&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Alarm Clock&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBoxButtons.YesNo,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBoxIcon.Exclamation);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; player.Stop();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (replay != DialogResult.Yes)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myAlarm.Snooze();      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim myAlarm As Alarm     &lt;br /&gt;Private Sub SetTheAlarm()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Create an alarm object      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myAlarm = New Alarm(Now.AddMinutes(1), 1)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; AddHandler myAlarm.AlarmWentOff, AddressOf AlarmWentOff      &lt;br /&gt;End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private Sub AlarmWentOff(ByVal sender As Object, ByVal e As EventArgs)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim player As New SoundPlayer(&amp;quot;myFile.wav&amp;quot;)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; player.PlayLooping()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim replay As DialogResult = _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBox.Show(&amp;quot;Wake up&amp;quot; &amp;amp; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Environment.NewLine &amp;amp; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Yes to wake up.&amp;quot; &amp;amp; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Environment.NewLine &amp;amp; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;No to snooze.&amp;quot;, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Alarm Clock&amp;quot;, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBoxButtons.YesNo, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBoxIcon.Exclamation)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; player.Stop()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If replay &amp;lt;&amp;gt; DialogResult.Yes Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myAlarm.Snooze()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If      &lt;br /&gt;End Sub&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This code defines a variable that references the Alarm class. In a SetTheAlarm method, the class creates an instance of the class, passing in an alarm time and snooze interval.&lt;/p&gt;  &lt;p&gt;For testing, this is set to 1 minute from the current time and the snooze is set to one minute. In most cases, you would want to get the alarm time from the user.&lt;/p&gt;  &lt;p&gt;When the alarm goes off, this code plays a .wav file using the SoundPlayer provided in .NET. Be sure to change the .wav file name to the directory and file name of a .wav file on your system.&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;PlayLooping&lt;/strong&gt; method of the SoundPlayer is used to play the sound continuously. This method is asynchronous, so the code will continue to the next line.&lt;/p&gt;  &lt;p&gt;The example displays a message box, allowing the user to cancel the alarm or snooze the alarm. In either cause, the code calls the Stop method of the SoundPlayer to stop the .wav file. If the user requested to snooze, it calls the alarm&amp;#39;s &lt;strong&gt;Snooze&lt;/strong&gt; method.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1718734" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Building a Business Object Base Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx</link><pubDate>Tue, 21 Jul 2009 18:49:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1704681</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1704681</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx#comments</comments><description>&lt;p&gt;If you are building applications in .NET to manage data for a business, you are most likely creating business object classes. Depending on the business, these classes could include Customer, Product, Order, Invoice, PurchaseOrder, Employee, TimeCard and so on.&lt;/p&gt;  &lt;p&gt;A simple sample Customer class is shown &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;There are some features that all business objects need to support. For example:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Entity State: The business object needs to track whether it is new, updated, or deleted so it can make the appropriate change to the database. &lt;/li&gt;    &lt;li&gt;Validation: Is the current data defined for the business object valid? &lt;/li&gt;    &lt;li&gt;Save: Saves the changes to the database. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Notice that data retrieval functionality is not included in this list. That is because in most cases, you may never retrieve a single business object. Rather, you would retrieve a set of them. For example, all active orders or all customers with overdue invoices. So the retrieve functionality is not included in the class that manages a single object. (Most on this in a later post.)&lt;/p&gt;  &lt;p&gt;Instead of repeating this common functionality in each business object, it makes sense to build a base class. Each business object can then inherit from this base class to share this common functionality.&lt;/p&gt;  &lt;p&gt;So let’s get started.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;using System;      &lt;br /&gt;using System.ComponentModel;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public abstract class BoBase :      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; IDataErrorInfo,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; INotifyPropertyChanged       &lt;br /&gt;{       &lt;br /&gt;      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Imports System.ComponentModel      &lt;br /&gt;      &lt;br /&gt;Public MustInherit Class BOBase       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Implements IDataErrorInfo       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Implements INotifyPropertyChanged       &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The class is abstract (MustInherit in VB) to indicate that it is meant to be a base class and not to be used on its own. An abstract class cannot be instantiated directly, so no other code can create an instance of the class.&lt;/p&gt;  &lt;p&gt;The class then implements two interfaces:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;IDataErrorInfo: This interface provides error information that the user interface can use to report validation errors to the user. It works well with the ErrorProvider control provided in WinForms and supports ASP.NET and WPF features. &lt;/li&gt;    &lt;li&gt;INotifyPropertyChanged: This interface ensures that the user interface is notified when a property value changes, keeping your business object and user interface in sync. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We’ll look at the implementation of these interfaces shortly.&lt;/p&gt;  &lt;p&gt;First, define the valid set of entity states.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;protected internal enum EntityStateType      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Unchanged,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Added,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Deleted,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Modified       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Protected Friend Enum EntityStateType      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Unchanged       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Added       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Deleted       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Modified       &lt;br /&gt;End Enum&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The Enum is declared Protected because the entity state should only be accessible from the business object itself. However, Internal (Friend in VB) was added so that related objects (such as Order and OrderLineItem) could reference the related object state.&lt;/p&gt;  &lt;p&gt;The business object state is retained using an EntityState property.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;protected EntityStateType EntityState { get; private set; }&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _EntityState As EntityStateType      &lt;br /&gt;Protected Property EntityState() As EntityStateType       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _EntityState       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As EntityStateType)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _EntityState = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This property is protected, but the setter is private. So the business objects that inherit from this class can read the entity state, but only the base class can set the value.&lt;/p&gt;  &lt;p&gt;Additional properties provide a way to get the entity’s state in an easier fashion. These properties are not required, but they make the base class a little easier to use.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;[BindableAttribute(false)]      &lt;br /&gt;[BrowsableAttribute(false)]       &lt;br /&gt;public bool IsDirty       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get { return this.EntityState != EntityStateType.Unchanged; }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;[BindableAttribute(false)]      &lt;br /&gt;[BrowsableAttribute(false)]       &lt;br /&gt;public bool IsNew       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get { return this.EntityState == EntityStateType.Added; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;lt;BindableAttribute(False)&amp;gt; _      &lt;br /&gt;&amp;lt;BrowsableAttribute(False)&amp;gt; _       &lt;br /&gt;Public ReadOnly Property IsDirty() As Boolean       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return Me.EntityState &amp;lt;&amp;gt; EntityStateType.Unchanged       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;lt;BindableAttribute(False)&amp;gt; _      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;lt;BrowsableAttribute(False)&amp;gt; _      &lt;br /&gt;Public ReadOnly Property IsNew() As Boolean       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return Me.EntityState = EntityStateType.Added       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;End Property&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The IsDirty and IsNew properties are public, so they can be accessed from anywhere. They are marked with two attributes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Bindable: Defines whether the property should be used for binding. In this case the value is false because the user interface should not be able to bind to this property. &lt;/li&gt;    &lt;li&gt;Browsable: Defines whether the property should be displayed in the Properties window. Again, the value is false. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Two other properties handle the validation.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;[BindableAttribute(false)]      &lt;br /&gt;[BrowsableAttribute(false)]       &lt;br /&gt;public bool IsValid       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get { return (ValidationInstance.Count == 0); }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;protected Validation ValidationInstance { get; set; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt; &lt;font color="#65402e" face="Consolas"&gt;&amp;lt;Bindable(False)&amp;gt; _&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;lt;BrowsableAttribute(False)&amp;gt; _     &lt;br /&gt;Public ReadOnly Property IsValid() As Boolean     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return (ValidationInstance.Count = 0)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get     &lt;br /&gt;End Property &lt;/font&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _ValidationInstance As Validation      &lt;br /&gt;Protected Property ValidationInstance() As Validation       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _ValidationInstance       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As Validation)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ValidationInstance = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Again, the public property is marked with the Browsable and Bindable attributes. The ValidationInstance property retains an instance of the Validation class for the business object. The code for the Validation class is &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/16/validation-class.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The constructor creates an instance of the Validation class.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;protected BoBase()      &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; ValidationInstance = new Validation();       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Protected Sub New()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ValidationInstance = New Validation      &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The following is the implementation of IDataErrorInfo.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#region IDataErrorInfo Members &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;[BrowsableAttribute(false)]      &lt;br /&gt;[BindableAttribute(false)]       &lt;br /&gt;public string Error       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get { return ValidationInstance.ToString(); }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;[BrowsableAttribute(false)]      &lt;br /&gt;[BindableAttribute(false)]       &lt;br /&gt;public string this[string columnName]       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get { return ValidationInstance[columnName]; }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#endregion&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#Region &amp;quot; Properties required by the IDataErrorInfo&amp;quot;      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Bindable(False)&amp;gt; _&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;BrowsableAttribute(False)&amp;gt; _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public ReadOnly Property [Error]() As String _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Implements IDataErrorInfo.Error       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return ValidationInstance.ToString       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;BrowsableAttribute(False)&amp;gt; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Bindable(False)&amp;gt; _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Default Protected ReadOnly Property Item(ByVal columnName _&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; As String) As String _&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Implements IDataErrorInfo.Item       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return ValidationInstance.Item(columnName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property       &lt;br /&gt;      &lt;br /&gt;#End Region&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Error&lt;/strong&gt; property uses the overridden ToString method of the validation class to return the full list of validation errors.&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Item&lt;/strong&gt; property provides access to the validation errors given a property name. This property is implemented as the class indexer in C#.&lt;/p&gt;  &lt;p&gt;The following in the implementation of INotifyPropertyChanged.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#region INotifyPropertyChanged Members     &lt;br /&gt;public event PropertyChangedEventHandler PropertyChanged;      &lt;br /&gt;#endregion&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#Region &amp;quot; Events required by INotifyPropertyChanged&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Event PropertyChanged(ByVal sender As Object, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal e As System.ComponentModel.PropertyChangedEventArgs) _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Implements INotifyPropertyChanged.PropertyChanged      &lt;br /&gt;#End Region&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This interface only defines a single event. This event should be raised whenever the data is changed.&lt;/p&gt;  &lt;p&gt;Since every business object will have unique requirements for the save operation, the &lt;strong&gt;SaveItem&lt;/strong&gt; method is not implemented. Rather it is defined as abstract.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public abstract Boolean SaveItem();&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public MustOverride Function SaveItem() As Boolean&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Defining an abstract (MustOverride in VB) SaveItem method ensures that every business object has a SaveItem method.&lt;/p&gt;  &lt;p&gt;Finally, since the EntityState property setter is private, the base class needs a method to set the entity state.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;protected internal void SetEntityState(EntityStateType newEntityState)     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; switch (newEntityState)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case EntityStateType.Deleted:      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case EntityStateType.Unchanged:      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case EntityStateType.Added:      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.EntityState = newEntityState;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; default:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (this.EntityState == EntityStateType.Unchanged)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.EntityState = newEntityState;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;protected internal void SetEntityState(EntityStateType newEntityState, string propertyName)     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SetEntityState(newEntityState);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (PropertyChanged != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyChanged(this,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;new PropertyChangedEventArgs(propertyName));     &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Protected Friend Sub SetEntityState(ByVal dataState As EntityStateType)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SetEntityState(dataState, Nothing)      &lt;br /&gt;End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Protected Friend Sub SetEntityState( _     &lt;br /&gt; ByVal newEntityState As EntityStateType, ByVal propertyName As String)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Select Case newEntityState      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Case EntityStateType.Deleted, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EntityStateType.Unchanged, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EntityStateType.Added      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Me.EntityState = newEntityState &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Case Else     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If Me.EntityState = EntityStateType.Unchanged Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Me.EntityState = newEntityState      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Select &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; If Not String.IsNullOrEmpty(propertyName) Then     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim e As New PropertyChangedEventArgs(propertyName)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RaiseEvent PropertyChanged(Me, e)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If      &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;SetEntityState&lt;/strong&gt; method has two overloads. The first is used when changing the entity state in general and the second is used when changing the entity state because a specific property is changed.&lt;/p&gt;  &lt;p&gt;For example, when setting an object as Unchanged, Added, or Deleted, it does not matter which property was changed. But when a particular property is changed, the code must also raise the PropertyChanged event.&lt;/p&gt;  &lt;p&gt;In this case, the C# and VB code was implemented differently. In the C# code, the code to set the entity state is in the first overload. The second overload then calls the first and then raises the event.&lt;/p&gt;  &lt;p&gt;In the VB code, the first overload simply calls the second overload. The second overload then sets the entity state and then raises the event as appropriate. You can use either technique in either language.&lt;/p&gt;  &lt;p&gt;That’s it! You now have a base class that can be used with any business object class. If you have any other common functionality, you can add it to this base class. &lt;/p&gt;  &lt;p&gt;Here is an example of how you use this base class.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class Customer : BoBase     &lt;br /&gt;{      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private string _LastName;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string LastName      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return _LastName; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (_LastName == null || _LastName != value)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string propertyName = &amp;quot;LastName&amp;quot;;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _LastName = value; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Validate the last name     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateClear(propertyName);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateRequired(propertyName, value); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SetEntityState(EntityStateType.Modified, propertyName);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; public override Boolean SaveItem()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // TODO: Add code here      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class Customer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Inherits BoBase&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _LastName As String     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property LastName() As String      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _LastName      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As String)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If _LastName Is Nothing OrElse _LastName &amp;lt;&amp;gt; value Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim propertyName As String = &amp;quot;LastName&amp;quot;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _LastName = value &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Validate the last name     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateClear(propertyName)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateRequired(propertyName, value) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SetEntityState(EntityStateType.Modified, propertyName)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Public Overrides Function SaveItem() As Boolean     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; TODO: Add code here      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Function      &lt;br /&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Notice how the class statement includes the syntax to inherit from BoBase. The LastName property uses the ValidationInstance defined in the base class to validate the value. It also sets the entity state when the last name is changed.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1704681" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Enum/default.aspx">Enum</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Binding/default.aspx">Binding</category></item><item><title>Validation Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/16/validation-class.aspx</link><pubDate>Thu, 16 Jul 2009 23:15:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1702427</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1702427</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/16/validation-class.aspx#comments</comments><description>&lt;p&gt;A common requirement in most applications is to validate the data entered by the user. This is such a common requirement, that it makes sense to build a reusable Validation class. This post details the beginnings of a .NET Validation class.&lt;/p&gt;  &lt;p&gt;The class was originally designed to validate business object properties. But it could also be used to directly validate fields in your user interface if you are not yet using business objects. &lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;namespace InStepLibrary      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public class Validation       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Imports System.Text&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class Validation&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This class uses a StringBuilder, hence the need for the System.Text imports.&lt;/p&gt;  &lt;p&gt;The Validation class has three properties.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public int Count      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return ValidationList.Count;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public string this[string propertyName]      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (ValidationList.ContainsKey(propertyName))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return ValidationList[propertyName];       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return null;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;private Dictionary&amp;lt;String, String&amp;gt; ValidationList { get; set; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;&lt;font color="#65402e"&gt;Public ReadOnly Property Count() As Integer        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return ValidationList.Count         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get         &lt;br /&gt;End Property&lt;/font&gt; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public ReadOnly Property Item(ByVal propertyName As String) As String      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If ValidationList.ContainsKey(propertyName) Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return ValidationList.Item(propertyName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return Nothing       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _ValidationList As Dictionary(Of String, String)      &lt;br /&gt;Private Property ValidationList() As Dictionary(Of String, String)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _ValidationList       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Dictionary(Of String, String))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ValidationList = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;ValidationList&lt;/strong&gt; property retains a private Dictionary of validation errors. The key of the dictionary is the property name and the value is the error text. For example, the “LastName” property may have a validation error such as “Last Name is required.”&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Count&lt;/strong&gt; property returns the number of properties with validation errors by returning the count of the ValidationList items. &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Item&lt;/strong&gt; property provides access to the validation errors given a property name. This property is implemented as the class indexer in C#.&lt;/p&gt;  &lt;p&gt;The constructor ensures that a new instance of the ValidationList Dictionary is created.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public Validation()      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Create the list to contain the validation errors       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ValidationList = new Dictionary&amp;lt;String, String&amp;gt;();       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Sub New()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Create the list to contain the validation errors       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ValidationList = New Dictionary(Of String, String)       &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;ToString&lt;/strong&gt; method is overwritten in the Validation class to build a single string containing all of the validation errors.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public override string ToString()      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; StringBuilder sb = new StringBuilder(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; foreach (string k in ValidationList.Values)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sb.AppendLine(k + &amp;quot;: &amp;quot; + ValidationList[k]); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; return sb.ToString();      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Overrides Function ToString() As String      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim sb As New StringBuilder &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; For Each k As String In ValidationList.Keys      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sb.AppendLine(k &amp;amp; &amp;quot;: &amp;quot; &amp;amp; ValidationList(k))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Next       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Return sb.ToString       &lt;br /&gt;End Function&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This method uses the StringBuilder class to build up the potentially large string of errors.&lt;/p&gt;  &lt;p&gt;When a validation error is added to the list, it is added for a particular property. If the property already has a validation error, additional validation errors are appended to it, separated by semi-colons (;). An &lt;strong&gt;AddValidationError&lt;/strong&gt; method handles this logic.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;private void AddValidationError(string propertyName, string message)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // If the property already has a message, append this message       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (ValidationList.ContainsKey(propertyName))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string existingMessage = ValidationList[propertyName]; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (!existingMessage.Contains(message))      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Append the new message to the existing message       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationList[propertyName] += &amp;quot;; &amp;quot; + message;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Add the message to the validation list       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationList.Add(propertyName, message);       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private Sub AddValidationError(ByVal propertyName As String, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal message As String) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; If the property already has a message, append this message      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If ValidationList.ContainsKey(propertyName) Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim existingMessage As String = ValidationList(propertyName) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If Not existingMessage.Contains(message) Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Append the new message to the existing message       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationList(propertyName) &amp;amp;= &amp;quot;; &amp;quot; &amp;amp; message       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Add the message to the validation list       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationList.Add(propertyName, message)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;A &lt;strong&gt;ValidateClear&lt;/strong&gt; method clears any existing validation errors for a property. This method should be called before performing any new validation on the property. For example, the user leaves the Last Name field empty. The validation is performed and a validation error entry is created in the ValidationList. Then the user enters a value into the last name field. The original validation error must be cleared before revalidating the value.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public void ValidateClear(string propertyName)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // If the Property doesn&amp;#39;t have any messages, this is done       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (ValidationList.ContainsKey(propertyName))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Otherwise, remove the entry       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationList.Remove(propertyName);       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Sub ValidateClear(ByVal propertyName As String)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; If the Property doesn&amp;#39;t have any messages, this is done       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If ValidationList.ContainsKey(propertyName) Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Otherwise, remove the entry       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationList.Remove(propertyName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The ValidateClear method uses the Remove method of the Dictionary to remove any Dictionary entry for the property.&lt;/p&gt;  &lt;p&gt;That’s it for the basics of the Validation class. The only thing that is left to do is build all of the methods to perform the types of validation that your application requires.&lt;/p&gt;  &lt;p&gt;For example, a common requirement is to ensure that a property is not left blank.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public bool ValidateRequired(string propertyName,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string value)       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string newMessage = String.Empty; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; if (String.IsNullOrEmpty(value))      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newMessage =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String.Format(&amp;quot;{0} is required, please enter a valid value&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; propertyName);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Add the message to the validation list       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddValidationError(propertyName, newMessage);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Function ValidateRequired(ByVal propertyName As String, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal value As String) As Boolean       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim newMessage As String = String.Empty &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; If String.IsNullOrEmpty(value) Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newMessage = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String.Format(&amp;quot;{0} is required. Please enter a valid value.&amp;quot;, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; propertyName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Add the message to the validation list       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddValidationError(propertyName, validationMessage)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return False       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return True       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;End Function&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This function performs the validation, adding a validation error as appropriate.&lt;/p&gt;  &lt;p&gt;Another common requirement is that a property not exceed a maximum length.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public Boolean ValidateLength(string propertyName,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string value, int maxLength)       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; String newMessage = String.Empty; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; if (!String.IsNullOrEmpty(value) &amp;amp;&amp;amp; value.Length &amp;gt; maxLength)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newMessage = String.Format(&amp;quot;{0} has a maximum size of {1}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; propertyName, maxLength);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Add the message to the validation list       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddValidationError(propertyName, newMessage);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Function ValidateLength(ByVal propertyName As String, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal value As String, ByVal maxLength As Integer) As Boolean       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim sMessage As String = String.Empty&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; If Not String.IsNullOrEmpty(value) AndAlso _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; value.Length &amp;gt; maxLength Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sMessage = String.Format(&amp;quot;{0} has a maximum size of {1}.&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; propertyName, maxLength)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Add the message to the validation list       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddValidationError(propertyName, sMessage)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return False       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return True       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;End Function&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;You get the idea. You can add any number of these to perform whatever validation your application requires.&lt;/p&gt;  &lt;p&gt;Some additional suggestions:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ValidateAlphaNumeric &lt;/li&gt;    &lt;li&gt;ValidateDirectory &lt;/li&gt;    &lt;li&gt;ValidateEnum &lt;/li&gt;    &lt;li&gt;ValidateFileExists &lt;/li&gt;    &lt;li&gt;ValidateMinLength &lt;/li&gt;    &lt;li&gt;ValidateNonZero &lt;/li&gt;    &lt;li&gt;ValidateNoSpaces &lt;/li&gt;    &lt;li&gt;ValidateNumeric &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;You can create any validation method you need for your application. Just add code to perform the validation and then call AddValidationError as appropriate.&lt;/p&gt;  &lt;p&gt;You call these validation methods from your business objects as shown below.&lt;/p&gt;  &lt;p&gt;NOTE: This code assume you have a ValidationInstance variable that is declared in your class as a new instance of the Validation class.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;private string _LastName;      &lt;br /&gt;public string LastName       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; get { return _LastName; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (_LastName == null || _LastName != value)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string propertyName = &amp;quot;LastName&amp;quot;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _LastName = value; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Validate the last name      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateClear(propertyName);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateRequired(propertyName, value);&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _LastName As String      &lt;br /&gt;Public Property LastName() As String       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _LastName       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As String)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If _LastName Is Nothing OrElse _LastName &amp;lt;&amp;gt; value Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim propertyName As String = &amp;quot;LastName&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _LastName = value &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Validate the last name      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateClear(propertyName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidationInstance.ValidateRequired(propertyName, value)&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Or you can call these validation methods directly from your user interface as shown below.&lt;/p&gt;  &lt;p&gt;NOTE: This code assume you have a ValidationInstance variable that is declared in your form as a new instance of the Validation class.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;private void textBox1_Validating(object sender, CancelEventArgs e)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string propertyName&amp;#160; = &amp;quot;Last Name&amp;quot;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; validationInstance.ValidateClear(propertyName);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; validationInstance.ValidateRequired(propertyName, textBox1.Text);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; validationInstance.ValidateLength(propertyName, textBox1.Text, 20); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; if (validationInstance.Count &amp;gt; 0)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; errorProvider1.SetError(textBox1,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; validationInstance[propertyName]);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Clear the validation error       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; errorProvider1.SetError(textBox1, String.Empty);       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private Sub TextBox1_Validating(ByVal sender As Object, _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ByVal e As System.ComponentModel.CancelEventArgs) _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Handles TextBox1.Validating       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim propertyName As String = &amp;quot;Last Name&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; validationInstance.ValidateClear(propertyName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; validationInstance.ValidateRequired(propertyName, TextBox1.Text)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; validationInstance.ValidateLength(propertyName, TextBox1.Text, 20) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; If validationInstance.Count &amp;gt; 0 Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ErrorProvider1.SetError(TextBox1, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; validationInstance.Item(propertyName))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Clear the validation error       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ErrorProvider1.SetError(TextBox1, String.Empty)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Hope this helps you encapsulate all of your validation logic into a reusable class.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1702427" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Populating a Business Object from a DataTable</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/10/populating-a-business-object-from-a-datatable.aspx</link><pubDate>Fri, 10 Jul 2009 20:43:39 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1699360</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1699360</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/10/populating-a-business-object-from-a-datatable.aspx#comments</comments><description>&lt;p&gt;Most business applications have business objects such as customer, order, or invoice. Often, the data access layer (DAL) provides the data and your code needs to use that data to manually populate a business object.&lt;/p&gt;  &lt;p&gt;This post describes how to manually populate a business object from a DataTable. It uses the Customer class defined &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The code is first shown in both VB and C#. It is then described in detail below.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public static List&amp;lt;Customer&amp;gt; Retrieve()      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160; DataTable dt = Dac.ExecuteDataTable(&amp;quot;CustomerRetrieveAll&amp;quot;, null); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Customer&amp;gt; customerList = new List&amp;lt;Customer&amp;gt;(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; foreach (DataRow dr in dt.Rows)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customerList.Add(new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerId = (int)dr[&amp;quot;CustomerId&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = (string)dr[&amp;quot;LastName&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName = (string)dr[&amp;quot;FirstName&amp;quot;]       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; return customerList;      &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Shared Function Retrieve() As List(Of Customer)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Dim dt As DataTable = Dac.ExecuteDataTable( _     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;quot;CustomerRetrieveAll&amp;quot;, nothing) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Dim customerList As New List(Of Customer) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; For Each dr As DataRow In dt.Rows      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customerList.Add(New Customer With _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {.CustomerId = CType(dr(&amp;quot;CustomerID&amp;quot;), Integer), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = dr(&amp;quot;LastName&amp;quot;).ToString, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = dr(&amp;quot;FirstName&amp;quot;).ToString})&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Next &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Return customerList      &lt;br /&gt;End Function&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The Retrieve method is public and static (shared in VB). It is public so it can be called from the user interface code. It is static/shared because it does not use or retain any state. This allows the function to be called without creating an instance of the class containing the function.&lt;/p&gt;  &lt;p&gt;This function calls the ExecuteDataTable method from &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-retrieve-a-datatable-using-a-stored-procedure.aspx" target="_blank"&gt;here&lt;/a&gt;, which returns a DataTable. It then creates a new list and adds a new customer to the list for each row in the DataTable.&lt;/p&gt;  &lt;p&gt;The resulting list of customers is returned. The user interface code can then use this list to bind to a control such as a grid or combo box. You can also search, sort, filter, or work with this list using LINQ or Lambda expressions.&lt;/p&gt;  &lt;p&gt;NOTE: If the fields in the DataTable match the properties of the business object, you could use reflection to map the fields to the properties. Though reflection does have a performance hit, it provides a more general solution that could be used by any business object.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1699360" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Data/default.aspx">Data</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/DataTable/default.aspx">DataTable</category></item><item><title>Zodiac Signs: DateRange Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/zodiac-signs-daterange-class.aspx</link><pubDate>Mon, 06 Jul 2009 21:56:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698497</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698497</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/zodiac-signs-daterange-class.aspx#comments</comments><description>&lt;p&gt;This entry details the implementation of the DateRange class from &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-situations-chinese-zodiac-signs.aspx"&gt;this example&lt;/a&gt; in C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class DateRange     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public DateTime StartDate { get; set; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public DateTime EndDate { get; set; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; // Constructor     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public DateRange(DateTime startdate, DateTime enddate)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.StartDate = startdate;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.EndDate = enddate;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This class uses the automatically implemented properties feature that was introduced to C# in .NET 3.5 to define the two DateTime properties.&lt;/p&gt;  &lt;p&gt;The constructor in this example creates a DateRange with an appropriate start date and end date. Though this class was created for the Zodiac Signs example, it could be used anywhere a DateRange is needed.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698497" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Zodiac Sign: ZodiacSigns Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/zodiac-sign-zodiacsigns-class.aspx</link><pubDate>Mon, 06 Jul 2009 21:53:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698496</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698496</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/zodiac-sign-zodiacsigns-class.aspx#comments</comments><description>&lt;p&gt;This entry details the implementation of the ZodiacSigns class from &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-situations-chinese-zodiac-signs.aspx"&gt;this example&lt;/a&gt; in C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class ZodiacSigns : List&amp;lt;ZodiacSign&amp;gt;      &lt;br /&gt;{&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;h1&gt;Constructor&lt;/h1&gt;  &lt;p&gt;The following is the constructor defined in the ZodiacSigns class:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public ZodiacSigns()     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; InitializeCollection();      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This constructor ensures that the collection of zodiac signs is initialized when an instance of this class is created.&lt;/p&gt;  &lt;h1&gt;Methods&lt;/h1&gt;  &lt;p&gt;The following are the methods in the ZodiacSigns class:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public string FindSign(DateTime desiredDate)     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Find the name of the Zodiak sign with the date within the ranges      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var query = from z in this      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; from d in z.DateRanges      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where (desiredDate &amp;gt;= d.StartDate) &amp;amp;&amp;amp;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;(desiredDate &amp;lt;= d.EndDate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select z.Name;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string name = query.FirstOrDefault();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return name;      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The FindSign method uses LINQ to find the date within the defined ranges. It returns the name of the appropriate sign.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;private void InitializeCollection()     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // This could potentially read all of these from a file.      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // NOTE: This data may not be accurate      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Rat&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(1996, 2,19),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(1997, 2,6)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2008, 2,7),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2009, 2,25))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Ox&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(1997, 2,7),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(1998, 2,27)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2009, 2,26),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2010, 2,13))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Tiger&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(1998, 2,28),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(1999, 2,15)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2010, 2,14),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2011, 2,2))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Rabbit&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(1999, 2,16),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2000, 2,4)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2011, 2,3),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2012, 1,22))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Drago&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2000, 2,5),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2001, 2,23)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2012, 1,23),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2013, 2,9))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Snake&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2001, 1,24),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2002, 2,11)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2013, 2,10),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2014, 1,30))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Horse&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2002, 2,12),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2003, 1,31)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2014, 1,31),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2015, 2,18))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Sheep&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2003, 2,1),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2004, 2,21)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2015, 2,19),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2016, 2,7))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Monkey&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2004, 1, 22),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2005, 2, 8)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2016, 2, 8),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2017, 1, 27))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Rooster&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2005, 2, 9),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2006, 1, 28)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2017, 1, 28),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2018, 2, 15))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Dog&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2006, 1, 29),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2007, 2, 17)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2018, 2, 16),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2019, 2, 4))}));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Add(new ZodiacSign(&amp;quot;Pig&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new List&amp;lt;DateRange&amp;gt; {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2007, 2, 18),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2008, 2, 6)),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateRange(new DateTime(2019, 2, 5),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new DateTime(2020, 2, 21))}));      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The InitializeCollection method does exactly what it sounds like … creating the set of date ranges associated with each zodiac sign. This code takes advantage of the list initializers now available in C#.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698496" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Zodiac Sign: ZodiacSign Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/zodiac-sign-zodiacsign-class.aspx</link><pubDate>Mon, 06 Jul 2009 21:40:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698492</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698492</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/zodiac-sign-zodiacsign-class.aspx#comments</comments><description>&lt;p&gt;This entry details the implementation of the ZodiacSign class from &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-situations-chinese-zodiac-signs.aspx" target="_blank"&gt;this example&lt;/a&gt; in C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class ZodiacSign      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Properties       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Name { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public List&amp;lt;DateRange&amp;gt; DateRanges { get; set; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; // Constructor      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public ZodiacSign(string name, List&amp;lt;DateRange&amp;gt; dateranges)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Name = name;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.DateRanges = dateranges;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This example uses the automatically implemented properties feature that was introduced to C# in .NET 3.5 to define the two properties: Name and DateRanges.&lt;/p&gt;  &lt;p&gt;The constructor in this example creates a zodiac sign with an appropriate name and set of date ranges.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698492" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Applying OOP to Simple Situations: Chinese Zodiac Signs</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-situations-chinese-zodiac-signs.aspx</link><pubDate>Mon, 06 Jul 2009 21:34:35 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698490</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698490</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-situations-chinese-zodiac-signs.aspx#comments</comments><description>&lt;p&gt;Here is the story defining the simple use case for this application:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The user picks a date between 2/19/1996 and 2/5/2019. &lt;/li&gt;    &lt;li&gt;The system displays the appropriate Chinese zodiac sign (Monkey, Dog, Rat, etc) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Seems simple enough. So how to implement this …&lt;/p&gt;  &lt;h1&gt;Defining the Classes&lt;/h1&gt;  &lt;p&gt;The first step in using OOP with a simple situation is the same as with any application … define the &amp;quot;nouns”.&lt;/p&gt;  &lt;p&gt;These are the first nouns I came up with:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Zodiac sign &lt;/li&gt;    &lt;li&gt;Date range &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The next step is to think through each of these nouns and determine which make sense as classes for building the code to support this feature.&lt;/p&gt;  &lt;h2&gt;Zodiac Sign&lt;/h2&gt;  &lt;p&gt;This feature needs to work with a zodiac sign, so a ZodiacSign class makes sense.&lt;/p&gt;  &lt;p&gt;In addition, this feature needs to retain the set of zodiac signs. So a ZodiacSigns (plural) class is also needed to track the list of ZodiacSign instances.&lt;/p&gt;  &lt;h2&gt;Date Range&lt;/h2&gt;  &lt;p&gt;Each zodiac sign is associated with a date range, so a DateRange class makes sense to track the dates.&lt;/p&gt;  &lt;h1&gt;Defining the Properties and Methods&lt;/h1&gt;  &lt;p&gt;The next step is to define what data that each class retains (called &lt;strong&gt;properties&lt;/strong&gt; in OOP) and what functionality that the class provides (called &lt;strong&gt;methods&lt;/strong&gt; in OOP).&lt;/p&gt;  &lt;p&gt;After reviewing the nouns, three classes were defined:&lt;/p&gt;  &lt;h2&gt;ZodiacSign&lt;/h2&gt;  &lt;p&gt;This class provides the definition of a single Chinese zodiac sign. The properties for this class include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Name: Name of the sign such as “dog” or “monkey”. &lt;/li&gt;    &lt;li&gt;DateRanges: Set of date ranges associated with the sign. &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;ZodiacSigns&lt;/h2&gt;  &lt;p&gt;This class manages the list of all zodiac signs. To leverage the .NET Framework List features, this class can inherit from the built in generic List class.&lt;/p&gt;  &lt;p&gt;This class has two methods:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;InitializeCollection: Builds the list with the set of zodiac signs and date ranges. &lt;/li&gt;    &lt;li&gt;FindSign: Given a date, finds the sign. &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;DateRange&lt;/h2&gt;  &lt;p&gt;This class has two simple properties:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Start Date: First date of the date range. &lt;/li&gt;    &lt;li&gt;End Date: End date of the date range. &lt;/li&gt; &lt;/ul&gt;  &lt;h1&gt;Using the Classes&lt;/h1&gt;  &lt;p&gt;The user interface portion of the application creates an instance of the ZodiacSigns class and calls the FindSign method as needed.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698490" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>MasterMind: Peg Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/mastermind-peg-class.aspx</link><pubDate>Mon, 06 Jul 2009 20:07:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698474</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698474</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/mastermind-peg-class.aspx#comments</comments><description>&lt;p&gt;This entry describes the Peg class from &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-games-mastermind.aspx"&gt;this example&lt;/a&gt; in further detail:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class Peg&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;h1&gt;Properties&lt;/h1&gt;  &lt;p&gt;The properties of the class are as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _Column As Integer      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets the column of this peg.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property Column() As Integer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _Column       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _Column = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The Column property defines the column of the board that contains this peg. The setter is private because once the peg is created, its location cannot be moved.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _Correct As Boolean?      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets or sets whether the peg is in a correct position.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property Correct() As Boolean?       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _Correct       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Friend Set(ByVal value As Boolean?)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _Correct = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The Correct property defines whether this peg denotes a correct answer. A correct answer requires that the peg be of the same color and column position as the correct answer.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _PegColor As Color?      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets or sets the peg color.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property PegColor() As Color?       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _PegColor       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Color?)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _PegColor = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The PegColor property defines the color of this peg as defined by the user.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _Row As Integer      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets the row containing the peg.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property Row() As Integer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _Row       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _Row = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The Row property defines the row of the board that contains this peg. The setter is private because once the peg is created, its location cannot be moved.&lt;/p&gt;  &lt;h1&gt;Constructor&lt;/h1&gt;  &lt;p&gt;&lt;font face="v"&gt;The following is the constructor defined in the Peg class:&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Constructs a new instance in a specific position.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;columnIndex&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;rowIndex&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Sub New(ByVal columnIndex As Integer, ByVal rowIndex As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Me.Column = columnIndex       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Me.Row = rowIndex       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Me.PegColor = Nothing       &lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Download the sample code (that is currently only in VB) from &lt;a href="http://www.insteptech.com/techLibrary/samplecode.htm"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698474" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>MasterMind: MasterMind Class</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/mastermind-mastermind-class.aspx</link><pubDate>Mon, 06 Jul 2009 20:00:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698472</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698472</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/mastermind-mastermind-class.aspx#comments</comments><description>&lt;p&gt;This entry describes the MasterMind class from &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-games-mastermind.aspx" target="_blank"&gt;this example&lt;/a&gt; in further detail:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class MasterMind&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;h1&gt;Properties&lt;/h1&gt;  &lt;p&gt;The properties of the class are as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _Answer As List(Of Color)      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets the answer.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property Answer() As List(Of Color)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _Answer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As List(Of Color))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _Answer = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The Answer property is a list of correct colors. This property has a Private setter because the answer itself is managed internal to the MasterMind class.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _Board As List(Of Peg)      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets or sets the board for the game.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property Board() As List(Of Peg)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _Board       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As List(Of Peg))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _Board = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The Board property is a list of Pegs. This property also has a Private setter because the board is managed internal to the MasterMind class.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _CurrentTurn As Integer      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets or sets the number of the current turn.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; The user cannot go back to any prior turn.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property CurrentTurn() As Integer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _CurrentTurn       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _CurrentTurn = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The CurrentTurn property counts the number of guesses. This property also has a Private setter because it is managed internal to the MasterMind class.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private Shared _GameColors As List(Of Color)      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets the game colors.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Shared Property GameColors() As List(Of Color)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If _GameColors Is Nothing Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors = New List(Of Color)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Red)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Yellow)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Green)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Purple)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Blue)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Black)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Orange)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors.Add(Color.Pink)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _GameColors       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As List(Of Color))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _GameColors = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The GameColors property defines the set of available peg colors. These are hard-coded into the application, but could instead be settable by a game configuration feature (which is not implemented in this sample). Because the set of colors is the same for every game (at least in this implementation), they are defined using a Shared property.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _NumberOfRows As Integer = 8      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets the number of rows that should be allowed in the game.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property NumberOfRows() As Integer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _NumberOfRows       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _NumberOfRows = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The NumberOfRows property defines the number of rows in the game. This implementation defines a default value of 8 rows. This could instead by set by the user with a configuration feature (which is not implemented in this sample).&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _NumberOfHoles As Integer = 4      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Gets the number of holes in each row that should be allowed in the game.       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;value&amp;gt;&amp;lt;/value&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;       &lt;br /&gt;Public Property NumberOfHoles() As Integer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _NumberOfHoles       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Set(ByVal value As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _NumberOfHoles = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The NumberOfHoles property defines the number of holes in each row in the game. This implementation defines a default value of 4 holes. This could instead by set by the user with a configuration feature (which is not implemented in this sample).&lt;/p&gt;  &lt;h1&gt;Constructors&lt;/h1&gt;  &lt;p&gt;The following are the constructors defined in the MasterMind class:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;     &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Construct the game with the default holes and rows      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;      &lt;br /&gt;Public Sub New()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Initialize the game      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; InitializeGame()      &lt;br /&gt;End Sub &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;     &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; Construct the game with a defined number of holes and rows.      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;holesPerRow&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;rows&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;      &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;      &lt;br /&gt;Public Sub New(ByVal holesPerRow As Integer, ByVal rows As Integer)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Me.NumberOfHoles = holesPerRow      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Me.NumberOfRows = rows &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Initialize the game     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; InitializeGame()      &lt;br /&gt;End Sub&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;When a new instance of the game is created, the InitializeGame method is called to set up the board and define an answer.&lt;/p&gt;  &lt;h1&gt;Methods&lt;/h1&gt;  &lt;p&gt;The following are the MasterMind class methods:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#Region &amp;quot; CreateAnswer&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; Creates the answer for the game      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Sub CreateAnswer()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim maxColorIndex As Integer = GameColors.Count - 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim colorIndex As Integer &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Clear the answers     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Answer = New List(Of Color) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Set up for random numbers     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim randomColor As New Random() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Build the answer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; For hole As Integer = 0 To NumberOfHoles - 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; colorIndex = randomColor.Next(0, maxColorIndex) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Answer.Insert(hole, GameColors(colorIndex))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Next hole &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; End Sub     &lt;br /&gt;#End Region &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The CreateAnswer method uses the Random .NET Framework class to create a valid answer. Notice that this is a private method and cannot be called directly.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#Region &amp;quot; CreateBoard&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; Creates the board for the game.      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Sub CreateBoard()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Create the board as a list of potential pegs      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Board = New List(Of Peg) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim peg As Peg &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Build a control for each hole in the board.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; For rowIndex As Integer = 0 To NumberOfRows - 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; For pegIndex As Integer = 0 To NumberOfHoles - 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Create a peg      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; peg = New Peg(pegIndex, rowIndex) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Add it to the board     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Board.Add(peg)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Next      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Next &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; End Sub     &lt;br /&gt;#End Region &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The CreateBoard method builds the structure of the board with the defined number of rows and holes. Notice that this is a private method and cannot be called directly.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#Region &amp;quot; InitializeGame&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; Initialize the game.      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Private Sub InitializeGame()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Create the board      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CreateBoard() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Create the answer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CreateAnswer() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Initialize the try     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CurrentTurn = 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Sub      &lt;br /&gt;#End Region &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The InitializeGame method creates the board and defines the answer. It is also a private method that is called when the game is constructed.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;#Region &amp;quot; ProcessGuess&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; Processes the user&amp;#39;s guess.      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Function ProcessGuess() As Boolean      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Row is 0-based; turns are 1-based      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim row As Integer = CurrentTurn - 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim allCorrect As Boolean = True &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Get the pegs for the row     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim rowPegs = Board.Where(Function(p) p.Row = row) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; For Each p As Peg In rowPegs     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; If p.PegColor = Answer(p.Column) Then      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; This answer is correct      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; p.Correct = True      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Else      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; This answer is incorrect      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; p.Correct = False      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; allCorrect = False      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End If      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Next &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Increment the turn     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CurrentTurn += 1 &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return allCorrect     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Function      &lt;br /&gt;#End Region&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The ProcessGuess method is a public method that is called by the UI each time the user is finished with a row and wants to submit it as a guess.&lt;/p&gt;  &lt;p&gt;Download the sample code (that is currently only in VB) from &lt;a href="http://www.insteptech.com/techLibrary/samplecode.htm"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698472" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Applying OOP to Simple Games: MasterMind</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-games-mastermind.aspx</link><pubDate>Mon, 06 Jul 2009 19:36:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698467</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1698467</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/06/applying-oop-to-simple-games-mastermind.aspx#comments</comments><description>&lt;p&gt;Someone on the MSDN forums recently asked how to apply object-oriented programming (OOP) principles to a basic game. So I thought it would be an interesting project to develop a very simple sample game using OOP.&lt;/p&gt;  &lt;p&gt;I selected MasterMind because it is a relatively simple game. If you are not familiar with the game, you can find a description and picture &lt;a href="http://en.wikipedia.org/wiki/Mastermind_%28board_game%29" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;h1&gt;Defining the Classes&lt;/h1&gt;  &lt;p&gt;The first step in using OOP with a game is the same as with any application … define the &amp;quot;nouns”.&lt;/p&gt;  &lt;p&gt;These are the first nouns I came up with:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Game &lt;/li&gt;    &lt;li&gt;Board &lt;/li&gt;    &lt;li&gt;Code maker player &lt;/li&gt;    &lt;li&gt;Code breaker player &lt;/li&gt;    &lt;li&gt;Answer pattern &lt;/li&gt;    &lt;li&gt;Guess pegs &lt;/li&gt;    &lt;li&gt;Key (or Feedback) pegs &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The next step is to think through each of these nouns and determine which make sense as classes for building the game. &lt;/p&gt;  &lt;h2&gt;Game&lt;/h2&gt;  &lt;p&gt;The application will need to keep track of all of the facets of the game. So a game class makes sense. Since the class will only describe the MasterMind game, I selected to call this class “MasterMind”.&lt;/p&gt;  &lt;h2&gt;Board&lt;/h2&gt;  &lt;p&gt;Hmmm. Did the game need a separate board class? Other than defining the board layout, there was not much that the board itself needed to do. So the game could have a Board class with one method to create the board. Or the MasterMind class could have a Board property and the method to create the board. I selected to make the board a property to keep this “introduction to OOP” example as simple as possible.&lt;/p&gt;  &lt;h2&gt;Code Maker Player&lt;/h2&gt;  &lt;p&gt;This is the player that defines the pattern that is the correct answer. One of the nice things about playing MasterMind as a computer game is that you don’t need this player. The game itself can select a valid correct answer.&lt;/p&gt;  &lt;h2&gt;Code Breaker Player&lt;/h2&gt;  &lt;p&gt;This is the player that is guessing the pattern and is the user of your game. You could track the name of the player and their best score. In “phase 1” of this simple game, the decision was made not to implement a player class. This can be implemented in “phase 2”. For now, every player is a guest and no score is retained after a game is complete.&lt;/p&gt;  &lt;h2&gt;Answer Pattern&lt;/h2&gt;  &lt;p&gt;The pattern of pegs that defines the answer does not really need to perform any actions. It is more a property of a particular game.&lt;/p&gt;  &lt;h2&gt;Guess Peg&lt;/h2&gt;  &lt;p&gt;As the game progresses, the user puts pegs into the board as a guess. Pegs are a key part of the game, so I selected to include a “Peg” class.&lt;/p&gt;  &lt;h2&gt;Key (Feedback) Peg&lt;/h2&gt;  &lt;p&gt;With a board game, it is difficult to draw onto each guess peg to define whether it is correct or incorrect and still reuse the pegs in a new game. Instead, in the real-world game pegs are added to the sides to depict which are correct or incorrect. This is not necessary in the computer game because each guess peg can be marked as correct or incorrect.&lt;/p&gt;  &lt;h1&gt;Defining the Properties and Methods&lt;/h1&gt;  &lt;p&gt;The next step is to define what data that each class retains (called &lt;strong&gt;properties&lt;/strong&gt; in OOP) and what functionality that the class provides (called &lt;strong&gt;methods&lt;/strong&gt; in OOP).&lt;/p&gt;  &lt;p&gt;After reviewing the nouns, only two classes were defined:&lt;/p&gt;  &lt;h2&gt;MasterMind&lt;/h2&gt;  &lt;p&gt;This class manages the basic game including the game board and game play. Because this game is on the computer, there is no need for the user to “pick up” a peg of the desired color and place it into a hole in the board. Rather, the board is a set of “blank” pegs. The user can select to color each peg in a row to form a guess. The correct pegs are left in tact. The incorrect pegs are x’ed out.&lt;/p&gt;  &lt;p&gt;The MasterMind class needs properties such as:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Board: The board property defines the set of pegs that make up the game board. The user can then set the color of each peg in a row to form a guess. &lt;/li&gt;    &lt;li&gt;NumberOfRows: The number of rows to display in the game. &lt;/li&gt;    &lt;li&gt;NumberOfHoles: The number of holes per row to display in the game. &lt;/li&gt;    &lt;li&gt;GameColors: The set of colors used by the game. &lt;/li&gt;    &lt;li&gt;Answer: The list of colors that form the answer. &lt;/li&gt;    &lt;li&gt;CurrentTurn: Keeps track of the number of turns required to find the answer. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The basic set of methods for the MasterMind class include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;CreateBoard: Creates the internal board structure based on the number of rows and number of holes per row. NOTE: Since good coding practice dictates separating our “business objects” from our UI, this code does not &lt;em&gt;draw&lt;/em&gt; the board. Rather, it creates the internal data structures for managing the board. &lt;/li&gt;    &lt;li&gt;CreateAnswer: Uses the Random .NET features to randomly pick a set of colors that form the correct answer. &lt;/li&gt;    &lt;li&gt;ProcessGuess: Checks the colors that the user defined for pegs in a particular row and x’s out the incorrect pegs. &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Peg&lt;/h2&gt;  &lt;p&gt;The Peg class manages a particular hole in the game board. The Peg class needs properties such as:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Column: Column on the game board. &lt;/li&gt;    &lt;li&gt;Row: Row on the game board. &lt;/li&gt;    &lt;li&gt;PegColor: Color of the peg as defined by the user. &lt;/li&gt;    &lt;li&gt;Correct: Whether or not the peg color is correct. &lt;/li&gt; &lt;/ul&gt;  &lt;h1&gt;Using the Classes&lt;/h1&gt;  &lt;p&gt;The user interface portion of the application creates an instance of the MasterMind class and calls its properties and methods as needed. The UI has the code that draws the game board using the MasterMind class and Peg class properties.&lt;/p&gt;  &lt;p&gt;Download the sample code (that is currently only in VB) from &lt;a href="http://www.insteptech.com/techLibrary/samplecode.htm" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698467" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Generics: Building a List of Customers</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx</link><pubDate>Fri, 03 Jul 2009 15:23:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697709</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1697709</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx#comments</comments><description>&lt;p&gt;Often times applications require lists of things: lists of customers, lists of experiments, lists of accounts and so on. The generic lists provided in .NET 2.0 made working with these lists easy. And the list initializers in C# and the object initializers in VB make lists easier still.&lt;/p&gt;  &lt;p&gt;As an example, here is a first draft of a simple Customer class.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public class Customer      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int CustomerId { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string EmailAddress { get; set; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The above code takes advantage of automatically implemented properties. VB does not yet have this feature, but it is expected with VB 10.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB 9 (VS 2008):&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Class Customer&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _CustomerId As Integer      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property CustomerId() As Integer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _CustomerId       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As Integer)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _CustomerId = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _FirstName As String      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property FirstName() As String       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _FirstName       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As String)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _FirstName = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _LastName As String&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property LastName() As String       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _LastName       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As String)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _LastName = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Private _EmailAddress As String      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Public Property EmailAddress () As String       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _EmailAddress       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As String)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _EmailAddress = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Property       &lt;br /&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Most often, you would read the customers from a database and build the list. But there are times where you may want to build the list manually. For example, for prototyping or testing.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;With C#, you can build a list of customers in one line of code:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;List&amp;lt;Customer&amp;gt; custList = new List&amp;lt;Customer&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 1,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Bilbo&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Baggins&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;bb@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 2,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Frodo&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Baggins&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;fb@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 3,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Samwise&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Gamgee&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;sg@hob.me&amp;quot;},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { CustomerId = 4,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName=&amp;quot;Rosie&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastName = &amp;quot;Cotton&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EmailAddress = &amp;quot;&lt;a href="mailto:rc@hob.me"&gt;rc@hob.me&lt;/a&gt;&amp;quot;}};&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The above example takes advantage of the list initializers feature of C#. VB does not yet have this feature, but it is expected in VB10.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB 9 (VS 2008):&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt; The code to do this in VB takes advantage of object initializers:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim custList As New List(Of Customer)      &lt;br /&gt;custList.Add(New Customer With {.CustomerId = 1, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Baggins&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Bilbo&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress=&amp;quot;&lt;/font&gt;&lt;a href="mailto:bb@hob.me"&gt;&lt;font color="#65402e" face="Consolas"&gt;bb@hob.me&lt;/font&gt;&lt;/a&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;quot;})      &lt;br /&gt;custList.Add(New Customer With {.CustomerId = 2, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Baggins&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Frodo&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;&lt;/font&gt;&lt;a href="mailto:fb@hob.me"&gt;&lt;font color="#65402e" face="Consolas"&gt;fb@hob.me&lt;/font&gt;&lt;/a&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;quot;})      &lt;br /&gt;custList.Add(New Customer With {.CustomerId = 3, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Gamgee&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Samwise&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;&lt;/font&gt;&lt;a href="mailto:sg@hob.me"&gt;&lt;font color="#65402e" face="Consolas"&gt;sg@hob.me&lt;/font&gt;&lt;/a&gt;&amp;quot;&lt;font color="#65402e" face="Consolas"&gt;})      &lt;br /&gt;custList.Add(New Customer With {.CustomerId = 4, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .LastName = &amp;quot;Cotton&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FirstName = &amp;quot;Rosie&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .EmailAddress = &amp;quot;&lt;/font&gt;&lt;a href="mailto:rc@hob.me"&gt;&lt;font color="#65402e" face="Consolas"&gt;rc@hob.me&lt;/font&gt;&lt;/a&gt;&amp;quot;&lt;font color="#65402e" face="Consolas"&gt;})&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Notice the With statement in this VB code.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;EDIT 4/29/10:&lt;/strong&gt; This syntax is much easier now in VB 10 (VS 2010). See &lt;a href="http://msmvps.com/blogs/deborahk/archive/2010/04/11/auto-implemented-properties.aspx"&gt;this link&lt;/a&gt; for more information on auto-implemented properties in VB 10. See &lt;a href="http://msmvps.com/blogs/deborahk/archive/2010/04/29/collection-initializers.aspx"&gt;this link&lt;/a&gt; for more information on collection initializers in VB 10.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697709" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/OOP/default.aspx">OOP</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Generics/default.aspx">Generics</category></item></channel></rss>