<?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>Search results for 'app:weblogs' matching tag 'CSharp'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=CSharp&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'CSharp'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Unit Testing: Execution</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/25/unit-testing-execution.aspx</link><pubDate>Sun, 25 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1735158</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;Visual Studio 2008 (Professional Edition and above) provides a really nice set of tools for development and execution of unit tests.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of unit testing, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/25/unit-testing-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/25/unit-testing-an-introduction.aspx"&gt;This prior post&lt;/a&gt; demonstrates how to build a unit test using the &amp;quot;Create Unit Tests...&amp;quot; feature of the Code Editor. This post demonstrates how to execute a unit test.&lt;/p&gt;  &lt;p&gt;NOTE: This post assumes you have already generated or created at least one unit test.&lt;/p&gt;  &lt;p&gt;To execute one or more unit tests:&lt;/p&gt;  &lt;p&gt;1) Open your solution in Visual Studio.&lt;/p&gt;  &lt;p&gt;The solution should include both the project(s) to test and the test project(s).&lt;/p&gt;  &lt;p&gt;2) Select Test | Windows | Test View from the menu to view the Test View window. This window contains every method marked with the TestMethod attribute.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/2620.image_5F00_27D6A4F6.png"&gt;&lt;img style="border-right-width:0px;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/6518.image_5F00_thumb_5F00_40561C79.png" width="457" height="247" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The solution used for the screenshot above has two LastNameTest unit tests: One for the VB example and one for the C# example. If you coded along&amp;#160; from the prior unit testing post, you will only have one for whichever language you selected. If you have been creating unit tests for one of your projects, you may have hundreds of tests in this list.&lt;/p&gt;  &lt;p&gt;To execute unit tests:&lt;/p&gt;  &lt;p&gt;1) Select one or&amp;#160; more tests from the list of tests in the Test View window.&lt;/p&gt;  &lt;p&gt;You can add columns and sort the list or use the filter feature to make it easier to locate and select the desired tests to execute.&lt;/p&gt;  &lt;p&gt;2) Click the Run Selection button in the upper left&amp;#160; corner of the Test View window or right-click on any selected test and select &amp;quot;Run Selection&amp;quot; from the context menu.&lt;/p&gt;  &lt;p&gt;The selected test(s) will then execute, displaying their status in the Test Results window:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/6180.image_5F00_543F8942.png"&gt;&lt;img style="border-right-width:0px;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/4428.image_5F00_thumb_5F00_0A145890.png" width="505" height="142" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;When the test is complete, the Test Results window will look something like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/2055.image_5F00_1967BA9F.png"&gt;&lt;img style="border-right-width:0px;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/0777.image_5F00_thumb_5F00_75EB1639.png" width="499" height="128" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Both of&amp;#160; the tests shown in the above screenshot are marked as Inconclusive because the generated unit testing code used the Assert class Inconclusive method. The generated unit testing template is designed to prevent a false positive. It generates an inconclusive result until you update the unit test with correct valid and invalid values and remove the Inconclusive method call.&lt;/p&gt;  &lt;p&gt;To see more information on the result of the test, double-click on a test result.&lt;/p&gt;  &lt;p&gt;To run the test again, use the Run or Debug buttons at the top of the Test Results window.&lt;/p&gt;  &lt;p&gt;If any of&amp;#160; the tests don&amp;#39;t pass, they are marked as Failed in the Test Results window as shown below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/2475.image_5F00_57047C8E.png"&gt;&lt;img style="border-right-width:0px;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/5684.image_5F00_thumb_5F00_6A719095.png" width="499" height="138" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Double click on any of&amp;#160; the failed tests to view the test results:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/1033.image_5F00_085F78BD.png"&gt;&lt;img style="border-right-width:0px;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/2502.image_5F00_thumb_5F00_1A9819B2.png" width="475" height="389" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The Error Stack Trace at the bottom of this window gives you further information on the source of the failure. Click on any link in the stack trace to jump to the associated location in the code.&lt;/p&gt;  &lt;p&gt;You can also debug the code as the test is executing if you &amp;quot;Debug Selection&amp;quot; option instead of the &amp;quot;Run Selection&amp;quot; option.&lt;/p&gt;  &lt;p&gt;After you update the unit tests with valid code and the tests pass, the Test Results will appear as follows:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/3755.image_5F00_617116E2.png"&gt;&lt;img style="border-right-width:0px;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/6215.image_5F00_thumb_5F00_11B7EB99.png" width="473" height="131" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice the color-coded green passing indicator.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Unit Testing: An Introduction</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/25/unit-testing-an-introduction.aspx</link><pubDate>Sun, 25 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1735139</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;If you have Visual Studio 2008 or later and have the Professional Edition or better (NOT the Express Editions), you have some very nice unit testing tools within your Visual Studio environment. These tools help you write, execute, and track your unit tests and code coverage. This post provides an introduction to using these tools.&lt;/p&gt;  &lt;p&gt;If you are new to unit testing, the idea is to test the smallest possible units of your code. In most cases, the smallest units of code are the property procedures and methods of your classes.&lt;/p&gt;  &lt;p&gt;For more general information about the purpose of unit testing, see &lt;a href="http://en.wikipedia.org/wiki/Unit_testing"&gt;this link&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;NOTE: Unit testing is not meant to replace integration testing, system testing, or user testing.&lt;/p&gt;  &lt;p&gt;In some development methodologies, such as Test Driven Development (TDD), unit tests are written &lt;em&gt;before&lt;/em&gt; the code is written. You capture the basic requirements in the unit test and then write the code. The code is complete when the unit tests&amp;#160; pass. Visual Studio 2010 has features to assist with this &amp;quot;unit test first&amp;quot; approach.&lt;/p&gt;  &lt;p&gt;But&amp;#160; this introduction demonstrates how to write unit tests for &lt;em&gt;existing&lt;/em&gt; code. This technique is helpful if you follow a code-first approach or if you received code from another source or have old code that did not originally have unit tests.&lt;/p&gt;  &lt;h2&gt;Build the Code&lt;/h2&gt;  &lt;p&gt;The business object tested in this post is a simplified Customer class that uses the business object base class defined in &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx"&gt;this post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The Customer class is shown below.&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 Customer: BoBase      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private int? _CustomerId;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int? CustomerId       &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 _CustomerId; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; internal 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 (_CustomerId == null || !_CustomerId.Equals(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;&amp;#160;&amp;#160; string propertyName = &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; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;// Perform any validation here      &lt;br /&gt;&lt;/font&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; if (!_CustomerId.Equals(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;&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; _CustomerId = 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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SetEntityState(EntityStateType.Modified,       &lt;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;&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;&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; }       &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; 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;&amp;#160;&amp;#160; string propertyName = &amp;quot;LastName&amp;quot;;&amp;#160; &lt;br /&gt;&lt;/font&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; // Perform any validation here&amp;#160; &lt;br /&gt;&lt;/font&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; if (_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;&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; _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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SetEntityState(EntityStateType.Modified,       &lt;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;&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;&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; }       &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; private string _FirstName;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string FirstName       &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 _FirstName; }       &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 (_FirstName == null || _FirstName != 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;&amp;#160;&amp;#160; string propertyName = &amp;quot;FirstName&amp;quot;;&amp;#160; &lt;br /&gt;&lt;/font&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; // Perform any validation here&amp;#160; &lt;br /&gt;&lt;/font&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; if (_FirstName != 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;&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; _FirstName = 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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SetEntityState(EntityStateType.Modified,       &lt;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;&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;&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; }       &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; private string _EmailAddress;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string EmailAddress       &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 _EmailAddress; }       &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 (_EmailAddress == null || _EmailAddress != 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;&amp;#160;&amp;#160; string propertyName = &amp;quot;EmailAddress&amp;quot;;&amp;#160; &lt;br /&gt;&lt;/font&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; // Perform any validation here&amp;#160; &lt;br /&gt;&lt;/font&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; if (_EmailAddress != 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;&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; _EmailAddress = 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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SetEntityState(EntityStateType.Modified,       &lt;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;&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;&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; }       &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 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 _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; Friend 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; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;If _CustomerId Is Nothing OrElse _      &lt;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; Not _CustomerId.Equals(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;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;#39; Perform any validation&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; If Not _CustomerId.Equals(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;&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;&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;&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; End If       &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&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; If _FirstName Is Nothing OrElse _       &lt;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 IsNot 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;&amp;#160;&amp;#160; Dim propertyName As String = &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;#39; Perform any validation       &lt;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; If _FirstName IsNot 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;&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;&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, _       &lt;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;&amp;#160;&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;&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; 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; 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 _       &lt;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; _LastName IsNot 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;&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;&amp;#160;&amp;#160; &amp;#39; Perform any validation       &lt;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; If _LastName IsNot 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;&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;&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, _       &lt;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;&amp;#160;&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;&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; 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; 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; If _EmailAddress Is Nothing OrElse _       &lt;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; _EmailAddress IsNot 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;&amp;#160;&amp;#160; Dim propertyName As String = &amp;quot;EmailAddress&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;#39; Perform any validation       &lt;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; If _EmailAddress IsNot 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;&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;&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, _       &lt;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;&amp;#160;&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;&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; 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;br /&gt;End Class&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The first if statement in each of the property procedures for the Customer class checks whether the current value is null or was changed. If the property is changed, you want to revalidate it, set it as modified, and generate a PropertyChanged event (which is handled in the SetEntityState method).&lt;/p&gt;  &lt;p&gt;NOTE: C# uses a != operator to judge whether the value was changed. VB cannot use the &amp;lt;&amp;gt; operator because VB propagates null values. So if either value is null (nothing) the result is false. (See &lt;a href="http://msdn.microsoft.com/en-us/library/0x9tb07z.aspx"&gt;this msdn entry&lt;/a&gt; for more information.) To prevent this problem, IsNot is used in the VB code to determine if the value was changed.&lt;/p&gt;  &lt;p&gt;The null check allows for possible validation of required fields (that is, fields that cannot be null or empty). If you know you will never need a null validation check, you can leave the null check off the first if statement.&lt;/p&gt;  &lt;p&gt;The propertyName variable defines a name&amp;#160; that you can use in any validation error messages and it is the name used in the PropertyChanged event.&lt;/p&gt;  &lt;p&gt;You can then perform any necessary validation. In this simple example, no validation was added. If you want to see an example of some validation, check out &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx"&gt;this post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Finally, if the value was changed, the backing variable is set to the new value and the SetEntityState method is called&amp;#160; to mark the business object as&amp;#160; &amp;quot;dirty&amp;quot; and to generate the PropertyChanged event.&lt;/p&gt;  &lt;h2&gt;Define the Testing Scenarios&lt;/h2&gt;  &lt;p&gt;So let&amp;#39;s start with the test of the LastName property. Looking at the requirements, the following testing scenarios are required:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Initial null value; set to null value (should perform validation but not set the dirty flag) &lt;/li&gt;    &lt;li&gt;Initial null value; set to valid&amp;#160; string (should perform validation and set the dirty flag) &lt;/li&gt;    &lt;li&gt;Initial null value; set to empty string (should perform validation and set the dirty flag) &lt;/li&gt;    &lt;li&gt;Initial string value; set to null value (should perform validation and set the dirty flag) &lt;/li&gt;    &lt;li&gt;Initial string value, set to different string value (should perform validation and set the dirty flag) &lt;/li&gt;    &lt;li&gt;Initial string value, set&amp;#160; to same string value (should not&amp;#160; perform validation and not set the dirty flag) &lt;/li&gt;    &lt;li&gt;Initial string value, set&amp;#160; to empty value (should perform validation and set the dirty flag) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;And if you have validation code, you will have more scenarios to test valid and invalid values. But this is enough to give you the general idea.&lt;/p&gt;  &lt;p&gt;So now let&amp;#39;s generate the unit test for the LastName property using the tools provided in Visual Studio (Professional Edition or above).&lt;/p&gt;  &lt;h2&gt;Generate the Unit Test&lt;/h2&gt;  &lt;p&gt;The following are the steps for generating the unit test for a particular property procedure or method:&lt;/p&gt;  &lt;p&gt;1) Open the Code Editor for the code you want to test.&lt;/p&gt;  &lt;p&gt;2) Right-click and select Create Unit Tests... from the context menu.&lt;/p&gt;  &lt;p&gt;The following dialog will appear:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/7713.image_5F00_52563B2F.png"&gt;&lt;img style="border-right-width:0px;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/6646.image_5F00_thumb_5F00_77DFB890.png" width="430" height="318" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;3) Select the properties, methods, or&amp;#160; constructors you wish to test.&lt;/p&gt;  &lt;p&gt;If you were in a specific property procedure or method, that procedure or method is automatically checked in this dialog. You can select to generate tests for any properties, methods,&amp;#160; or constructors using this dialog.&lt;/p&gt;  &lt;p&gt;Notice the Output project combobox on this dialog. Using this combobox, you can create a new C# unit test, new VB unit test, or select any existing unit test project if you already&amp;#160; have some in your solution. This allows you to add unit tests to existing unit testing projects at any point in the development process.&lt;/p&gt;  &lt;p&gt;4) Click OK.&lt;/p&gt;  &lt;p&gt;5) If you are creating a new C# or VB project, enter the project name and click Create.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/0385.image_5F00_4F60D67C.png"&gt;&lt;img style="border-right-width:0px;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/4274.image_5F00_thumb_5F00_2E6D63D5.png" width="416" height="136" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;6) The test project is then added to your Solution Explorer.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/5734.image_5F00_0D79F12E.png"&gt;&lt;img style="border-right-width:0px;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/6327.image_5F00_thumb_5F00_3E992BCE.png" width="408" height="327" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If you created a VB test project, the result is the same, but with a CustomerTest.vb file&amp;#160; within the BoTest project.&lt;/p&gt;  &lt;h2&gt;View the Generated Code&lt;/h2&gt;  &lt;p&gt;Visual Studio automatically creates an first cut of your unit testing code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Double-click on&amp;#160; CustomerTest.cs to view the code.&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;/// &amp;lt;summary&amp;gt;      &lt;br /&gt;///A test for LastName       &lt;br /&gt;///&amp;lt;/summary&amp;gt;       &lt;br /&gt;[TestMethod()]       &lt;br /&gt;public void LastNameTest()       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Customer target = new Customer(); // TODO: Initialize to an appropriate value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string expected = string.Empty; // TODO: Initialize to an appropriate value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string actual;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; target.LastName = expected;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; actual = target.LastName;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.AreEqual(expected, actual);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.Inconclusive(&amp;quot;Verify the correctness of this test method.&amp;quot;);       &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;Double-click on CustomerTest.vb to view the code.&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;A test for LastName       &lt;br /&gt;&amp;#39;&amp;#39;&amp;#39;&amp;lt;/summary&amp;gt;       &lt;br /&gt;&amp;lt;TestMethod()&amp;gt; _       &lt;br /&gt;Public Sub LastNameTest()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim target As Customer = New Customer &amp;#39; TODO: Initialize to an appropriate value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim expected As String = String.Empty &amp;#39; TODO: Initialize to an appropriate value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim actual As String       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; target.LastName = expected       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; actual = target.LastName       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.AreEqual(expected, actual)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.Inconclusive(&amp;quot;Verify the correctness of this test method.&amp;quot;)       &lt;br /&gt;End Sub&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The TestMethod attribute defines this method as a unit test. It is then picked up by the other unit testing tools within Visual Studio.&lt;/p&gt;  &lt;p&gt;The generated code includes some ToDo notes, defining where you should change the code.&lt;/p&gt;  &lt;p&gt;The Assert class is a key aspect of your unit tests. It provides many methods that you can use to assert whether your test produced expected results.&lt;/p&gt;  &lt;p&gt;The generated code uses two of the Assert class methods. The &lt;strong&gt;AreEqual&lt;/strong&gt; method determines whether the expected and actual values are equal. If not, it fails the test.&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Inconclusive&lt;/strong&gt; method always causes the test to fail. This is added to all generated code to ensure that the test will fail until you modify the test with valid values.&lt;/p&gt;  &lt;p&gt;At this point, you can try to execute this unit test, or update it first and then execute it.&lt;/p&gt;  &lt;p&gt;See the following posts for more information:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/25/unit-testing-execution.aspx"&gt;Unit Testing: Execution&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/25/unit-testing-code-coverage.aspx"&gt;Unit Testing: Code Coverage&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Unit Testing: Testing Properties &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Populating a DataGridView from Xml Data</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/20/populating-a-datagridview-from-xml-data.aspx</link><pubDate>Tue, 20 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1733866</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;If you are using XML in a WinForms application you may find the need to display the XML data in a DataGridView. &lt;/p&gt;  &lt;p&gt;Let&amp;#39;s take this XML:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;lt;states&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;state name=&amp;quot;California&amp;quot;&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;abbreviation&amp;gt;CA&amp;lt;/abbreviation&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;year&amp;gt;1850&amp;lt;/year&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;governor&amp;gt;Schwarzenegger&amp;lt;/governor&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/state&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;state name=&amp;quot;Wisconsin&amp;quot;&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;abbreviation&amp;gt;WI&amp;lt;/abbreviation&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;year&amp;gt;1848&amp;lt;/year&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;governor&amp;gt;Doyle&amp;lt;/governor&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/state&amp;gt;       &lt;br /&gt;&amp;lt;/states&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Displaying XML in a DataGridView sounds easy, but if you just set the DataGridView DataSource to the XML data, you will get something like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/6013.image_5F00_044D9E51.png"&gt;&lt;img style="border-right-width:0px;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/0552.image_5F00_thumb_5F00_62EDF8B4.png" width="469" height="184" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice how it has lots of yuck in it: attribute details, node properties, and so on for every node in the XML. So how do you get something more like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/5758.image_5F00_3ADB4995.png"&gt;&lt;img style="border-right-width:0px;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/5736.image_5F00_thumb_5F00_64DB47BD.png" width="463" height="203" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The trick is to use anonymous types.&lt;/p&gt;  &lt;p&gt;The code is provided here in VB and C# and then described in detail below.&lt;/p&gt;  &lt;p&gt;NOTE: Be sure to set a reference to &lt;strong&gt;System.Core&lt;/strong&gt; and &lt;strong&gt;System.Xml.Linq&lt;/strong&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;XElement statesXml = XElement.Parse(&amp;quot;&amp;lt;states&amp;gt;&amp;quot; +      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;state name=&amp;#39;California&amp;#39;&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;abbreviation&amp;gt;CA&amp;lt;/abbreviation&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;year&amp;gt;1850&amp;lt;/year&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;governor&amp;gt;Schwarzenegger&amp;lt;/governor&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;/state&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;state name=&amp;#39;Wisconsin&amp;#39;&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;abbreviation&amp;gt;WI&amp;lt;/abbreviation&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;year&amp;gt;1848&amp;lt;/year&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;governor&amp;gt;Doyle&amp;lt;/governor&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&amp;lt;/state&amp;gt;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160; &amp;quot;&amp;lt;/states&amp;gt;&amp;quot;); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;var query = from st in statesXml.Descendants(&amp;quot;state&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; select new       &lt;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;&amp;#160;&amp;#160; Name = st.Attribute(&amp;quot;name&amp;quot;).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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Abbrev = st.Element(&amp;quot;abbreviation&amp;quot;).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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Year = st.Element(&amp;quot;year&amp;quot;).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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Governor = st.Element(&amp;quot;governor&amp;quot;).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;DataGridView1.DataSource = query.ToList();&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 statesXml As XElement = _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;states&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;state name=&amp;quot;California&amp;quot;&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;lt;abbreviation&amp;gt;CA&amp;lt;/abbreviation&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;lt;year&amp;gt;1850&amp;lt;/year&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;lt;governor&amp;gt;Schwarzenegger&amp;lt;/governor&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/state&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;state name=&amp;quot;Wisconsin&amp;quot;&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;lt;abbreviation&amp;gt;WI&amp;lt;/abbreviation&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;lt;year&amp;gt;1848&amp;lt;/year&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;lt;governor&amp;gt;Doyle&amp;lt;/governor&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/state&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/states&amp;gt; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim query = From st In statesXml...&amp;lt;state&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; Select New 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; .Name = st.@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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Abbrev = st.&amp;lt;abbreviation&amp;gt;.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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Year = st.&amp;lt;year&amp;gt;.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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Governor = st.&amp;lt;governor&amp;gt;.Value}       &lt;br /&gt;DataGridView1.DataSource = query.ToList&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The first part of this code builds the XML. The C# code uses the XElement.Parse function to build the XML; VB uses XML literals. This part of the code is not necessary if you are reading the XML from another source, such as a file.&lt;/p&gt;  &lt;p&gt;The second part of the code leverages Linq to XML to process the set of state XML elements. For each element, it uses the Select New syntax to create an anonymous type. The syntax defines an unnamed (anonymous) type with properties Name, Abbrev, Year, and Governor.&lt;/p&gt;  &lt;p&gt;[To view an overview of anonymous types, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/19/anonymous-types-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The last line converts the results of the query to a generic list and assigns it to the DataSource property of the DataGridView. Visual Studio uses the anonymous type property names as the text for the column titles and populates the rows with each state element.&lt;/p&gt;  &lt;p&gt;Use this technique any time you have XML that you want to display in a DataGridView.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Execution</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/16/lambda-expressions-execution.aspx</link><pubDate>Fri, 16 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1732784</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;Lambda expressions can be assigned to a delegate variable. The lambda expression is then executed when the delegate is called.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;In the following example, a lambda expression is assigned to a variable (f).&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;Func&amp;lt;int,string&amp;gt; f = &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;x =&amp;gt; (x + x).ToString();      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(5));      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(10));&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 f = Function(x as Integer) &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;(x + x).ToString()      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(5))      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(10))&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example is a Func&amp;lt;int, string&amp;gt; [Func(Of Integer, String) in VB] meaning it takes one integer input parameter and returns a string. The lambda expression simply takes the input value, adds it to itself, and returns the result as a string.&lt;/p&gt;  &lt;p&gt;[For more information on Func delegates, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-func-delegates.aspx"&gt;this post&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;When the expression is defined, it is &lt;em&gt;not&lt;/em&gt; executed. It is not executed until it is called.&lt;/p&gt;  &lt;p&gt;The first Debug.WriteLine statement calls the lambda expression, passing in a 5. The result displays &amp;quot;10&amp;quot; in the debug window.&lt;/p&gt;  &lt;p&gt;The second Debug.WriteLine statement calls the lambda expression again, passing in a 10. The result displays &amp;quot;20&amp;quot; in the debug window.&lt;/p&gt;  &lt;p&gt;Lambda expressions are executed when they are called, not when they are constructed. This is important to consider, especially when the lambda expression contains local variables. &lt;/p&gt;  &lt;p&gt;Local variables used in a lambda expression are “captured” or “lifted”. The variable value used is the value at &lt;em&gt;execution&lt;/em&gt; time. The variable lifetime extends to the lifetime of the delegate.&lt;/p&gt;  &lt;p&gt;The following code updates the original example to use a local variable.&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;int y = 0;      &lt;br /&gt;Func&amp;lt;int,string&amp;gt; f = x =&amp;gt; (x + y).ToString();       &lt;br /&gt;y = 10;       &lt;br /&gt;Debug.WriteLine(f(5));&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 y As Integer = 0      &lt;br /&gt;Dim f = Function(x) (x + y).ToString()       &lt;br /&gt;y = 10       &lt;br /&gt;Debug.WriteLine(f(5))&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example takes the input value, adds it to the current value of the local variable (y), and returns the result as a string.&lt;/p&gt;  &lt;p&gt;The Debug.WriteLine statement in this example calls the lambda expression, passing in a 5.&amp;#160; At the point of executing the Debug statement, y is 10, so the result displays &amp;quot;15&amp;quot; in the debug window.&lt;/p&gt;  &lt;p&gt;For another example of using lambda expressions with local variables, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/02/lambda-expressions-finding-differences-in-two-lists.aspx"&gt;this post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>WinForms User Controls 101</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/13/winforms-user-controls-101.aspx</link><pubDate>Tue, 13 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1732168</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;The same several questions often come up in the forums regarding the basics of building a user control with VB.NET or C#. The goal of this post is to answer those questions.&lt;/p&gt;  &lt;p&gt;There are three basic types of WinForms user controls that you can create:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Extended control &lt;/li&gt;    &lt;li&gt;Composite control &lt;/li&gt;    &lt;li&gt;Custom control &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Each of these are discussed below.&lt;/p&gt;  &lt;h2&gt;Extended Control&lt;/h2&gt;  &lt;p&gt;Use an extended control whenever you want to extend the behavior of &lt;strong&gt;one&lt;/strong&gt; particular control. Say for example that you want to build your own TextBox that only allows alphabetic characters. Or you want to build your own Button that has specific behaviors.&lt;/p&gt;  &lt;p&gt;The best thing about extended controls is that you automatically get all of the intrinsic behavior of the original control. All you need to do is add any extended functionality that you want for the control.&lt;/p&gt;  &lt;p&gt;To create this type of user control, follow these steps.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on the project and select Add | User Control. &lt;/li&gt;    &lt;li&gt;Name the user control and click Add. &lt;/li&gt;    &lt;li&gt;Do &lt;strong&gt;NOT&lt;/strong&gt; put anything on the design surface of the user control. &lt;/li&gt;    &lt;li&gt;Open the code behind file (UserControl1.cs). &lt;/li&gt;    &lt;li&gt;Modify the code to inherit from the control you are extending. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public partial class UserControl1 : TextBox&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This example extends a TextBox control. Replace TextBox above with whatever WinForm control you want to extend.&lt;/p&gt;  &lt;p&gt;NOTE: After changing the Inherits statement, you may get an error on a line that begins with this.AutoScaleMode. If so, just delete that line. It is no longer needed now that you are inheriting from a control other than UserControl.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on the project and select Add | User Control. &lt;/li&gt;    &lt;li&gt;Name the user control and click Add. &lt;/li&gt;    &lt;li&gt;Do &lt;strong&gt;NOT&lt;/strong&gt; put anything on the design surface of the user control. &lt;/li&gt;    &lt;li&gt;Select the project in the Solution Explorer and click the Show All Files button in the Solution Explorer toolbar. &lt;/li&gt;    &lt;li&gt;Click on the plus sign to the left of the user control in Solution Explorer to view the nodes below it. &lt;/li&gt;    &lt;li&gt;Double-click on the designer file to open it. (UserControl1.Designer.vb) &lt;/li&gt;    &lt;li&gt;Modify the code to inherit from the control you are extending. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Partial Class UserControl1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Inherits TextBox&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This example extends a TextBox control. Replace TextBox above with whatever WinForm control you want to extend.&lt;/p&gt;  &lt;p&gt;NOTE: After changing the Inherits statement, you may get an error on a line that begins with Me.AutoScaleMode. If so, just delete that line. It is no longer needed now that you are inheriting from a control other than UserControl.&lt;/p&gt;  &lt;p&gt;Then add any additional behavior or functionality that you want in the extended control.&lt;/p&gt;  &lt;p&gt;The MSDN documentation provides an example of building an extended control using &lt;a href="http://msdn.microsoft.com/en-us/library/w2a8y03d.aspx" target="_blank"&gt;VB.NET here&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/5h0k2e6x.aspx" target="_blank"&gt;C# here&lt;/a&gt;.&lt;/p&gt;  &lt;h2&gt;Composite Control&lt;/h2&gt;  &lt;p&gt;A composite controls is so named because it is composed of multiple controls. Use this type of user control any time you want to build a control that combines a set of other controls. For example, you want to build a search control that contains a Textbox for entry of the text to search, Button to click to perform the search, and ListBox for the results of the search.&lt;/p&gt;  &lt;p&gt;The best thing about composite controls is that you can easily add any controls to the composite control using the design surface. &lt;/p&gt;  &lt;p&gt;However, because it is a composite control that inherits from UserControl, no properties, methods, or events from any of the underlying controls are exposed. This means that when you use the user control, you cannot set the Text property of the TextBox or respond to Button click events without writing some code.&lt;/p&gt;  &lt;p&gt;You basically have to write code to expose any property, method, or event that you need.&lt;/p&gt;  &lt;p&gt;To create this type of user control, follow these steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on the project and select Add | User Control. &lt;/li&gt;    &lt;li&gt;Name the user control and click Add. &lt;/li&gt;    &lt;li&gt;Put the controls you want onto the design surface. &lt;/li&gt;    &lt;li&gt;Expose any properties, methods, or events that you need. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;An example of generating events from a user control is provided in &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/22/user-control-events-in-vb-and-c.aspx" target="_blank"&gt;this post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The MSDN documentation provides an example of building a composite control using &lt;a href="http://msdn.microsoft.com/en-us/library/c316f119.aspx" target="_blank"&gt;VB.NET here&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/a6h7e207.aspx" target="_blank"&gt;C# here&lt;/a&gt;.&lt;/p&gt;  &lt;h2&gt;Custom Control&lt;/h2&gt;  &lt;p&gt;A custom control is a control you create from the ground up including all control drawing and any unique behavior.&lt;/p&gt;  &lt;p&gt;To build a custom control, inherit from Control to get the basic behavior of a WinForms control. Then use the OnPaint event to render your custom user interface.&lt;/p&gt;  &lt;p&gt;The MSDN documentation provides an example of building a simple custom control &lt;a href="http://msdn.microsoft.com/en-us/library/649xahhe.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731685</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;A delegate is an object that holds a reference to a method with a particular parameter list and return type.&lt;/p&gt;  &lt;p&gt;It is basically like an object-oriented, type-safe function pointer.&lt;/p&gt;  &lt;p&gt;Delegates basically make it possible to treat methods as entities. You can then assign a delegate to a variable or pass it as a parameter. &lt;/p&gt;  &lt;p&gt;The classic delegate example uses events.&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;HelloButton.Click += new EventHandler(HelloButton_Click);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Or the short-cut version of this code:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;HelloButton.Click += HelloButton_Click;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This passes the HelloButton_Click method to a new EventHandler delegate.&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;AddHandler HelloButton.Click, AddressOf HelloButton_Click&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB, the AddressOf assigns the address of the HelloButton_Click method to the delegate.&lt;/p&gt;  &lt;p&gt;Instead of using a named method for the delegate (in this example, a HelloButton_Click method), you can use a lambda expression.&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;HelloButton.Click += &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;(s, ev) =&amp;gt;      &lt;br /&gt;&lt;/font&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; MessageBox.Show(&amp;quot;Hello World&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;AddHandler HelloButton.Click, &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Function(s, ev) _      &lt;br /&gt;&lt;/font&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; MessageBox.Show(&amp;quot;Hello World&amp;quot;)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In this case the lambda expression has two parameters: s (sender) and ev (eventArgs). When the Click event is generated on the HelloButton, the code displays the MessageBox.&lt;/p&gt;  &lt;p&gt;You can use a lambda expression (inline function) anywhere a delegate is required. However, if the function requires more than a few lines of code, a named method is the preferred and recommended technique.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Func Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-func-delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731723</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;A Func delegate encapsulates a method that returns a value. It takes up to four parameters (and this number is increased in .NET 4.0) plus the return value.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The following signature is a Func delegate that takes two integer parameters and returns a string.&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;Func&amp;lt;int, int, string&amp;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;Func(Of Integer, Integer, String)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;There are many examples of Func delegates. &lt;/p&gt;  &lt;p&gt;The following code demonstrates the &lt;strong&gt;Sum&lt;/strong&gt; function that sums the sales total for all customers in the list (and assumes that a Customer object has a SalesTotal property).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;var total = custList.Sum(c =&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; c.SalesTotal);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Dim total = custList.Sum(Function(c) _    &lt;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; c.SalesTotal)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Action Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-action-delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731722</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;Just as the name implies, an action delegate encapsulates a method that performs an action and has no return value. It takes up to four parameters (and this number is increased in .NET 4.0).&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;ForEach&lt;/strong&gt; method of the List&amp;lt;T&amp;gt; is an example of an action delegate. It takes an action delegate as a parameter.&lt;/p&gt;  &lt;p&gt;One of the common uses of ForEach is to display all of the elements in a list using one line of code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;custList.ForEach(c =&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; Debug.WriteLine(c.LastName));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;custList.ForEach(AddressOf WriteToDebug)&lt;/p&gt;  &lt;p&gt;NOTE: VB lambda expressions do not currently support action delegates. In the above example, the code uses a named method as the action delegate. VB 10 lambda expressions will support action delegates using the Sub keyword instead of the Function keyword.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Predicate Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-predicate-delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731716</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;According to &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus"&gt;Wikipedia&lt;/a&gt;, a predicate is “a function which returns a Boolean value”.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;A predicate delegate encapsulates a method that evaluates to True or False. It takes a single parameter.&lt;/p&gt;  &lt;p&gt;The Array class Find method is an example of a predicate delegate. It takes an array as a first parameter and a predicate delegate as its second parameter.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;var foundCustomer = Array.Find(custArray, c =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(&amp;quot;K&amp;quot;));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Dim foundCustomer = Array.Find(custArray, Function(c) _    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(&amp;quot;K&amp;quot;))&lt;/p&gt;  &lt;p&gt;This code iterates through the array, checking each entry and evaluating the expression as true or false.&amp;#160; If the expression is false, it continues. If the expression is true, it returns the entry. Basically, this code finds the first customer in the array with a last name that starts with the defined letter.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Syntax</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-syntax.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731696</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;This post covers the syntax for lambda expressions. It uses the customer list defined in &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx"&gt;this prior post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The first example below uses the FirstOrDefault extension method on the Enumerable object to find the first customer that matches the defined expression. The FirstOrDefault method takes a delegate as a parameter and that delegate is defined with a lambda expression.&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;&amp;#39; General syntax:      &lt;br /&gt;Customer foundCustomer =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault((Customer c) =&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; c.CustomerId == 4);)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; With inferred typing:      &lt;br /&gt;var foundCustomer =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault(c =&amp;gt; c.CustomerId == 4);)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example is as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;c =&amp;gt; c.CustomerId == 4&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The code begins with the set of parameters to the lambda expression. In this example, there is one parameter (c). The =&amp;gt; is the “goes to” or lambda operator. The remainder of the code is the expression itself. In this case, checking for the item in the list where CustomerId is 4.&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;&amp;#39; General syntax:      &lt;br /&gt;Dim foundCustomer as Customer = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault(Function(c as 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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerId = 4)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; With inferred typing:      &lt;br /&gt;Dim foundCustomer = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault(Function(c) _       &lt;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; c.CustomerId = 4)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example is as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Function(c) c.CustomerId = 4&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The code begins with the word “Function” along with the set of parameters to the lambda expression. In this example, there is one parameter (c). The remainder of the code is the expression itself. In this case, checking the item in the list where CustomerId is 4.&lt;/p&gt;  &lt;p&gt;In either language, notice the shorter syntax with inferred typing. The compiler recognizes that the list contains customers, so it can infer the type of the parameter and the type of return value.&lt;/p&gt;  &lt;p&gt;You can combine some of the extension methods to form more complex statements.&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;var query = custList.FindAll(c =&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(“K”)).OrderBy(c =&amp;gt; c.FirstName));&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 query = custList.FindAll(Function(c) _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(“K”)). _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;OrderBy(Function(c) c.FirstName))&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This code finds all of the entries that match the first lambda expression, basically all customers with a last name that begins with “K”. The code then performs an OrderBy using the second lambda expression, basically sorting the resulting list by first name.&lt;/p&gt;  &lt;p&gt;The above examples demonstrate single-line lambdas. You can also use multi-line lambdas as shown in the example below.&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;var foundCustomer =&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;custList.FirstOrDefault(c =&amp;gt;      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160; Debug.WriteLine(c.LastName);       &lt;br /&gt;&amp;#160;&amp;#160; if (c.CustomerId == 4)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;       &lt;br /&gt;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;       &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;Not available in VB9, coming in VB10.&lt;/p&gt;  &lt;p&gt;Multi-line lambda expressions require the standard {} syntax to define the statement block. Whereas single-line lambda expressions automatically handle&amp;#160; the return value for you, you have to handle it yourself in multi-line lambda expressions.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item></channel></rss>