<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
    <title>Dominic Zukiewicz</title>
    <link rel="self" type="application/xml" href="http://blogs.interakting.co.uk/dominicz/atom.aspx" />
    <subtitle type="html">ASP.NET 2.0 / C# / WPF</subtitle>
    <id>http://blogs.interakting.co.uk/dominicz/Default.aspx</id>
    <author>
        <name>Dominic</name>
        <uri>http://blogs.interakting.co.uk/dominicz/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 1.9.5.177">Subtext</generator>
    <updated>2008-11-20T14:25:01Z</updated>
    <entry>
        <title>Internet Explorer 8 (IE8) does not generate new Session IDs for new tabs and windows</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/11/20/internet-explorer-8-ie8-does-not-generate-new-session-ids.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/11/20/internet-explorer-8-ie8-does-not-generate-new-session-ids.aspx</id>
        <published>2008-11-20T09:15:3100:00:00</published>
        <updated>2008-11-20T09:15:31Z</updated>
        <content type="html">&lt;p&gt;One of IE8's new features is a that when closing a window or tab, it can remember the session ID and therefore maintain the session with a website. This is an advantage if you accidentally close a window or tab, but there is a problem.&lt;/p&gt;  &lt;p&gt;For example, if you have several logins for a site, each login giving totally separate functionality of the site (e.g customer and administrator), then you may close your browser and login as someone else. What you &lt;em&gt;may&lt;/em&gt; find is that when you open a new window, you are already logged in as the other person. Why does this happen?&lt;/p&gt;  &lt;p&gt;IE8 seems to use the first window a storage mechanism for sites visited in this browsing session. When visiting a site, the unique Session ID is generated and then stored in this host process. When opening new tabs and windows the new process checks the host process to see if the website has been visited and if so returns the existing session ID.&lt;/p&gt;  &lt;p&gt;So to re-iterate, if you have at least 1 browser window open, all subsequent tabs and windows pointing to a specific site will all generate the same session ID.&lt;/p&gt;  &lt;p&gt;In order to get around this "problem", you should close all instances of IE8. The session information seems to be stored in memory rather than on disk, so by closing all of these instances, the problem goes away. &lt;/p&gt;  &lt;p&gt;This serves as a warning to both developers and kiosk/Internet café users. Developers should provide the ability to log out of a site so that the Session is cleaned up after a user logs out. Kiosk/Internet Café users should close ALL Internet browser sessions down before leaving!&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/365.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/365.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/365.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/365.aspx</trackback:ping>
    </entry>
    <entry>
        <title>SecurityException when making requests to websites in Silverlight 2.0</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/11/17/securityexception-when-making-requests-to-websites-in-silverlight-2.0.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/11/17/securityexception-when-making-requests-to-websites-in-silverlight-2.0.aspx</id>
        <published>2008-11-17T12:48:5500:00:00</published>
        <updated>2008-11-20T14:25:01Z</updated>
        <content type="html">&lt;p&gt;The 3 types of connections Silverlight can make are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Connections to web services and WCF services &lt;/li&gt;    &lt;li&gt;HTTP requests (via &lt;em&gt;HttpWebRequest&lt;/em&gt; and &lt;em&gt;WebClient&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;Raw data transfers &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;When building one of your early applications, you may choose to try out one of these classes to get back a really simple piece of HTML, purely to test the connection. For example:&lt;/p&gt;  &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;   &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Button_Click(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, EventArgs e)
{
    WebClient wc = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WebClient(&lt;span style="color: #006080"&gt;"http://www.google.com"&lt;/span&gt;);    
    wc.DownloadStringAsyncCompleted += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; EventHandler&amp;lt;DownloadStringEventArgs&amp;gt;(wc_DownloadStringCompleted);    
    wc.DownloadStringAsync();
}

&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; wc_DownloadStringCompleted(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, DownloadStringEventArgs e)
{
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(e.Error == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; e.Cancelled == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)  
    {        
        Debug.WriteLine(&lt;span style="color: #006080"&gt;"Data downloaded = "&lt;/span&gt; + e.Result);    
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;(N.B I apologise if this doesn't compile, I've just typed this by hand.)&lt;/p&gt;

&lt;p&gt;If you run this, you'll find it generates a &lt;em&gt;SecurityException&lt;/em&gt;, but why?&lt;/p&gt;

&lt;p&gt;To improve security, the only connections you can make without any security implications is &lt;em&gt;&lt;strong&gt;your own&lt;/strong&gt; website&lt;/em&gt;. If you want to call another website, Silverlight needs to check a few things. Firstly, it looks for a policy file called "clientaccesspolicy.xml", which is a file used by Adobe Flash to find out if it allows connections from Flash applications. If this file is missing, it then looks for "crossdomain.xml", which is a Silverlight version of this file.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If one of these exists AND the policy file allows connections, then the application will be allowed to connect. &lt;/li&gt;

  &lt;li&gt;If neither exists, or the policy in an existing file denies access to the requested virtual directory, then a &lt;em&gt;SecurityException&lt;/em&gt; will be thrown by the application. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To overcome this problem, you have 2 options&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Check the website has an API to use the capabilities you are after. &lt;a target="_blank" href="http://code.google.com/apis/ajaxsearch/"&gt;Google&lt;/a&gt; and &lt;a target="_blank" href="http://apidoc.digg.com/"&gt;Digg&lt;/a&gt; have service APIs in order to allow applications to do this. &lt;/li&gt;

  &lt;li&gt;Write a web service (or WCF service), that does the enquiry for you. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you need to see the contents of these files, I have found &lt;a title="Microsoft Fiddler" target="_blank" href="http://www.fiddlertool.com"&gt;Fiddler&lt;/a&gt; an excellent tool for this purpose.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/363.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/363.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/363.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/363.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Delayed Write Problem on $MFT and $Bitmap on an external hard disk</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/11/12/delayed-write-problem-on-mft-and-bitmap-on-an-external.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/11/12/delayed-write-problem-on-mft-and-bitmap-on-an-external.aspx</id>
        <published>2008-11-12T09:23:1900:00:00</published>
        <updated>2008-11-12T09:23:19Z</updated>
        <content type="html">&lt;p&gt;I put my VPC's on my external hard disk and run them off there, as the hard drive can run independently of Windows.&lt;/p&gt;  &lt;p&gt;I've noticed over the past few months, that randomly, my external USB 2.0 hard drive (Freecom 400GB) disconnects for no reason and I get a balloon in the system tray with the title "Delayed Write Problem" , then something to do with G:\$MFT or G:\$Bitmap. &lt;/p&gt;  &lt;p&gt;Yesterday, I was defragmenting my external hard disk and noticed it kept happening. I turned it off and on again, started defragmenting and the same problem happened.&lt;/p&gt;  &lt;p&gt;I thought it might be the writing caching, which helps improve performance, so when viewing the properties of the drive (My Computer -&amp;gt; [External Hard Disk] -&amp;gt; Right-click Properties -&amp;gt; Hardware -&amp;gt; Properties -&amp;gt; "Enable write caching on the disk"), I turned this option off.&lt;/p&gt;  &lt;p&gt;Another defragmentation session and the same problem happened again and again.&lt;/p&gt;  &lt;p&gt;What I finally realised was that I was using some software (DAEMON Tools Lite) for mounting ISO's to a CD drive. Although the software wasn't running, I still had the CD drive present in my drive listing. I loaded up DAEMON Tools Lite and turned off the emulated CD drive (which I wasn't using anyway) and HEY PRESTO! it was working.&lt;/p&gt;  &lt;p&gt;I defragmented half the drive and then turned write caching back on and it was fine!&lt;/p&gt;  &lt;p&gt;Hope this helps someone, as when a hard drive disconnects and a VPC is running off it, you are in trouble!&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/361.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/361.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/361.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/361.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Should I change my Session state to StateServer ?</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/10/09/should-i-change-my-session-state-to-stateserver.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/10/09/should-i-change-my-session-state-to-stateserver.aspx</id>
        <published>2008-10-09T15:35:1101:00:00</published>
        <updated>2008-10-09T15:46:18Z</updated>
        <content type="html">&lt;p&gt;After various conversations on both internal and external projects, I’ve noticed that some of our web projects were being developed using a Session state called &lt;em&gt;InProc&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;InProc&lt;/em&gt; means In-Process, so IIS is hosting all of the &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;Session[“Dom”] = “Idiot!”; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;variables. Now, if the web application eats up too much memory, IIS 6 and above can be configured to recycle the Application pool – a contained unit of web sites, in order to free up memory. This is good for IIS but baaaad news for your website, as everything in the Session is lost – that could be shopping basket, current order status, user details! InProc is a bit of a quick and nasty method of developing as it just does its job, assuming that you are not going to host this to a larger audience (over about 5 users).&lt;/p&gt;
&lt;p&gt;As I found out with any large project, nothing goes to plan, so, the best thing to do is to use &lt;em&gt;StateServer&lt;/em&gt; and not &lt;em&gt;InProc&lt;/em&gt; – but WHY?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;StateServer&lt;/em&gt; is a totally out-of-process service that runs on either the same web server or a different server entirely. Session data is saved to this service so if the AppPool recycles, you have no problems whatsoever. Also, the data needs to serialize properly in order for it to be saved to the service.&lt;/p&gt;
&lt;p&gt;Whats the point I hear you ask? Well, the point is that if you use InProc and your application gets load balanced (often without your knowledge), the website won’t work properly. Remember, load balancing is decided, by either the hardware or the software, to help manage the network traffic to a destination server. So 1 request to go to Server1 and another or Server2, or even ServerX! Youch! So your site’s Session could be stored on another machine.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;StateServer&lt;/em&gt; works because all of the web.config’s point to the same machine, so they all work off that one service.&lt;/p&gt;
&lt;p&gt;The other option is &lt;em&gt;SQLServer&lt;/em&gt;, which stores it in SQL Server, which is more for web farms (multiple physical PCs), rather than web gardens (multiple virtualized PCs). Or you can use &lt;em&gt;Custom&lt;/em&gt; for a custom implementation (e.g ODBC or XML), or &lt;em&gt;Off&lt;/em&gt; to turn it off completely.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Therefore, a good recommendation is to always change your web.config to say:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;sessionState&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;mode&lt;/span&gt;=”&lt;span style="color: rgb(255, 0, 0);"&gt;StateServer&lt;/span&gt;” … &lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And start the ASP.NET State Service on your machine. This way to can always revert to &lt;em&gt;InProc&lt;/em&gt; if you need to, but you are still ready for the switch over.&lt;/p&gt;
&lt;p&gt;Believe me, it’s a lot easier to start out this way, rather than have to work backwards.&lt;/p&gt;
&lt;p&gt;It is always a worthwhile exercise!&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/357.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/357.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/357.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/357.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Creating objects that know when they have changed - using IsDirty and custom PropertyChangedEvent's</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/10/09/creating-objects-that-know-when-they-have-changed---using.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/10/09/creating-objects-that-know-when-they-have-changed---using.aspx</id>
        <published>2008-10-09T11:57:5701:00:00</published>
        <updated>2008-10-09T14:32:22Z</updated>
        <content type="html">&lt;p&gt;When working on a project recently, the data saved back to the database was huge. We are talking well over 100 properties. After talking to my Java buddy, he said that one way is to create an IsDirty method that returns if the object has changed state.&lt;/p&gt;  &lt;p&gt;The IsDirty() method is used to query the state of an object - if it has been modified or not. The implementation is up to the developer, but ultimately it's use can save you a trip to the data source.&lt;/p&gt;  &lt;p&gt;I have split this example into 3 stages:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;First attempt &lt;/li&gt;    &lt;li&gt;Refactored &lt;/li&gt;    &lt;li&gt;Event Handled &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;N.B. This article helps someone in (archaic) .NET 2.0 - but if you've looking to develop for .NET 3.5, it might be worth looking into DependencyProperty's, which caters for this sort of problem.&lt;/p&gt;  &lt;h2&gt;First Attempt&lt;/h2&gt;  &lt;p&gt;The problem I found when prototyping this is that it can be quite impractical at first glance:&lt;/p&gt;  &lt;div&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Customer
{
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; firstName, surname;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Customer(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; firstName, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; surname)
    {
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.firstName = firstName;
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.surname = surname;
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; FirstName
    {
        get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; firstName; }
        set 
        { 
            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;( firstName != &lt;span style="color: #0000ff"&gt;value&lt;/span&gt; )
            {
                IsDirty = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
                firstName = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;
            }
        }
    }
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Surname
    {
        get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; surname; }
        set 
        { 
            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;( surname != &lt;span style="color: #0000ff"&gt;value&lt;/span&gt; )
            {
                IsDirty = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
                surname = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;
            }
        }
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsDirty { get; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set; }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;This is a bit bloated. Also, it relies on every property having to check for itself whether it is equal or not, setting the IsDirty property and also assigning the value.&lt;/p&gt;

&lt;p&gt;After writing this and implementing a few more properties I found this to be a bit too impractical. Instead, I realised that they are all basically doing 3 steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Checking the previous value to the new value. &lt;/li&gt;

  &lt;li&gt;Setting IsDirty. &lt;/li&gt;

  &lt;li&gt;Assigning the new value. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This example needs simplifying - or refactoring to be more precise.&lt;/p&gt;

&lt;h2&gt;Refactoring&lt;/h2&gt;

&lt;p&gt;Instead, I decided to implement this behaviour in a method called SetValue&amp;lt;T&amp;gt;() , which would raise events when a property has changed. This allows the IsDirty property to be changed centrally and debugging to be centralized. &lt;/p&gt;

&lt;p&gt;Firsty I created the method:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; T varName, T &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
{
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (varName.Equals(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;))
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;
    
    IsDirty = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
    varName = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Now, we can change our properties to:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; FirstName
{
    get {&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; firstName; }
    set
    {
        SetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(ref firstName,&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;);
    }
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Surname
{
    get {&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; surname; }
    set
    {
        SetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(ref surname,&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; This is one implementation of it, but I decided to extend it to event handling as well, so that even the outside world can be notified about what is changing.&lt;/p&gt;

&lt;h2&gt;Event Handling&lt;/h2&gt;

&lt;p&gt;I decided to also implement this is in a typical Microsoft fashion: OnPropertyChanging and OnPropertyChanged. &lt;/p&gt;

&lt;p&gt;In order to turn this into an event driven model I need to do a few things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create some event arguments describing what is being changed. &lt;/li&gt;

  &lt;li&gt;Add some event handlers to trigger a notification that they have been changed. &lt;/li&gt;

  &lt;li&gt;Trigger the events from within your code &lt;/li&gt;

  &lt;li&gt;(Optionally) wire up the events. &lt;/li&gt;

  &lt;li&gt;Trigger the events from within your code &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Create event arguments&lt;/h3&gt;

&lt;p&gt;Since I am using events, I want to create event arguments detailing what has changed. I have implemented this in 2 identical classes (and 1 base class). Here I have implemented a base class, and 2 classes which improve the clarity of the base class. I have chosen clarity over code bloat:&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #008000"&gt;//Base class implementation&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; PropertyChangeEventArgs : EventArgs
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; PropertyName { get; &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; set; }
    &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; Value { get; set; }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; PropertyChangeEventArgs(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; propertyName, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; before)
    {
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.PropertyName = propertyName;
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Value = before;
    }
}

&lt;span style="color: #008000"&gt;//Holds arguments before the proerty has changed&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; PropertyChangingEventArgs : PropertyChangeEventArgs
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; ValueBeforeChange
    {
        get
        {
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Value;
        }
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; PropertyChangingEventArgs(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; propertyName, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; before) 
        : &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;(propertyName,before)
    {
    }
}

&lt;span style="color: #008000"&gt;//Holds arguments after the proerty has changed&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; PropertyChangedEventArgs : PropertyChangeEventArgs
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; ValueAfterChange
    { 
        get 
        { 
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Value; 
        } 
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; PropertyChangedEventArgs(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; propertyName, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; after) 
        : &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;(propertyName, after)
    {
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Create event handlers&lt;/h3&gt;

&lt;p&gt;Now implement some event handlers:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;event&lt;/span&gt; PropertyChangingEventHandler PropertyChanging;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PropertyChangingEventHandler(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; o, PropertyChangingEventArgs e);

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PropertyChangedEventHandler(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; o,PropertyChangedEventArgs e);&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Trigger the events from within your code&lt;/h3&gt;

&lt;p&gt;Now I need to trigger these events from the SetValue&amp;lt;T&amp;gt;() method. I will also need to find out what property was called by this SetValue&amp;lt;T&amp;gt;() method.&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(T varName, T &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
{
       &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (varName.Equals(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;))
           &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;

       &lt;span style="color: #008000"&gt;//TODO: Find the property's name that is calling this method&lt;/span&gt;
       &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; propertyName = &lt;span style="color: #006080"&gt;"MyProperty"&lt;/span&gt;;

       &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (PropertyChanging != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
           OnPropertyChanging(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PropertyChangingEventArgs(propertyName, varName));

       varName = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;

       &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (PropertyChanged != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
           OnPropertyChanged(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PropertyChangedEventArgs(propertyName, varName));
   }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The last thing to do is to find the property that is calling this method. This can be done using Reflection, by looking up the call stack. By getting the previous "frame", we can deduce where this is being called from. In our case, this is called "set_FirstName". Remove the "set_" and we have our property name:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;StackTrace st = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StackTrace();
StackFrame current = st.GetFrame(1);
&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; propertyName = current.GetMethod().Name.Replace(&lt;span style="color: #006080"&gt;"set_"&lt;/span&gt;, &lt;span style="color: #006080"&gt;""&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;(Optionally) wire up the events&lt;/h3&gt;

&lt;p&gt;I want to subscribe to the OnPropertyChanged event, so that I can set the IsDirty property within it. I have done this within a private constructor:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Customer()
{
   PropertyChanged += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PropertyChangedEventHandler(OnPropertyChanged);
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Customer(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; firstName, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; surname) : &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;()
{
   &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.firstName = firstName;
   &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.surname = surname;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;(Optionally) trigger the events from within your code&lt;/h3&gt;

&lt;p&gt;Now, I want to move the IsDirty assignment out of the SetValue() method because although overkill, it is not related to the setting of the value. It is related to the &lt;em&gt;event &lt;/em&gt;but not the &lt;em&gt;assignment&lt;/em&gt;. It also means that you have just wasted 15 minutes reading this article! :-) :&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnPropertyChanged(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, PropertyChangedEventArgs ea)
{
   IsDirty = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;So what have we done? We've looked at ways in which we can implement notification within a class, so that we can save a database access. When getting to a large number of properties, we can bypass each save and also allow the outside world to be notified of its changes.&lt;/p&gt;

&lt;p&gt;In the first example,we saw a cheap and nasty way of doing it, but impractical for larger classes. In the second example we saw a better implementation and in the third example we added event handling to extend the functionality further.&lt;/p&gt;

&lt;p&gt;I hope this article helps someone - but if you've looking to develop for .NET 3.5, it might be worth looking into DependencyProperty's, which caters for this sort of problem.&lt;/p&gt;

&lt;p&gt;Here is the full source code to have a play with:&lt;/p&gt;

&lt;div overflow="auto"&gt;
  &lt;pre&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer c = new Customer("Dominic", "Zukiewicz");
            c.FirstName = "Dommy dom dom";

            Console.WriteLine("Customer.IsDirty = " + c.IsDirty);

            Customer c2 = new Customer("Dominic", "Zukiewicz");
            c.Surname = "Zukiewicz";

            Console.WriteLine("Customer2.IsDirty = " + c2.IsDirty);
        }
    }

    class Customer
    {
        private string firstName;
        private string surname;

        private Customer()
        {
            PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);
        }

        public Customer(string firstName, string surname) : this()
        {
            this.firstName = firstName;
            this.surname = surname;
        }

        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                SetValue&lt;string&gt;(ref firstName, value);
            }
        }

        public string Surname
        {
            get 
            { 
                return surname; 
            }
            set
            {
                SetValue&lt;string&gt;(ref surname,value);
            }
        }

        public bool IsDirty { get; private set; }


        private void SetValue&lt;t&gt;(T varName, T value)
        {
            if (varName.Equals(value))
                return;

            StackTrace st = new StackTrace();
            StackFrame current = st.GetFrame(1);
            string propertyName = current.GetMethod().Name.Replace("set_", "");

            if (PropertyChanging != null)
                OnPropertyChanging(this, new PropertyChangingEventArgs(propertyName, varName));

            varName = value;

            if (PropertyChanged != null)
                OnPropertyChanged(this, new PropertyChangedEventArgs(propertyName, varName));
        }

        public event PropertyChangingEventHandler PropertyChanging;
        public delegate void PropertyChangingEventHandler(object o, PropertyChangingEventArgs e);

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void PropertyChangedEventHandler(object o,PropertyChangedEventArgs e);

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs ea)
        {
            Debug.Print("Property: {0}, After: {1}", ea.PropertyName, ea.ValueAfterChange);
            IsDirty = true;
        }

        private void OnPropertyChanging(object sender, PropertyChangingEventArgs ea)
        {
            Debug.Print("Property: {0}, Before: {1}", ea.PropertyName, ea.ValueBeforeChange);
        }
        
    }

    //Base class implementation
    public abstract class PropertyChangeEventArgs : EventArgs
    {
        public string PropertyName { get; protected set; }
        protected object Value { get; set; }

        public PropertyChangeEventArgs(string propertyName, object before)
        {
            this.PropertyName = propertyName;
            this.Value = before;
        }
    }

    //Holds arguments before the proerty has changed
    public class PropertyChangingEventArgs : PropertyChangeEventArgs
    {
        public object ValueBeforeChange
        {
            get
            {
                return this.Value;
            }
        }

        public PropertyChangingEventArgs(string propertyName, object before) 
            : base(propertyName,before)
        {
        }
    }

    //Holds arguments after the proerty has changed
    public class PropertyChangedEventArgs : PropertyChangeEventArgs
    {
        public object ValueAfterChange
        { 
            get 
            { 
                return this.Value; 
            } 
        }

        public PropertyChangedEventArgs(string propertyName, object after) 
            : base(propertyName, after)
        {
        }
    }
}&lt;/t&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/356.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/356.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/356.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/356.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Using Visual Studio 2005/2008 to easily insert column names into a stored procedure</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/10/08/using-visual-studio-20052008-to-easily-insert-column-names-into.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/10/08/using-visual-studio-20052008-to-easily-insert-column-names-into.aspx</id>
        <published>2008-10-08T16:13:1001:00:00</published>
        <updated>2008-10-08T16:18:35Z</updated>
        <content type="html">&lt;p&gt;When writing stored procedures or views, I always create them in Visual Studio (2005/2008) and then run them on a specified database. When using "SELECT *", it is compact, but obviously does not help you find the columns of the table or view.&lt;/p&gt;
&lt;p&gt;When using the IDE, I added a new SQL Script for Stored Procedures, and then I typed:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;SELECT&lt;/span&gt; *&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;FROM&lt;/span&gt; Customers&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and a blue box surrounded the statement. I right-clicked -&amp;gt; Design SQL Block.&lt;/p&gt;
&lt;p&gt;The designer then listed all of the column names for me, and when clicking OK, instantly put them back into the SQL script I was creating!&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;SELECT&lt;/span&gt;  CustomerID, CompanyName, ContactName, ContactTitle, &lt;br /&gt;        Address, City, Region, PostalCode, Country, Phone, Fax&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;FROM&lt;/span&gt;    Customers&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;For the table I was using, this list over 60 columns for me!&lt;/p&gt;
&lt;p&gt;So if you want a quick way of listing all of the column names for table of view:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Add a database project to your site (if you haven't already done so)&lt;/li&gt;
    &lt;li&gt;Right click Queries -&amp;gt; Add SQL Script&lt;/li&gt;
    &lt;li&gt;Choose Stored Procedure Script&lt;/li&gt;
    &lt;li&gt;Type your SELECT * From XYZ statement&lt;/li&gt;
    &lt;li&gt;Right click -&amp;gt; Design SQL Block&lt;/li&gt;
    &lt;li&gt;Click OK.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The designer will be updated with the new query.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/355.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/355.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/355.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/355.aspx</trackback:ping>
    </entry>
    <entry>
        <title>StreamReader.BaseStream.Position isn't changing after calling StreamReader.ReadLine()</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/09/05/streamreader.basestream.position-isnt-changing-after-calling-streamreader.readline-yet-again.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/09/05/streamreader.basestream.position-isnt-changing-after-calling-streamreader.readline-yet-again.aspx</id>
        <published>2008-09-05T14:28:5601:00:00</published>
        <updated>2008-09-08T16:16:03Z</updated>
        <content type="html">&lt;p&gt;I was trying to parse a file which was divided into numerous parts - each one 12 lines long. For debugging, I was interested in how fast the file was being read and processed and therefore looked at the &lt;code&gt;StreamReader.BaseStream.Position&lt;/code&gt; property. Here is an example of what I was trying to do:&lt;/p&gt;
&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; height: 313px; background-color: rgb(244, 244, 244);"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Program&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; filename = &lt;span style="color: rgb(0, 96, 128);"&gt;@"C:\windows\windowsupdate.log"&lt;/span&gt;;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; filenameBackup = filename + &lt;span style="color: rgb(0, 96, 128);"&gt;".bac"&lt;/span&gt;;&lt;br /&gt;        File.Copy(filename, filenameBackup);&lt;br /&gt;&lt;br /&gt;        FileStream fs = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; FileStream(filenameBackup, FileMode.Open, FileAccess.Read);&lt;br /&gt;        StreamReader sr = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; StreamReader(fs);&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; lineNumber = 0;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt; (!sr.EndOfStream)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"Lines Read = {0}, Position = {1}"&lt;/span&gt;, lineNumber, sr.BaseStream.Position);&lt;br /&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] lines = ReadXLines(sr,5);&lt;br /&gt;            lineNumber += lines.Length;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        Console.Read();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] ReadXLines(StreamReader sr, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; linesToRead)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] lines = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[linesToRead];&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; lineNumber = 0; lineNumber &amp;lt; lines.Length; lineNumber++)&lt;br /&gt;        {&lt;br /&gt;            lines[lineNumber] = sr.ReadLine();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; lines;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now, the output on my computer was this:&lt;/p&gt;
&lt;code&gt;   &lt;/code&gt;
&lt;p&gt;Lines Read = 0, Position = &lt;strong&gt;1024&lt;/strong&gt;       &lt;br /&gt;
Lines Read = 5, Position = &lt;strong&gt;1024&lt;/strong&gt;       &lt;br /&gt;
Lines Read = 10, Position = &lt;strong&gt;1024&lt;/strong&gt;       &lt;br /&gt;
Lines Read = 15, Position = 2048       &lt;br /&gt;
Lines Read = 20, Position = 2048       &lt;br /&gt;
Lines Read = 25, Position = 3072       &lt;br /&gt;
Lines Read = 30, Position = 3072       &lt;br /&gt;
Lines Read = 35, Position = 4096       &lt;br /&gt;
Lines Read = 40, Position = 5120       &lt;br /&gt;
Lines Read = 45, Position = 5120       &lt;br /&gt;
Lines Read = 50, Position = 6144       &lt;br /&gt;
Lines Read = 55, Position = 7168       &lt;br /&gt;
Lines Read = 60, Position = 7168       &lt;br /&gt;
Lines Read = 65, Position = 8192       &lt;br /&gt;
Lines Read = 70, Position = 8192       &lt;br /&gt;
Lines Read = 75, Position = 9216       &lt;br /&gt;
Lines Read = 80, Position = 9216       &lt;br /&gt;
Lines Read = 85, Position = 10240       &lt;br /&gt;
Lines Read = 90, Position = 10240       &lt;br /&gt;
Lines Read = 95, Position = 11264       &lt;br /&gt;
Lines Read = 100, Position = 11264       &lt;br /&gt;
Lines Read = 105, Position = 12288       &lt;br /&gt;
......      &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;What is odd about this sequence is that the position of the &lt;code&gt;FileStream&lt;/code&gt; is already at 1024 before anything has been read! Also, the 1024 position stays the same for 2 reads. Why is this?&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;StreamReader&lt;/code&gt; class is a buffered reader. Therefore, it is reading the stream before anything is processed and when reading directly from &lt;code&gt;StreamReader&lt;/code&gt;, if the amount read does not extract the entire buffer, then the position of the underlying stream doesn't change.&lt;/p&gt;
&lt;p&gt;Unfortunately, there is nothing you can do about this. Therefore the only 2 ways you can keep track of the position is to:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Count how many characters are in each line (plus the &lt;code&gt;Environment.Newline&lt;/code&gt;) and total up the position line by line.&lt;/li&gt;
    &lt;li&gt;Create your own implementation of the &lt;code&gt;FileStream&lt;/code&gt;, which has a ReadLine() method. This method should continue to read the Stream until Environment.Newline is encountered. Remember, &lt;code&gt;Environment.Newline&lt;/code&gt; is not included in the string returned.&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/348.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/348.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/348.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/348.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Windows Presentation Foundation (WPF) - What is it?</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/08/29/windows-presentation-foundation-wpf---what-is-it.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/08/29/windows-presentation-foundation-wpf---what-is-it.aspx</id>
        <published>2008-08-29T13:14:5501:00:00</published>
        <updated>2008-08-29T13:22:57Z</updated>
        <content type="html">&lt;p&gt;WPF is revolutionary as it re-invents the Windows design and programming for the Microsoft platform. But why bother?&lt;/p&gt;
&lt;p&gt;I could write an article for hours and hours on the changes brought about with WPF. Here, I will give a very brief overview of the more visual changes brought about.&lt;/p&gt;
&lt;h2&gt;History&lt;/h2&gt;
&lt;p&gt;If you have had any experience programming Windows in .NET, VB6 or C++ MFC, you'll have heard of GDI. GDI (more recently GDI+) is an API which you use to render text images and other custom drawing functions (like lines, circles, rectangles etc). The GDI library has been around since Windows 95 and was a huge hit with developers for drawing basic elements on the page. The standard Windows forms and controls are rendered by the User.dll (more recently User32.dll). &lt;/p&gt;
&lt;p&gt;The problem that faces GDI in the modern computer is that it is implemented in software. In 1995, this is a sensible solution, but in 2008 the average computer has a half decent graphics processor. Also, DirectX has matured a huge amount since Windows 95 and this isn't utilised at all. We need a way of using the hardware to do our UI rendering so that we can draw both 2D and 3D objects within our applications, without imposing too much on the software core of the OS.&lt;/p&gt;
&lt;h2&gt;Overview&lt;/h2&gt;
&lt;h3&gt;Graphics&lt;/h3&gt;
&lt;p&gt;WPF replaces the slow GDI libraries by using DirectX as its UI drawing library. WPF is also intelligent enough to know about your hardware capabilities, falling back to the slower software rendering should it need to. WPF's DirectX rendering capabilities only work for Windows XP and Windows Vista and even then there is a catch. When Microsoft were thinking about Vista and "Indigo" (code name for the WPF project), they released a new specification for hardware manufacturers to follow, to accommodate the new interface. Therefore, if your video card didn't receive a patch after 2004, OR they haven't added this requirement, then you will only have software rendering capabilities.&lt;/p&gt;
&lt;p&gt;That doesn't mean that Direct3D and OpenGL are defunct. WPF is an implementation and is not as fast as Direct3D. If you need to create some real-time, high resolution graphics package or game then WPF will not be the way to go. WPF does cater for 3D graphics, rotation, lighting, transformation etc, but it does not &lt;em&gt;replace&lt;/em&gt; the abilities of the Direct3D.&lt;/p&gt;
&lt;h3&gt;Flexible UI components&lt;/h3&gt;
&lt;p&gt;Some of the web concepts have really given WPF so great ideas on how to improve the framework. These include:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Layouts are flexible to the width of the application.&lt;/li&gt;
    &lt;li&gt;Creating text documents similar to Microsoft Word are as simple as adding headings, paragraphs, tables with captions, images etc.&lt;/li&gt;
    &lt;li&gt;Create styles for controls and apply them across your application - much like skins in ASP.NET.&lt;/li&gt;
    &lt;li&gt;Data binding can be done at page level, rather than by each control separately.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Think about a list. A list, as a high level abstract is just a grouping of 1 or more items. That's it. Therefore, the current ListBox implementation is quite inflexible; you just have a bit of text for the description. If you work harder you could get an image in there as well, but ultimately, the ListBox is really a TextListBox.&lt;/p&gt;
&lt;p&gt;An awesome example of the thinking behind WPF is that the ListBox has been totally redesigned to hold just about anything you want. If you want an image/text combination, you group the 2 together and add it to the list! Easy peesy. You might be surprised that a CheckBoxList doesn't exist - why? Because it is just a list of CheckBox controls - so why create a new control, when a ListBox with CheckBoxes in does the same thing!&lt;/p&gt;
&lt;h3&gt;Resolution Independence&lt;/h3&gt;
&lt;p&gt;When working off high resolution monitors, the pixel density is a lot higher. This means if 2 screens of the same size, but with different resolutions, display the same size window, the higher resolution monitor will show the window to be much smaller. I personally use a high resolution laptop, therefore my task bar is tiny. You can increase the size of your desktop by either:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Changing the font sizes&lt;/li&gt;
    &lt;li&gt;Changing the DPI settings (preferred)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; By changing your DPI settings, the fonts remain proportionate to your screen.&lt;/p&gt;
&lt;p&gt;The only problem is that applications built prior to XP didn't have to take this into account and therefore changing the DPI can make some windows look a little strange, whereas others (which have catered for it) remain relatively normal. In Vista, it uses a slightly different technology called &lt;em&gt;bitmap scaling&lt;/em&gt; which intelligently stretches images and fonts to fill out the DPI setting.&lt;/p&gt;
&lt;h3&gt;Event Model Changes&lt;/h3&gt;
&lt;p&gt;The event notification model has changed as well, meaning that events can "bubble" up to parent containers if needs be. &lt;/p&gt;
&lt;h3&gt;Navigation support&lt;/h3&gt;
&lt;p&gt;Users are getting more and more familiar with the navigation of web pages - a step by step approach to using sites. The WPF team have created page based navigation too, with page history. This allows users to create a similar navigation system to that of web pages, but from a user interface.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;There are many many many more exciting features that WPF brings to .NET, and you will find huge books on the subject. Hopefully, I have given a small taster of the high level features available to you and in the future, I will try and provide some samples of what it can do.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/341.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/341.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/341.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/341.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Microsoft StyleCop - Source code analysis for formatting and style</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/07/23/microsoft-stylecop---source-code-analysis-for-formatting-and-style.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/07/23/microsoft-stylecop---source-code-analysis-for-formatting-and-style.aspx</id>
        <published>2008-07-23T15:23:4501:00:00</published>
        <updated>2008-08-06T17:05:02Z</updated>
        <content type="html">&lt;p&gt;Microsoft recently (on my birthday) released a tool to analyse coding structure and formatting. No - its not FxCop - &lt;em&gt;StyleCop&lt;/em&gt; analyses syntactical formatting. Let me explain:&lt;/p&gt;  &lt;h3&gt;Background&lt;/h3&gt;  &lt;p&gt;When Microsoft released FxCop, some people cringed. I remember at my previous job when having a code review and the conversation following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Consultant: "Have you FxCop'd it?". &lt;/li&gt;    &lt;li&gt;Me: "No...." &lt;/li&gt;    &lt;li&gt;Consultant: "Hahahah - see you in 3 months!" &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Those days, for me anyway, have gone. I started to use it in my everyday coding and am now pleased I use it without a second thought.&lt;/p&gt;  &lt;p&gt;In my position with this company, I have tried to promote its use and benefits, because it promotes good coding guidelines standard approaches to software development.&lt;/p&gt;  &lt;p&gt;FxCop works by analysing compiled code and therefore can look into its improvements at a detailed view. This is all good, but what about coding standards for syntax!?&lt;/p&gt;  &lt;h3&gt;Overview&lt;/h3&gt;  &lt;p&gt;StyleCop is a tool written and used by Microsoft to ensure all of their code is formatted, commented and laid out in a fixed format. It has quite a rigid structure to it, but so it should. I've read lots of threads about people complaining about its flexibility, but that's the whole point of standards - they don't change &lt;strike&gt;(often)&lt;/strike&gt; .&lt;/p&gt;  &lt;p&gt;Before I provide a link, I just want to show you how much you need to do, in order to pass one of these successfully. Here is a typical piece of code:&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244,244,244)"&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas,'Courier New',courier,monospace; border-right-style: none; border-left-style: none; background-color: rgb(244,244,244); border-bottom-style: none"&gt;&lt;p&gt;&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System.&lt;span style="color: rgb(0,0,255)"&gt;Diagnostics&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; LogicAndData;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;User&lt;/span&gt; {     &lt;/p&gt;&lt;p&gt;    private string _username;      &lt;/p&gt;&lt;p&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; string Username&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; _username; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [DebuggerBrowsable(DebuggerBrowsableState.Never)]&lt;br /&gt;    private string _password;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; bool IsCorrectPassword(string guess)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (String.IsNullOrEmpty(guess))&lt;br /&gt;            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; guess.&lt;span style="color: rgb(0,0,255)"&gt;Equals&lt;/span&gt;(_password, StringComparison.CurrentCulture);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;User&lt;/span&gt;(string username, string password)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (username == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)&lt;br /&gt;            throw &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; ArgumentNullException(Resources.Username_ParameterName);&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;else&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (username.Length == 0)&lt;br /&gt;            throw &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; ArgumentException(Resources.Username_ParameterEmpty, Resources.Username_ParameterName);&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (password == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)&lt;br /&gt;            throw &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; ArgumentNullException(Resources.Password_ParameterName);&lt;br /&gt;        &lt;span style="color: rgb(0,0,255)"&gt;else&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (password.Length == 0)&lt;br /&gt;            throw &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; ArgumentException(Resources.Password_ParameterEmpty, Resources.Password_ParameterName);&lt;br /&gt;&lt;br /&gt;        _username = username;&lt;br /&gt;        _password = password;&lt;br /&gt;    }&lt;br /&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Pretty standard stuff really. Okay, its missing comments but it does pass FxCop! So lets have a look at the errors generated for this class:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/dominicz/WindowsLiveWriter/MicrosoftStyleCopSourcecodeanalysisforfo_D8EF/styleCopErrors_6.gif"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="366" alt="styleCopErrors" src="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/dominicz/WindowsLiveWriter/MicrosoftStyleCopSourcecodeanalysisforfo_D8EF/styleCopErrors_thumb_2.gif" width="598" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So, what are the problems?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;SA1101: If you are referring to member variables (variables at class level), then you should use the keyword "this." to be explicit (2 errors) &lt;/li&gt;

  &lt;li&gt;SA1201: This is a formatting issue - the order is - private fields, constructors, properties, public methods, private methods. (2 errors) &lt;/li&gt;

  &lt;li&gt;SA1309: Don't use underscores! The "this" keyword implies it. (2 errors) &lt;/li&gt;

  &lt;li&gt;SA1400: Class must have an identifier instead of the default (protected internal) (1 error) &lt;/li&gt;

  &lt;li&gt;SA1503: If statements must use { }, even if they are only 1 line of code (6 errors) &lt;/li&gt;

  &lt;li&gt;SA1600: All classes, interfaces, methods, properties and fields (optional) MUST have an XML header for documentation purposes. (4 errors) &lt;/li&gt;

  &lt;li&gt;SA1633: Related to the above, there is a preset format for the documentation for the header of the file that MUST be at the top of each file created. (1 error) &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Using StyleCop&lt;/h3&gt;

&lt;p&gt;What does this improve? relating to the above:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It is clearer what you are working with. You don't need to use prefixes, as by using "this" you are describing which one you are referencing anyway. &lt;/li&gt;

  &lt;li&gt;This just standardises the layout of your code. &lt;/li&gt;

  &lt;li&gt;For clarity. It doesn't like Hungarian notation either, but DOES allow you to add acceptable notations should you wish. &lt;/li&gt;

  &lt;li&gt;Be explicit about the access type, as you may not understand the access of an unmarked class. &lt;/li&gt;

  &lt;li&gt;By laying out if statements this way, you are totally consistent across your code. &lt;/li&gt;

  &lt;li&gt;Documentation - who doesn't document their code!? &lt;/li&gt;

  &lt;li&gt;Add a header to just clarify what's doing on, any copyright notices, authors etc. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So lets see what this looks like without any errors at all!&lt;/p&gt;

&lt;p&gt;[40 minutes later and 3 cups of coffee].... only joking - it probably took 5 minutes, mainly because I have used it before:&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #008000"&gt;// &amp;lt;copyright file="User.cs" company="Interakting"&amp;gt;&lt;/span&gt;
&lt;span style="color: #008000"&gt;//     Copyright (C) 2008 Interakting. All rights reserved&lt;/span&gt;
&lt;span style="color: #008000"&gt;// &amp;lt;/copyright&amp;gt;&lt;/span&gt;
&lt;span style="color: #008000"&gt;// &amp;lt;author&amp;gt;Dominic Zukiewicz&amp;lt;/author&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;
&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; LogicAndData;

&lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// The User class describes a single username/password combination. The password&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// is kept secret, but timescales limit security.&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; User
{
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// The username of the user.    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; username;

    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// The password of the user.    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;    &lt;/span&gt;
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; password;

    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Creates a user with a specific username/password combination    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;param name="username"&amp;gt;The username of the user.&amp;lt;/param&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;param name="password"&amp;gt;The password of the user.&amp;lt;/param&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; User(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; username, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; password)
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (username == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentNullException(Resources.Username_ParameterName);
        }
        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (username.Length == 0) 
        { 
            &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException(Resources.Username_ParameterEmpty, Resources.Username_ParameterName); 
        } 
        
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (password == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) 
        {
            &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentNullException(Resources.Password_ParameterName); 
        } 
        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (password.Length == 0)
        { 
            &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException(Resources.Password_ParameterEmpty, Resources.Password_ParameterName); 
        }
        
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.username = username; 
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.password = password;
    }

    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Gets the username of the user.    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Username 
    { 
        get 
        { 
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.username; 
        } 
    }
    
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Checks to see if the password of the user matches that of the stored password.    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;param name="guess"&amp;gt;The guess for the password.&amp;lt;/param&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if it matches, else &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&amp;lt;/returns&amp;gt;    &lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; CheckPassword(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; guess)
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (String.IsNullOrEmpty(guess))
        { 
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;; 
        }
        
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; guess.Equals(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.password, StringComparison.CurrentCulture);
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So what's changed - really?&lt;/p&gt;

&lt;p&gt;What I have noticed, is that there is a good separation of the code, a very clear layout structure and of course, forcing you to comment your methods, which I really like. You'll find that the code is clearer because of the white-space implied by the curly brackets.&lt;/p&gt;

&lt;p&gt;It can seem quite daunting to see all of these errors, but like FxCop, once you start to take these rules onboard, you are more likely to code in this style rather than turn away from it.&lt;/p&gt;

&lt;p&gt;Here is the link to Microsoft's StyleCop: &lt;a title="http://code.msdn.microsoft.com/sourceanalysis/Release/ProjectReleases.aspx?ReleaseId=1047" href="http://code.msdn.microsoft.com/sourceanalysis/Release/ProjectReleases.aspx?ReleaseId=1047"&gt;http://code.msdn.microsoft.com/sourceanalysis/Release/ProjectReleases.aspx?ReleaseId=1047&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/333.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/333.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/333.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/333.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Generating random numbers within SELECT queries in SQL Server 2005</title>
        <link rel="self" type="text/html" href="http://blogs.interakting.co.uk/dominicz/archive/2008/07/23/generating-random-numbers-within-select-queries-in-sql-server-2005.aspx" />
        <id>http://blogs.interakting.co.uk/dominicz/archive/2008/07/23/generating-random-numbers-within-select-queries-in-sql-server-2005.aspx</id>
        <published>2008-07-23T10:06:2901:00:00</published>
        <updated>2008-07-23T15:39:56Z</updated>
        <summary type="html">When using RAND() with SQL SELECT statements, the same number is generated for all rows. This is annoying if you are trying to create test data, but I've found a way to get around this. Read on for more information.</summary>
        <content type="html">&lt;p&gt;I was generating some test data and tried to use the RAND() function to query data. When trying this out on the Northwind database, it returned with the following result:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;SELECT&lt;/span&gt; LastName, RAND() &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; Employees&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;table border="1"&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th width="199"&gt;LastName&lt;/th&gt;
            &lt;th width="199"&gt;(No column name)&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Buchanan &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Callahan &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Davolio &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Dodsworth &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Fuller &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;King &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Leverling &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Peacock &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Suyama &lt;/td&gt;
            &lt;td&gt;0.747783 &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The problem is that the randomly generated number is the same for all rows. What I need is a function that will generate the random number differently each time this is called.&lt;/p&gt;
&lt;p&gt;After digging around, the easiest way I have found is to &lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Create a view that generates a random number &lt;/li&gt;
    &lt;li&gt;Create a function that calls the view &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here are the 2 statements I used:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;CREATE&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;VIEW&lt;/span&gt; RandomNumberView &lt;span style="color: rgb(0, 0, 255);"&gt;AS&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;BEGIN&lt;/span&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;SELECT&lt;/span&gt; RAND() &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; RandNum&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;GO&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;CREATE&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;FUNCTION&lt;/span&gt; GetRandomNumber() &lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;RETURNS&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;FLOAT&lt;/span&gt; &lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;AS&lt;/span&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;BEGIN&lt;/span&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;RETURN&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;SELECT&lt;/span&gt; RandNum &lt;span style="color: rgb(0, 0, 255);"&gt;FROM&lt;/span&gt; RandomNumberView&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;END&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Then all you do is call your function (with schema name) to return a new random number:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;SELECT&lt;/span&gt; LastName, dbo.GetRandomNumber() &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; Employees&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;table border="1"&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th width="199"&gt;LastName&lt;/th&gt;
            &lt;th width="199"&gt;(No column name)&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Buchanan &lt;/td&gt;
            &lt;td&gt;0.750829897796789 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Callahan &lt;/td&gt;
            &lt;td&gt;0.669015405308125 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Davolio &lt;/td&gt;
            &lt;td&gt;0.119630891365149 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Dodsworth &lt;/td&gt;
            &lt;td&gt;0.488104954627394 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Fuller &lt;/td&gt;
            &lt;td&gt;0.30583533779782 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;King &lt;/td&gt;
            &lt;td&gt;0.359980386714745 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Leverling &lt;/td&gt;
            &lt;td&gt;0.290999223959155 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Peacock &lt;/td&gt;
            &lt;td&gt;0.972126393998601 &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Suyama &lt;/td&gt;
            &lt;td&gt;0.388598769319124 &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;&lt;img src="http://blogs.interakting.co.uk/dominicz/aggbug/331.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blogs.interakting.co.uk/dominicz/comments/331.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blogs.interakting.co.uk/dominicz/comments/commentRss/331.aspx</wfw:commentRss>
        <trackback:ping>http://blogs.interakting.co.uk/dominicz/services/trackbacks/331.aspx</trackback:ping>
    </entry>
</feed>