<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://soapitstop.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>Community Server</title><link>http://soapitstop.com/blogs/</link><description>The platform that enables you to build rich, interactive communities</description><dc:language>en-US</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>Review of Concurrent Programming on Windows</title><link>http://soapitstop.com/blogs/concurrent-programmer/archive/2009/01/03/review-of-concurrent-programming-on-windows.aspx</link><pubDate>Sat, 03 Jan 2009 14:25:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:268</guid><dc:creator>bobcalco</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I am in the middle of &lt;a href="http://www.bluebytesoftware.com/blog/Default.aspx" title="Joe Duffy&amp;#39;s Blog" target="_blank"&gt;Joe Duffy&lt;/a&gt;&amp;#39;s &lt;a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html" title="Concurrent Programming on Windows home page" target="_blank"&gt;Concurrent Programming on Windows&lt;/a&gt;, but already I know I&amp;#39;m going to be blogging a lot about this book and its sublime subject matter here for some time.&lt;/p&gt;&lt;p&gt;First impression: It&amp;#39;s the best book about the internals of multi-threaded processing ever written. It&amp;#39;s the best book in this space because it&amp;#39;s both practical, hands-on with its examples, and fairly systematic in its coverage---without becoming dreadfully dull reading. Basically, he illustrates his ideas with real, and often very useful and lively code, rather than terse formulas or a barrage of academic points-of-interest. &lt;/p&gt;&lt;p&gt;I learned at least one thing I didn&amp;#39;t know --- namely, that despite supposedly heroic efforts Microsoft has not yet been able to expose fibers to .NET developers. This is appalling, actually, but Duffy covers them anyway, with a great example of how to write a user-mode scheduler leveraging fibers in C++. Kudos to him for that! &lt;br /&gt;&lt;/p&gt;&lt;p&gt;More than anything, he is thorough. If you don&amp;#39;t know what a semaphore is, or how monitors work, or why liveness and correctness are always at risk---if, architecturally speaking, you don&amp;#39;t clearly understand the trade offs between various concurrency strategies---you almost certainly will if you read this book. There is no longer an excuse for any developer, even one untrained in computer science, not to be able to wrap his or her head around multi-threaded programming. Like Prego--it&amp;#39;s in this book, and its quite digestible. (Yes, that was faint praise for Prego---but a sincere expression of enthusiasm for the book!)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;One thing I plan to do in this blog is take the concepts for a ride on different platforms, using different programming languages. I am currently considering a plan-of-attack, including a general-purpose, onoing example that will help bring the issues into focus. &lt;br /&gt;&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=268" width="1" height="1"&gt;</description></item><item><title>Recurring Events in SQL</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/12/12/recurring-events-in-sql.aspx</link><pubDate>Fri, 12 Dec 2008 23:02:48 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:267</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I recently had a little piece of SQL code that I thought I would post and it reminded me of something I did a long time ago (5 yrs) and thought I&amp;#39;d write something on it.&amp;#160; Anyway, first here&amp;#39;s the little piece without much celebration.&lt;/p&gt;  &lt;p&gt;This SQL code answers the question &amp;quot;What date is the third Wednesday in November 2008?&amp;quot;&amp;#160; This code could be wrapped in a function pretty easily.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @&lt;span class="kwrd"&gt;month&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;,@&lt;span class="kwrd"&gt;year&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;,@weekday &lt;span class="kwrd"&gt;int&lt;/span&gt;,@weekOffset &lt;span class="kwrd"&gt;int&lt;/span&gt;

&lt;span class="kwrd"&gt;set&lt;/span&gt; @&lt;span class="kwrd"&gt;month&lt;/span&gt;=11 &lt;span class="rem"&gt;-- (Month of date to check) &lt;/span&gt;
&lt;span class="kwrd"&gt;set&lt;/span&gt; @&lt;span class="kwrd"&gt;year&lt;/span&gt;=2008 &lt;span class="rem"&gt;-- (Year of date to check)&lt;/span&gt;
&lt;span class="kwrd"&gt;set&lt;/span&gt; @weekday=5 &lt;span class="rem"&gt;-- 1-7 (Sun-Sat)&lt;/span&gt;
&lt;span class="kwrd"&gt;set&lt;/span&gt; @weekOffset=1 &lt;span class="rem"&gt;-- 1-5 (Week of month)&lt;/span&gt;

&lt;span class="kwrd"&gt;declare&lt;/span&gt; @currentMonth datetime
&lt;span class="kwrd"&gt;declare&lt;/span&gt; @firstDay &lt;span class="kwrd"&gt;int&lt;/span&gt;
&lt;span class="kwrd"&gt;declare&lt;/span&gt; @newDate datetime

&lt;span class="rem"&gt;-- get the first day of the month&lt;/span&gt;
&lt;span class="kwrd"&gt;set&lt;/span&gt; @currentMonth=&lt;span class="kwrd"&gt;cast&lt;/span&gt;(&lt;span class="kwrd"&gt;cast&lt;/span&gt;(@&lt;span class="kwrd"&gt;month&lt;/span&gt; &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;char&lt;/span&gt;)+&lt;span class="str"&gt;&amp;#39;/1/&amp;#39;&lt;/span&gt;+&lt;span class="kwrd"&gt;cast&lt;/span&gt;(@&lt;span class="kwrd"&gt;year&lt;/span&gt; &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;char&lt;/span&gt;) &lt;span class="kwrd"&gt;as&lt;/span&gt; datetime)
&lt;span class="kwrd"&gt;set&lt;/span&gt; @firstDay=datepart(dw,@currentmonth)

&lt;span class="rem"&gt;-- Add the number of days from the first day&lt;/span&gt;
&lt;span class="kwrd"&gt;set&lt;/span&gt; @newDate=dateadd(dd,7*(@weekOffset-1)+
    &lt;span class="kwrd"&gt;case&lt;/span&gt; 
        &lt;span class="kwrd"&gt;when&lt;/span&gt; @weekday&amp;gt;=@firstDay &lt;span class="kwrd"&gt;then&lt;/span&gt; @weekday-@firstDay
        &lt;span class="kwrd"&gt;when&lt;/span&gt; @weekday&amp;lt;@firstDay &lt;span class="kwrd"&gt;then&lt;/span&gt; 7+@weekday-@firstDay
    &lt;span class="kwrd"&gt;end&lt;/span&gt;, @currentmonth)

&lt;span class="kwrd"&gt;select&lt;/span&gt; @newdate&lt;/pre&gt;

&lt;p&gt;The bigger idea I had was for recurring events in SQL, like for a calendar.&amp;#160; Assume the following structure of an event table&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; dbo.Events(
    ID &lt;span class="kwrd"&gt;int&lt;/span&gt; &lt;span class="kwrd"&gt;IDENTITY&lt;/span&gt;(1,1) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt; ,
    StartDate datetime,
    EndDate datetime,
    Title &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(255),
    Description &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(2000),
    RepeatValue &lt;span class="kwrd"&gt;char&lt;/span&gt;(1),
    RepeatInterval &lt;span class="kwrd"&gt;int&lt;/span&gt;
) &lt;/pre&gt;

&lt;p&gt;So you have a bunch of events defined that start and end on a range of dates.&amp;#160; The RepeatValue would be: D,W,M,Y for Day, Week, Month, Year.&amp;#160; The RepeatInterval would be the number of Days/Weeks/Months between repeats.&amp;#160; Add in some sample data&lt;/p&gt;

&lt;pre class="csharpcode"&gt;insert events &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;&amp;#39;1/1/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;12/31/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;Each Month All Year&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt;,1
insert events &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;&amp;#39;1/1/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;6/30/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;Bi-weekly Half Year&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt;,2
insert events &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;&amp;#39;7/1/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;12/31/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;Weekly Half Year&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt;,1
insert events &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;&amp;#39;2/1/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;3/1/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;Every other Day in Feb&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;D&amp;#39;&lt;/span&gt;,2
insert events &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;&amp;#39;7/4/08&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;12/31/2999&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;July 4th&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;&amp;#39;&lt;/span&gt;,&lt;span class="str"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt;,1&lt;/pre&gt;

&lt;p&gt;This is the stored proc I came up with to return the events for a given month:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;Procedure&lt;/span&gt; [dbo].[sp_ListRecurringCalendarEvents]
@viewdate datetime
&lt;span class="kwrd"&gt;as&lt;/span&gt;

&lt;span class="kwrd"&gt;set&lt;/span&gt; nocount &lt;span class="kwrd"&gt;on&lt;/span&gt;

&lt;span class="kwrd"&gt;declare&lt;/span&gt; @checkdate datetime

&lt;span class="kwrd"&gt;create&lt;/span&gt; &lt;span class="kwrd"&gt;table&lt;/span&gt; #tempCalendar(
    id &lt;span class="kwrd"&gt;int&lt;/span&gt;,
    startdate datetime,
    enddate datetime,
    title &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(255),
    recurrance &lt;span class="kwrd"&gt;bit&lt;/span&gt; &lt;span class="kwrd"&gt;default&lt;/span&gt; 0
)

&lt;span class="rem"&gt;-- Insert Standard events&lt;/span&gt;
insert #tempCalendar
&lt;span class="kwrd"&gt;select&lt;/span&gt; e.id,e.startdate,e.enddate,e.title,0
&lt;span class="kwrd"&gt;from&lt;/span&gt; events e
&lt;span class="kwrd"&gt;where&lt;/span&gt; datepart(mm,e.startdate)=datepart(mm,@viewdate) 
    &lt;span class="kwrd"&gt;and&lt;/span&gt; datepart(yy,e.startdate)=datepart(yy,@viewdate)
    &lt;span class="kwrd"&gt;and&lt;/span&gt; repeatinterval=0

&lt;span class="rem"&gt;-- Insert all recurrances&lt;/span&gt;
&lt;span class="kwrd"&gt;set&lt;/span&gt; @checkdate=&lt;span class="kwrd"&gt;cast&lt;/span&gt;(&lt;span class="kwrd"&gt;cast&lt;/span&gt;(datepart(mm,@viewdate) &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;char&lt;/span&gt;) 
    + &lt;span class="str"&gt;&amp;#39;/1/&amp;#39;&lt;/span&gt; 
    + &lt;span class="kwrd"&gt;cast&lt;/span&gt;(datepart(yy,@viewdate) &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;char&lt;/span&gt;) &lt;span class="kwrd"&gt;as&lt;/span&gt; datetime)

&lt;span class="rem"&gt;-- Loop through each day in month and see if it is a recurrance&lt;/span&gt;
&lt;span class="kwrd"&gt;while&lt;/span&gt; datepart(mm,@viewdate)=datepart(mm,@checkdate)
    &lt;span class="kwrd"&gt;begin&lt;/span&gt;
    insert #tempCalendar
    &lt;span class="kwrd"&gt;select&lt;/span&gt; e.id,
    &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;D&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(dd,datediff(dd,startdate,@checkdate),startdate)
     &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(ww,datediff(ww,startdate,@checkdate),startdate)
     &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(mm,datediff(mm,startdate,@checkdate),startdate)
     &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(yy,datediff(yy,startdate,@checkdate),startdate)
    &lt;span class="kwrd"&gt;end&lt;/span&gt;,
    e.enddate,e.title,
    &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; @checkdate=e.startdate &lt;span class="kwrd"&gt;then&lt;/span&gt; 0 &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
    &lt;span class="kwrd"&gt;from&lt;/span&gt; events e
    &lt;span class="kwrd"&gt;where&lt;/span&gt; e.RepeatInterval&amp;lt;&amp;gt;0 
        &lt;span class="kwrd"&gt;and&lt;/span&gt;
        &lt;span class="rem"&gt;-- Daily (Check to see if the number of day interval matches&lt;/span&gt;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;D&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; (&lt;span class="kwrd"&gt;cast&lt;/span&gt;(datediff(dd,startdate,@checkdate) &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt;)
            /&lt;span class="kwrd"&gt;cast&lt;/span&gt;(RepeatInterval &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt;)) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt; =
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;D&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; (datediff(dd,startdate,@checkdate)/RepeatInterval) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
        &lt;span class="kwrd"&gt;and&lt;/span&gt; 
        &lt;span class="rem"&gt;-- Weekly (check to see if the week interval matches and the weekday is the same)&lt;/span&gt;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; (&lt;span class="kwrd"&gt;cast&lt;/span&gt;(datediff(ww,startdate,@checkdate) &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt;)
            /&lt;span class="kwrd"&gt;cast&lt;/span&gt;(RepeatInterval &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt;)) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt; =
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; (datediff(ww,startdate,@checkdate)/RepeatInterval) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
            &lt;span class="kwrd"&gt;and&lt;/span&gt; &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; datepart(dw,startdate) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;    =
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; datepart(dw,@checkdate) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
        &lt;span class="kwrd"&gt;and&lt;/span&gt; 
        &lt;span class="rem"&gt;-- Monthly (check to see that the month interval is the same and the day of the month is the same)&lt;/span&gt;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; (&lt;span class="kwrd"&gt;cast&lt;/span&gt;(datediff(mm,startdate,@checkdate) &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt;)
            /&lt;span class="kwrd"&gt;cast&lt;/span&gt;(RepeatInterval &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt;)) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt; =
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; (datediff(mm,startdate,@checkdate)/RepeatInterval) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
            &lt;span class="kwrd"&gt;and&lt;/span&gt; &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; datepart(dd,startdate) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;    =
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; datepart(dd,@checkdate) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
        &lt;span class="kwrd"&gt;and&lt;/span&gt; 
        &lt;span class="rem"&gt;-- Yearly (Check to see if the day of the year is the same)&lt;/span&gt;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; datepart(dy,startdate) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt; =
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; datepart(dy,@checkdate) &lt;span class="kwrd"&gt;else&lt;/span&gt; 1 &lt;span class="kwrd"&gt;end&lt;/span&gt;
        &lt;span class="kwrd"&gt;and&lt;/span&gt; 
        e.startdate &amp;lt;= &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;D&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(dd,datediff(dd,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(ww,datediff(ww,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(mm,datediff(mm,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(yy,datediff(yy,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;end&lt;/span&gt; 
        &lt;span class="kwrd"&gt;and&lt;/span&gt; 
        e.enddate &amp;gt;= &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;D&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(dd,datediff(dd,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;W&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(ww,datediff(ww,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;M&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(mm,datediff(mm,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;when&lt;/span&gt; RepeatValue=&lt;span class="str"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;then&lt;/span&gt; dateadd(yy,datediff(yy,startdate,@checkdate),startdate)
            &lt;span class="kwrd"&gt;end&lt;/span&gt;

    &lt;span class="rem"&gt;-- Increment the date&lt;/span&gt;
    &lt;span class="kwrd"&gt;set&lt;/span&gt; @checkdate=dateadd(dd,1,@checkdate)

    &lt;span class="kwrd"&gt;end&lt;/span&gt;    

&lt;span class="rem"&gt;-- return the results&lt;/span&gt;
&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; #tempCalendar

&lt;span class="rem"&gt;-- Clean up&lt;/span&gt;
&lt;span class="kwrd"&gt;drop&lt;/span&gt; &lt;span class="kwrd"&gt;table&lt;/span&gt; #tempCalendar&lt;/pre&gt;

&lt;p&gt;The proc is called passing a date within the month to be displayed.&amp;#160; The results include the ID of the event (for linking), the start date of the event, the end date of the event (not so relevant), the event title, and whether this is a recurring event or not (in case you want to display it differently or disable editing of events that aren&amp;#39;t the original).&lt;/p&gt;

&lt;p&gt;So, many years later, I still find it intriguing that I was doing stuff like that.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=267" width="1" height="1"&gt;</description></item><item><title>Your Code Is Very Important To Us, Please Continue To Wait...</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/11/25/your-code-is-very-important-to-us-please-continue-to-wait.aspx</link><pubDate>Wed, 26 Nov 2008 00:41:52 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:264</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;It was asked of me once: &lt;em&gt;Why isn&amp;#39;t my &amp;quot;please wait&amp;quot; form doing anything?&lt;/em&gt;&amp;nbsp; The form had an animation, but the animation never moved.&amp;nbsp; The main form code would show the &amp;quot;please wait&amp;quot; form, run some code, then hide the &amp;quot;please wait&amp;quot; form.&amp;nbsp; As it turned out, the &amp;quot;please wait&amp;quot; form was being blocked by a database call and some other stuff in the main form.&amp;nbsp; There was never a chance for a DoEvents or redraw or anything.&lt;/p&gt; &lt;p&gt;The quick answer is, you have to show the other form in another thread, so it can operate freely and redraw itself.&amp;nbsp; The longer answer is, you have to deal with threading.&lt;/p&gt; &lt;p&gt;Threading is very easy in VB.NET, but some people want answers, not lessons, so I ended up writing this quick class to show a form in another thread.&amp;nbsp; It&amp;#39;s pretty simple.&amp;nbsp; You instantiate the class by passing in an instance of your &amp;quot;please wait&amp;quot; form , then call show(), do your work, then call hide().&amp;nbsp; Like so:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; f &lt;span class="kwrd"&gt;as&lt;/span&gt; N&lt;span class="kwrd"&gt;ew&lt;/span&gt; BackgroundForm(&lt;span class="kwrd"&gt;new&lt;/span&gt; frmWait)

f.Show()
&lt;span class="rem"&gt;&amp;#39; Do some stuff&lt;/span&gt;
f.Hide()&lt;/pre&gt;
&lt;p&gt;Only one caveat: you can&amp;#39;t have the variable for the BackgroundForm class at the class level.&amp;nbsp; It must be declared, instantiated, and used in the same method.&amp;nbsp; Otherwise, you will get Cross-Threading errors.&lt;/p&gt;
&lt;p&gt;So, here&amp;#39;s the class you can use for yourself, if you prefer answers to lessons.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; BackgroundForm
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; displayForm &lt;span class="kwrd"&gt;As&lt;/span&gt; Form
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; t &lt;span class="kwrd"&gt;As&lt;/span&gt; System.Threading.Thread
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; isActive &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Shared&lt;/span&gt; lockObject &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; f &lt;span class="kwrd"&gt;As&lt;/span&gt; Form)
        displayForm = f
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Show()
        t = &lt;span class="kwrd"&gt;New&lt;/span&gt; System.Threading.Thread(&lt;span class="kwrd"&gt;AddressOf&lt;/span&gt; ShowForm)
        t.IsBackground = &lt;span class="kwrd"&gt;True&lt;/span&gt;
        t.Start()
        isActive = &lt;span class="kwrd"&gt;True&lt;/span&gt;

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Hide()
        isActive = &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; ShowForm()
        displayForm.Show()

        &lt;span class="kwrd"&gt;Do&lt;/span&gt; &lt;span class="kwrd"&gt;While&lt;/span&gt; isActive &lt;span class="kwrd"&gt;AndAlso&lt;/span&gt; displayForm.Visible
            System.Threading.Thread.Sleep(100)
            &lt;span class="kwrd"&gt;SyncLock&lt;/span&gt; lockObject
            Application.DoEvents()
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;SyncLock&lt;/span&gt;
        &lt;span class="kwrd"&gt;Loop&lt;/span&gt;

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;
&lt;p&gt;I made a slight change to the code to protect against thread stomping.&amp;nbsp; If you have an animated GIF in your &amp;quot;please wait&amp;quot; form, and you have a more than one &amp;quot;please wait&amp;quot; form active at once, there is the chance you will get a &amp;quot;object is in use elsewhere&amp;quot; error.&amp;nbsp; This happens when the image for the current form is being changed - in the DoEvents - but another form is being closed/disposed, taking the same image with it.&lt;/p&gt;
&lt;p&gt;By wrapping the DoEvents in a SyncLock that is shared among all instances, this is prevented.&amp;nbsp; I couldn&amp;#39;t get the error to happen unless I opened over 20 windows and had them closing at scheduled times, but your luck may be worse.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=264" width="1" height="1"&gt;</description></item><item><title>Autosize.  No, not the control, the text.</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/11/16/autosize-no-not-the-control-the-text.aspx</link><pubDate>Sun, 16 Nov 2008 16:37:31 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:262</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;It&amp;#39;s nice that .NET controls have an auto-size property so you don&amp;#39;t have to worry about overflow and all.&amp;#160; But what about cases where you have a fixed layout?&amp;#160; Well, that&amp;#39;s simple, you turn autosize off and fix the control to the size you need.&lt;/p&gt;  &lt;p&gt;That&amp;#39;s half the story.&amp;#160; What about the text that&amp;#39;s inside it?&amp;#160; Now you know I&amp;#39;m talking to marketing people when I say that there are times you want the text to be as big as possible within that control.&amp;#160; But you can&amp;#39;t just set the font to a huge size, because sometimes you&amp;#39;ll have more text to display and the font size must sadly be reduced.&lt;/p&gt;  &lt;p&gt;To accommodate this, I made a quick method that brute-forces the correct font size in the control.&amp;#160; basically, stepping down the size of the font until it fits.&amp;#160; I know loops like this are cheap, poor programming, and I did give consideration to doing some hard math to calculate the proper font size based on the initial size, but sometimes not getting hung up on performance can be liberating.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; ResizeText(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; c &lt;span class="kwrd"&gt;As&lt;/span&gt; Control)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; currentSize &lt;span class="kwrd"&gt;As&lt;/span&gt; Size
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; currentFont &lt;span class="kwrd"&gt;As&lt;/span&gt; Font

        currentFont = c.Font

        &lt;span class="kwrd"&gt;Do&lt;/span&gt;
            currentSize = TextRenderer.MeasureText(c.Text, currentFont, _
                c.Size, TextFormatFlags.WordBreak)

            &lt;span class="kwrd"&gt;If&lt;/span&gt; currentSize.Width &amp;gt; (c.Width - c.Margin.Horizontal) _
                &lt;span class="kwrd"&gt;OrElse&lt;/span&gt; currentSize.Height &amp;gt; (c.Height - c.Margin.Vertical) &lt;span class="kwrd"&gt;Then&lt;/span&gt;

                currentFont = &lt;span class="kwrd"&gt;New&lt;/span&gt; Font(currentFont.FontFamily, _
                    &lt;span class="kwrd"&gt;CSng&lt;/span&gt;(currentFont.Size - 0.5), currentFont.Style, currentFont.Unit)
            &lt;span class="kwrd"&gt;Else&lt;/span&gt;
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Do&lt;/span&gt;

            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        &lt;span class="kwrd"&gt;Loop&lt;/span&gt; &lt;span class="kwrd"&gt;While&lt;/span&gt; currentFont.Size &amp;gt;= 1

        c.Font = currentFont

    &lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=262" width="1" height="1"&gt;</description></item><item><title>Casting Upwards</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/11/04/casting-upwards.aspx</link><pubDate>Wed, 05 Nov 2008 00:52:40 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:261</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;When binding business objects to a datagrid, often you have a need to display some information that is not directly exposed by the object itself.&amp;#160; Maybe it&amp;#39;s a calculated value, maybe it&amp;#39;s something nested deeper in the object.&amp;#160; When faced with this issue, there are a few different action paths you can take.&amp;#160; You can add extra read-only properties to your business object to support the extra view information.&amp;#160; You can create a new class that inherits from the class you are displaying and put the extra properties in there.&amp;#160; Or you can handle the CellFormatting event in the datagrid and change the displayed values manually.&amp;#160; One of the downsides of using a new derived class with extra properties is that you can&amp;#39;t cast a base class to it.&amp;#160; You could cast down to the base class, but no casting up. &lt;/p&gt;  &lt;p&gt;Here is a technique that is closest to the second option listed above and side-steps the upcasting problem.&amp;#160; I dislike the first option because it clutters the business object with UI-specific code.&amp;#160; Going with option 2 is only slightly better, while you can populate the correct display-specific object and return it from your business logic layer, either you have to have a method that return the derived type, or you will have to cast it to its correct type in the UI.&amp;#160; Even then, your business layer still contains UI logic. &lt;/p&gt;  &lt;p&gt;So, keeping things separated, the derived display-specific class should be defined in the UI layer.&amp;#160; This means it will have extra read-only properties for use with databinding.&amp;#160; The business layer will return the basic object(s), so it will be up to us to convert these to UI-friendly versions.&amp;#160; There are two problems with converting the object: not all the object state may be exposed via public properties, and those properties may contain logic.&amp;#160; It would be best to copy the object by its internal state - private variables. &lt;/p&gt;  &lt;p&gt;On first thought, working with the private variables means the code must be inside the source object and the destination object.&amp;#160; This would be tedious to do, passing in the destination object, then sending the source object&amp;#39;s private variables to the destination so the destination object can manipulate its own private variables.&amp;#160; Yuck.&amp;#160; However, using Reflection, the job gets a whole lot easier. &lt;/p&gt;  &lt;p&gt;Here&amp;#39;s a small class with a method to convert one class to another by copying its private and public fields.&amp;#160; The properties are intentionally excluded since they may contain logic that modifies the internal state.&amp;#160; You should use this technique with care and know exactly what it does and does not do.&amp;#160; Basically, it copies values from one instance of a class to another.&amp;#160; This is fine for simple classes, but it&amp;#39;s not going to resolve references for you. &lt;/p&gt;  &lt;p&gt;Consider ClassA with a private field of type ClassB.&amp;#160; ClassB maintains a private variable with a reference to ClassA, so that it can manipulate all of its &amp;quot;parent&amp;#39;s&amp;quot; state and logic.&amp;#160; If you use this technique to cast ClassA to ClassAA, because you want an extra property to display some info from ClassB, you&amp;#39;re in for some fun results if you change some data in ClassAA.&amp;#160; This is because ClassB still has a reference to ClassA, not ClassAA. &lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; UpCaster
    &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; CastUp(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; sourceObj &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; destinationObj &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt;)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; values &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Dictionary(Of &lt;span class="kwrd"&gt;String&lt;/span&gt;, &lt;span class="kwrd"&gt;Object&lt;/span&gt;)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; props() &lt;span class="kwrd"&gt;As&lt;/span&gt; Reflection.FieldInfo 

        props = sourceObj.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetFields(Reflection.BindingFlags.NonPublic _
            &lt;span class="kwrd"&gt;Or&lt;/span&gt; Reflection.BindingFlags.&lt;span class="kwrd"&gt;Static&lt;/span&gt; _
            &lt;span class="kwrd"&gt;Or&lt;/span&gt; Reflection.BindingFlags.Instance _
            &lt;span class="kwrd"&gt;Or&lt;/span&gt; Reflection.BindingFlags.&lt;span class="kwrd"&gt;Public&lt;/span&gt;) 

        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; p &lt;span class="kwrd"&gt;As&lt;/span&gt; Reflection.FieldInfo &lt;span class="kwrd"&gt;In&lt;/span&gt; props
            values.Add(p.Name, p.GetValue(sourceObj))
        &lt;span class="kwrd"&gt;Next&lt;/span&gt; 

        props = destinationObj.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetFields(Reflection.BindingFlags.NonPublic _
            &lt;span class="kwrd"&gt;Or&lt;/span&gt; Reflection.BindingFlags.&lt;span class="kwrd"&gt;Static&lt;/span&gt; _
            &lt;span class="kwrd"&gt;Or&lt;/span&gt; Reflection.BindingFlags.Instance _
            &lt;span class="kwrd"&gt;Or&lt;/span&gt; Reflection.BindingFlags.&lt;span class="kwrd"&gt;Public&lt;/span&gt;) 

        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; p &lt;span class="kwrd"&gt;As&lt;/span&gt; Reflection.FieldInfo &lt;span class="kwrd"&gt;In&lt;/span&gt; props
            &lt;span class="kwrd"&gt;If&lt;/span&gt; values.ContainsKey(p.Name) &lt;span class="kwrd"&gt;Then&lt;/span&gt; p.SetValue(destinationObj, values(p.Name))
        &lt;span class="kwrd"&gt;Next&lt;/span&gt; 

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; 

&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;&lt;/pre&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=261" width="1" height="1"&gt;</description></item><item><title>Zune XNA Example 2</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/09/07/zune-xna-example-2.aspx</link><pubDate>Mon, 08 Sep 2008 00:55:57 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:259</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;The last post was really long and I think I tried to do too much in one post.&amp;#160; So, here&amp;#39;s something smaller.&lt;/p&gt;  &lt;p&gt;In the last example, we created a custom class for a stack of cards.&amp;#160; One thing I want to point out about that class is that it had a shuffle method.&amp;#160; Said another way, the class was in charge of shuffling the cards inside it.&amp;#160; The XNA game that used the card stack didn&amp;#39;t contain the logic for shuffling the cards.&amp;#160; That&amp;#39;s one of the keys of creating objects - they take care of themselves.&amp;#160; To reinforce that concept, here&amp;#39;s a game that has no playability.&amp;#160; Here&amp;#39;s the game description:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;The game will display a dot on the screen.&amp;#160; The dot will be a random color and random size, and will appear in a random location.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Objects?&amp;#160; I only read &amp;quot;dot&amp;quot;. &amp;quot;Screen&amp;quot; is part of the game, so we already have that object built in. Properties?&amp;#160; Dot seems to have a color, size and location.&amp;#160; The term &amp;quot;random&amp;quot; refers to the value of those properties, so we don&amp;#39;t need to concern ourselves with using that as a property.&amp;#160; We will need to remember it when we write the code though.&amp;#160; Methods? None that are mentioned.&amp;#160; But the way we are going to write this, we are going to have the dot draw itself, without any involvement from the game.&amp;#160; So the dot knows where it is, what size it is, and what color it is.&amp;#160; It seems perfectly capable of drawing itself.&lt;/p&gt;  &lt;p&gt;Into the code.&amp;#160; This is our RandomDot class:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Graphics;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Content;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; DrawRandomDots
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; RandomDot:DrawableGameComponent
    {
        Random _rndm;
        Game _parent;
        SpriteBatch _spriteBatch;
        List&amp;lt;Color&amp;gt; _availableColors;
        Texture2D _dot;
                
        &lt;span class="kwrd"&gt;public&lt;/span&gt; RandomDot(Game game):&lt;span class="kwrd"&gt;base&lt;/span&gt;(game)
        {
            _rndm = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();
            _parent = game;
            _availableColors=&lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Color&amp;gt;();
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
        {
            _spriteBatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpriteBatch(_parent.GraphicsDevice);
            _dot=_parent.Content.Load&amp;lt;Texture2D&amp;gt;(&lt;span class="str"&gt;&amp;quot;Dot&amp;quot;&lt;/span&gt;);
            _availableColors.AddRange(&lt;span class="kwrd"&gt;new&lt;/span&gt; Color[]{Color.Blue,Color.DarkBlue,Color.DarkGreen,
                Color.DarkRed,Color.Green,Color.LightBlue,Color.Magenta,Color.Navy,
                Color.Orange,Color.Purple,Color.Red,Color.White,Color.Yellow});
              
            &lt;span class="kwrd"&gt;base&lt;/span&gt;.LoadContent();
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(GameTime gameTime)
        {
            Rectangle size;
            Color color;
                        
            size = &lt;span class="kwrd"&gt;new&lt;/span&gt; Rectangle(_rndm.Next(0, 240), _rndm.Next(0, 320), _rndm.Next(1, 10), _rndm.Next(1, 10));
            color = _availableColors[_rndm.Next(0, _availableColors.Count)];

            _spriteBatch.Begin();
            _spriteBatch.Draw(_dot, size, color);
            _spriteBatch.End();
            
            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Draw(gameTime);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;It&amp;#39;s not a lot of code.&amp;#160; Here&amp;#39;s the line-by-line:&lt;/p&gt;

&lt;p&gt;Line 1-5 (using...): These are namespaces we are going to be using in our class.&amp;#160; Because we put them in using statements, we don&amp;#39;t need to type all that out when we include the type name.&amp;#160; Notice I brought in some XNA namespaces, which is something we didn&amp;#39;t need in the CardStack class.&amp;#160; Because this class is actually going to be drawing, we will be using classes in these namespaces.&lt;/p&gt;

&lt;p&gt;Line 7 (namespace...): This is the namespace for our custom class.&amp;#160; Like I mentioned before, this is the name of the project I&amp;#39;m working in.&amp;#160; If you create a project under a different name, you need to update this to match.&lt;/p&gt;

&lt;p&gt;Line 9 (class...): This is the beginning of our class definition.&amp;#160; It&amp;#39;s called RandomDot.&amp;#160; The colon indicates we are inheriting from DrawableGameComponent, so we&amp;#39;re going to get a lot of code for free.&amp;#160; DrawableGameComponent is part of the Microsoft.XNA.Framework namespace, which is included in the using statements above.&lt;/p&gt;

&lt;p&gt;Line 11 (random...): This is the variable for the random number generator we&amp;#39;ll use for the color, location, and size.&amp;#160; Our game description specifically says random.&lt;/p&gt;

&lt;p&gt;Line 12 (Game...): This variable will hold the game that the dot is being included in.&amp;#160; I used the name &amp;quot;parent&amp;quot; to emphasize a point.&amp;#160; The dot class cannot exist on its own.&amp;#160; It is dependant on a game.&amp;#160; Without a game to draw the dot in, the dot class cannot be drawn.&amp;#160; Similar to how a person cannot be created without a parent, the dot class cannot be created without a game.&amp;#160; &lt;/p&gt;

&lt;p&gt;Line 13 (spritebatch...): This variable will hold the spritebatch that we draw to.&amp;#160; Because this class is going to handle drawing itself, we need a spritebatch to draw to.&lt;/p&gt;

&lt;p&gt;Line 14 (List...): This variable will hold the colors that the dot can be.&amp;#160; We will select one color at random from this list.&lt;/p&gt;

&lt;p&gt;Line 15 (Texture2D...): This variable will hold the graphic that we will draw to the spritebatch.&lt;/p&gt;

&lt;p&gt;Line 17 (public...): This is our constructor for the class.&amp;#160; Unlike the cardstack class, this class has only one constructor and it requires that a game object be passed into it.&amp;#160; By doing this, we enforce the rule that this class cannot exist without a game.&amp;#160; The colon and &amp;quot;base(game)&amp;quot; code show that the base class, which is DrawableGameComponent, needs to receive the game object as well.&amp;#160; If you do not include this on the constructor, you&amp;#39;ll get a &amp;quot;...does not contain a constructor that takes &amp;#39;0&amp;#39; arguments&amp;quot; error.&lt;/p&gt;

&lt;p&gt;Line 19 (_rndm...): We instantiate our random generator.&amp;#160; If we don&amp;#39;t, we get &amp;quot;object reference not set&amp;quot; errors.&amp;#160; This is the most common error you will probably experience, so it&amp;#39;s always important to ensure your objects get instantiated.&lt;/p&gt;

&lt;p&gt;Line 20 (_parent...): We are going to hold on to a reference to the parent game, because other methods in our class will need it.&amp;#160; So we store it in a variable.&lt;/p&gt;

&lt;p&gt;Line 21 (_availablecolors...): We instantiate our list so we can start adding colors into it.&lt;/p&gt;

&lt;p&gt;Line 24 (public...): This is a method that is provided by DrawableGameComponent, but we want to add additional code into it.&amp;#160; To do that, we use the keyword &amp;quot;override&amp;quot;.&amp;#160; That tells .NET: &amp;quot;when using this class, run this code instead of the usual code.&amp;quot;&amp;#160; In Visual Studio it&amp;#39;s easy to do overrides, you type in &amp;quot;override&amp;quot; and Intellisense gives you a list of methods you can override.&amp;#160; Pick one and the proper code is written for you.&lt;/p&gt;

&lt;p&gt;Line 26 (_spritebatch...): We&amp;#39;re in the LoadContent method, which only gets run once.&amp;#160; So we are going to create the objects that we need to draw the dot.&amp;#160; We need a spritebatch to draw to.&amp;#160; We can get this from the game that the dot belongs to, if we had passed it in from the game, or we can create a brand new one from the GraphicsDevice of the game.&amp;#160; We&amp;#39;re going to to the latter, but there are drawbacks to doing it this way.&amp;#160; For such a simple game, it&amp;#39;s not a concern.&amp;#160; Notice we use the _parent variable to reference the game that the dot belongs to.&lt;/p&gt;

&lt;p&gt;Line 27 (_dot...): Here we load the image that will be our dot.&amp;#160; Again, we are using the functionality the game provides us.&amp;#160; This is a single pixel image called dot.png.&lt;/p&gt;

&lt;p&gt;Line 28-30 (_availablecolors...): Here we are assigning the colors to the list.&amp;#160; This is a shorthand way of putting in a bunch of items into a list.&amp;#160; The code &amp;quot;new Color[]{... ... ...}&amp;quot; creates an array of colors.&amp;#160; That array isn&amp;#39;t assigned to any variable.&amp;#160; That code is within the statement &amp;quot;.AddRange()&amp;quot;, so the array is sent into the AddRange method which adds all the array items into the list.&amp;#160; This is another case where you need to read the line from inside out.&lt;/p&gt;

&lt;p&gt;Line 32 (base...): In line 24, I said that we are replacing the usual code with our own using &amp;quot;override&amp;quot;.&amp;#160; But DrawableGameComponent has a lot of functionality in it for this method already.&amp;#160; We don&amp;#39;t want to have to rewrite it all; we want to use it.&amp;#160; In this line we are calling the LoadContent method in DrawableGameComponent, so we&amp;#39;re not losing anything.&amp;#160; So what we&amp;#39;ve done it not so much replace the functionality of LoadContent, but more enhance it with additional code.&lt;/p&gt;

&lt;p&gt;Line 35 (public...): Here&amp;#39;s another case where we want to inject additional code into the generic DrawableGameComponent.&amp;#160; So we override the Draw method also.&amp;#160; The Draw method has a parameter for the current game time.&amp;#160; When we override it, we have to include that parameter also.&amp;#160; When we override, the parameters must be just like the base class.&lt;/p&gt;

&lt;p&gt;Line 37 (Rectangle...): We need a variable to hold the size and location of the dot.&amp;#160; The rectangle class will do both for us together.&amp;#160; If we just wanted location, we could use Vector2&lt;/p&gt;

&lt;p&gt;Line 38 (Color...): This variable will hold the color of the dot.&amp;#160; Sometimes in the code, you&amp;#39;ll see some shorthand versions used and others we&amp;#39;ll assign the value to a variable when we use it.&amp;#160; If we&amp;#39;re going to assign it to a variable, it&amp;#39;s usually because we need to use that value more than once, or by using an extra variable, we add clarity to the code.&amp;#160; In this case, we are doing the latter.&lt;/p&gt;

&lt;p&gt;line 40 (size=...): Here we create the rectangle to hold the location and size of the dot.&amp;#160; Notice the X and Y coordinates are the width and height of the screen and the width and height of the rectangle are 1 through 10.&amp;#160; Each value is randomly selected.&lt;/p&gt;

&lt;p&gt;Line 41 (color=...): Here we pick a color at random from the list of available colors.&amp;#160; Notice the upper limit of the random.Next statement is the count of the list.&amp;#160; This means we can add or remove colors from the list without having to change this line.&amp;#160; If we hard-coded 10 in there (because we put 10 colors in the list), we&amp;#39;d have to change it if we altered the size of the available colors list.&amp;#160; Is this hypocritical that the width and height used in the screen size in line 40 are hard-coded values yet I&amp;#39;m saying hard-coded values are bad?&amp;#160; Absolutely.&amp;#160; You can get the current screen size from XNA, and maybe we&amp;#39;ll do that later.&lt;/p&gt;

&lt;p&gt;Line 43 (spritebatch...): Before writing to a spritebatch, we need to call Begin on it.&lt;/p&gt;

&lt;p&gt;Line 44 (spritebatch...):&amp;#160; Here we draw the dot texture at the specific location in the specific color.&amp;#160; Notice in line 38 how I mentioned clarity?&amp;#160; Imagine replacing the variables with all the code from line 40 and 41.&amp;#160; The extra variables are worth it.&amp;#160; Also, notice we are specifying what color to draw the dot?&amp;#160; But our dot is an image and it has a color already, right?&amp;#160; Yes, but if your image is just white, the color parameter tints the image in the color you specify.&amp;#160; So you can have a solid color image and draw it in any color as long as the original texture is white.&amp;#160; So you don&amp;#39;t need a green dot, a red dot, a blue dot.&amp;#160; You just make a white dot and draw it in the color you want.&lt;/p&gt;

&lt;p&gt;Line 45 (spritebatch...): When we are done drawing to a spritebatch, we need to call End.&lt;/p&gt;

&lt;p&gt;Line 47 (base...): Like line 32, we want the regular code for the Draw method of DrawableGameComponent to run as well, so we call that here.&amp;#160; You don&amp;#39;t have to always have this line at the end.&amp;#160; You may want the base code to run first or somewhere in the middle of your additional code.&amp;#160; In these cases, we run it last.&lt;/p&gt;

&lt;p&gt;So now you have a class that draws itself.&amp;#160; You&amp;#39;ll see that in the game code, there is actually very little we have to write now.&amp;#160; Here&amp;#39;s the code for the game:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Content;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Graphics;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Input;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Storage;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; DrawRandomDots
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        RandomDot dot;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; Game1()
        {
            graphics = &lt;span class="kwrd"&gt;new&lt;/span&gt; GraphicsDeviceManager(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
            Content.RootDirectory = &lt;span class="str"&gt;&amp;quot;Content&amp;quot;&lt;/span&gt;;
            dot = &lt;span class="kwrd"&gt;new&lt;/span&gt; RandomDot(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.Components.Add(dot);
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
        {
            spriteBatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpriteBatch(GraphicsDevice);
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                &lt;span class="kwrd"&gt;this&lt;/span&gt;.Exit();

            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(gameTime);
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Draw(gameTime);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;Once again, I&amp;#39;ll only discuss the additions I made to the Zune game template.&amp;#160; I added a dot.png to the Content folder of the project.&amp;#160; The image is just a single white pixel.&lt;/p&gt;

&lt;p&gt;Line 15 (RandomDot...): Here is the variable for our custom class.&amp;#160; We&amp;#39;re only going to have one for right now.&lt;/p&gt;

&lt;p&gt;Line 21 (dot=...): We need to instantiate our class so we can use it.&amp;#160; We pass in the current game in the constructor by using &amp;quot;this&amp;quot;.&lt;/p&gt;

&lt;p&gt;Line 22 (this...): This is something new.&amp;#160; We are going to add the random dot as a component of the game.&amp;#160; This means that it will get drawn automatically for us.&amp;#160; If you think of any normal application, like Microsoft Word, can you imagine how hard it would be to write the main program if you had to maintain all the little pieces like menus, toolbars, rulers, status bars, popup messages?&amp;#160; Instead, these individual pieces are built independently and they manage themselves.&amp;#160; That&amp;#39;s what you have done.&amp;#160; You have built a component that manages itself.&lt;/p&gt;

&lt;p&gt;That&amp;#39;s it.&amp;#160; Only three changes.&amp;#160; Well, I changed the background color to black in line 40, too.&amp;#160; But the point is, because the RandomDot class handles drawing itself, the main program does not need to.&amp;#160; As you make games that have lots of different parts, you will be crushed under the weight of the code in the Draw method is you try to do it all in the main game class.&amp;#160; So when you&amp;#39;re writing a card game, the cards should draw themselves.&amp;#160; But wait, how will the cards know where to be drawn?&amp;#160; If there&amp;#39;s five cards, how will they know about each other so they can choose a location that doesn&amp;#39;t overlap with each other?&amp;#160; We&amp;#39;ll do that in a later post, but the quick answer is: another class that has a list of cards inside it.&amp;#160; That class knows about the five cards.&amp;#160; The cards don&amp;#39;t know and will never know about each other.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=259" width="1" height="1"&gt;</description></item><item><title>A Zune Game Sample in C#</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/09/06/a-zune-game-sample-in-c.aspx</link><pubDate>Sat, 06 Sep 2008 21:45:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:257</guid><dc:creator>anachostic</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I will start out with a disclaimer: I am not a C# programmer; I am a VB programmer.&amp;#160; I have heard countless people say, &amp;quot;Why don&amp;#39;t you just learn C# and be done with it.&amp;#160; They&amp;#39;re just the same.&amp;quot;&amp;#160; After this little experiment, I&amp;#39;m a little more convinced, but I am certainly more productive in VB, so that&amp;#39;s where I will stay.&lt;/p&gt;  &lt;p&gt;So what is the purpose of this post (and likely future posts)?&amp;#160; Because I have a need to share knowledge and help others, and there is a need in the Zune community to get people up to speed with XNA programming.&amp;#160; As previous posts mention, I&amp;#39;m doing Zune programming in VB.&amp;#160; I realize this is not for everyone, least of all beginners.&amp;#160; So I will apply some concepts that are universal to .NET programming in a C# format in order to promote some skill building.&lt;/p&gt;  &lt;p&gt;The format I am going to use is to put out all the code first, then walk through it line-by-line in excruciating detail.&amp;#160; This way, you can hopefully skip over lines that you understand and just jump to a concept that is unfamiliar.&amp;#160; So scan through the full code listing, mentally compiling it and visualizing different parameters being passed and what the code path would be.&amp;#160; If you aren&amp;#39;t sure what the effect of a line would be, check the detail for that line.&lt;/p&gt;  &lt;p&gt;Ok.&amp;#160; The first sample I could think of, because of elevated interest in it, is cards.&amp;#160; This sample mixes a bit of basic programming with a sample of &amp;quot;what&amp;#39;s the best way to...&amp;quot;.&amp;#160; The sample is pretty basic, but can be improved upon rather easily because it&amp;#39;s going to be built with planned expansion.&lt;/p&gt;  &lt;p&gt;First some general terms and some design discussion.&amp;#160; The term &amp;quot;class&amp;quot; refers to a code structure.&amp;#160; A class should define something of interest to your program.&amp;#160; When you talk about your program to other people, the nouns you use in the description are excellent candidates for classes.&amp;#160; Consider the statement, &amp;quot;My game has a car driving around a course dodging potholes and barricades&amp;quot;.&amp;#160; Thinking in code, you should immediately think: &amp;quot;I need classes for car, course, pothole, and barricade.&amp;quot;&amp;#160; &lt;/p&gt;  &lt;p&gt;The term &amp;quot;property&amp;quot; refers to a piece of information contained in the class.&amp;#160; This is data that makes the instance of the class unique.&amp;#160; These are the adjectives in your description.&amp;#160; The statement: &amp;quot;The player&amp;#39;s car can be red or blue and can be upgraded with weapons.&amp;quot;&amp;#160; You should be visualizing that the car class needs at least two properties: color and weapons.&amp;#160; When visualizing properties, don&amp;#39;t consider the values of the properties - like red and blue - think of how to store the values.&lt;/p&gt;  &lt;p&gt;Finally, &amp;quot;method&amp;quot; refers to an action that the class can perform, either on itself or on another instance of a class.&amp;#160; When describing games, these are the verbs.&amp;#160; &amp;quot;The car can speed up, turn left and right, and brake.&amp;quot;&amp;#160; You should be thinking &amp;quot;accelerate, turn, brake&amp;quot; for methods on the car class.&lt;/p&gt;  &lt;p&gt;Now, that you can translate descriptions into classes, you are now officially a software architect.&amp;#160; Welcome to the world of software design.&amp;#160; Here&amp;#39;s the description of the &amp;quot;game&amp;quot; we are going to create:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;This game has a deck of cards.&amp;#160; You will draw a card and the value will be displayed to you.&amp;#160; After all the cards have been drawn, the deck is reshuffled and the first card is drawn again. That&amp;#39;s it.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;When I read that, I think the objects should be: card and card deck.&amp;#160; The properties of card should be: value.&amp;#160; The methods of card deck should be: shuffle.&amp;#160; What about having &amp;quot;display&amp;quot; on the card class?&amp;#160; In this case, we are going to assign all display duties to the game class.&amp;#160; And XNA has a method for that: Draw().&amp;#160; That doesn&amp;#39;t mean you will always do it that way.&amp;#160; Maybe in a future revision I&amp;#39;ll illustrate the difference.&lt;/p&gt;  &lt;p&gt;Hopefully you interpreted the description the same as I did, because that&amp;#39;s how we&amp;#39;re going to build it.&amp;#160; In this first example, the card class is going to be handled by String.&amp;#160; So all of our cards are going to be strings.&amp;#160; Later we&amp;#39;ll make it a class of its own.&amp;#160; The class we are going to focus on is the card deck.&amp;#160; &lt;/p&gt;  &lt;p&gt;When you think of a deck of cards, you can draw parallels to lots of programming concepts: arrays, lists, collections, but the most appropriate programming concept is the Stack.&amp;#160; A stack is an auto-sizing array where you can only put things on and take things off.&amp;#160; You can only get at the last thing put on the stack, just like you can only take off the last card put on a deck of cards.&amp;#160; It&amp;#39;s a perfect fit.&amp;#160; .NET gives us a Stack class that we can inherit from and put in our extra method: shuffle.&amp;#160; Here&amp;#39;s the code:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CardsSample
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; CardStack&amp;lt;T&amp;gt; : Stack&amp;lt;T&amp;gt;
    {
        Random rndm;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; CardStack()
        {
            rndm = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; CardStack(CardStack&amp;lt;T&amp;gt; cards)
        {
            rndm = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();

            &lt;span class="kwrd"&gt;while&lt;/span&gt; (cards.Count &amp;gt; 0)
            {
                &lt;span class="kwrd"&gt;this&lt;/span&gt;.Push(cards.Pop());
            }
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Shuffle()
        {
            List&amp;lt;T&amp;gt; cards = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;T&amp;gt;();
            T currentCard;
            &lt;span class="kwrd"&gt;int&lt;/span&gt; randomIndex;

            &lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.Count &amp;gt; 0)
            {
                cards.Add(&lt;span class="kwrd"&gt;this&lt;/span&gt;.Pop());
            }

            &lt;span class="kwrd"&gt;while&lt;/span&gt; (cards.Count &amp;gt; 0)
            {
                randomIndex = rndm.Next(0, cards.Count);
                currentCard = cards[randomIndex];
                cards.RemoveAt(randomIndex);

                &lt;span class="kwrd"&gt;this&lt;/span&gt;.Push(currentCard);
            }

        }
    }
}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s the explanation:&lt;/p&gt;

&lt;p&gt;Line 1&amp;amp;2 (using ...): We have using statements to reduce the amount of code we have to type.&amp;#160; If we didn&amp;#39;t have them, every time we defined a Random variable, we would have to use System.Random, and instead of typing Stack&amp;lt;T&amp;gt;, it would be System.Collections.Generic.Stack&amp;lt;T&amp;gt;.&amp;#160; &lt;/p&gt;

&lt;p&gt;Line 4 (namespace...): Classes have namespaces to help organize the code library.&amp;#160; If we didn&amp;#39;t have a unique namespace and we just happened to call our class &amp;quot;Stack&amp;quot;, how would .NET know whether any variable of type &amp;quot;Stack&amp;quot; was ours or the one in System.Collections.Generic?&amp;#160; Yes, that&amp;#39;s right.&amp;#160; The using statements are namespaces.&lt;/p&gt;

&lt;p&gt;Line 6 (class...): This is the beginning of the definition of our class.&amp;#160; There are a couple of elements on this line that are not going to be something you use all the time.&amp;#160; The first is &amp;lt;T&amp;gt;.&amp;#160; This makes our class a Generic class that can be used for any data type.&amp;#160; Big-picture, we want this to be a stack of cards, but right now, we are using strings as our cards.&amp;#160; This class could have been written using &amp;lt;string&amp;gt; instead of &amp;lt;T&amp;gt;, but when we converted it to work with our later card class, we&amp;#39;d have to change it all again.&amp;#160; My making it generic, we can use strings, cards, or anything else.&amp;#160; The other element is the colon and the class following it.&amp;#160; This indicates that our class is inheriting from Stack&amp;lt;T&amp;gt;.&amp;#160; We get a lot of pre-built functionality from Stack, and the &amp;lt;T&amp;gt; designation means that however we define our class (strings, cards, whatever), the Stack class we are sitting on top of will be the same.&lt;/p&gt;

&lt;p&gt;line 8 (Random...): This is the definition of our random number generator we&amp;#39;ll use when shuffling.&amp;#160; This variable is only seen and used inside the class, and that&amp;#39;s how we want it.&amp;#160; No other class should care about the deck&amp;#39;s random number generator.&lt;/p&gt;

&lt;p&gt;Line 10 (public...): This is our constructor for the class.&amp;#160; When another piece of code calls &amp;quot;New CardStack&amp;lt;string&amp;gt;()&amp;quot;, this is the code that runs.&amp;#160; The term &amp;quot;constructor&amp;quot; means the method that is called when creating a new instance of a class.&lt;/p&gt;

&lt;p&gt;Line 12 (rndm =): This is where we instantiate the random class.&amp;#160; If we didn&amp;#39;t do that, the shuffle method would error with an &amp;quot;Object reference not set&amp;quot; error.&amp;#160; Just by declaring a variable doesn&amp;#39;t always mean it&amp;#39;s ready to use.&lt;/p&gt;

&lt;p&gt;Line 15 (public ...): This is another constructor, but this one takes a parameter.&amp;#160; If you&amp;#39;ve ever created a new class and Visual Studio shows the yellow help with (1 of 15), showing all the different ways you can create a new object, that&amp;#39;s what we&amp;#39;re doing here.&amp;#160; If you write code new &amp;quot;CardStack&amp;lt;string&amp;gt;(&amp;quot;, you will be prompted for either of the two ways to create it, either empty, by passing no parameters, or filled, by passing in another CardStack instance.&amp;#160; Why would we want to create a filled CardStack?&amp;#160; That will be shown in the game code.&lt;/p&gt;

&lt;p&gt;Line 17 (rndm...): Just like Line 12&lt;/p&gt;

&lt;p&gt;Line 19 (while...): The constructor is there to fill the stack with items from another stack.&amp;#160; The while loop will do this.&amp;#160; Notice we are looking at &amp;quot;cards.count&amp;quot;.&amp;#160; What is &amp;quot;cards&amp;quot; and how do we know we can use it?&amp;#160;&amp;#160; This is the parameter we passed in: its name is cards and it&amp;#39;s type is CardStack&amp;lt;T&amp;gt;.&amp;#160; &amp;quot;cards&amp;quot; is only usable within this method.&amp;#160; If we edit line 15 and change &amp;quot;cards&amp;quot; to some other name, we also need to change it on this line so it is referenced properly.&lt;/p&gt;

&lt;p&gt;Line 21 (this.push...): This looks like magic to a first-timer.&amp;#160; The key is to read it from the inside out.&amp;#160; Inside the parentheses, the statement &amp;quot;cards.Pop()&amp;quot; will return the top item off the card stack that was passed in.&amp;#160; You might think you need to assign it to a variable first, but you don&amp;#39;t.&amp;#160; So right now, we have a card floating in limbo. The statement outside that, &amp;quot;this.Push()&amp;quot;, captures that card and puts it on the top of the stack for the current class instance.&amp;#160; The keyword &amp;quot;this&amp;quot; always means the current instance of the class.&amp;#160; It makes sense that you would need that when you have 10 instances of the CardStack class going at once.&lt;/p&gt;

&lt;p&gt;Line 25 (public void...): This is our shuffle method.&amp;#160; Because we used the keyword &amp;quot;public&amp;quot;, other code can see it and execute it.&amp;#160; Because we used &amp;quot;void&amp;quot;, other code knows that nothing is coming back when this method gets called.&amp;#160; The method definition doesn&amp;#39;t specify any parameters.&lt;/p&gt;

&lt;p&gt;line 27 (List&amp;lt;T&amp;gt;...): This variable is going to temporarily hold all the items in the stack.&amp;#160; Remember, we can only access the last item on a stack, so that&amp;#39;s going to seriously limit how random we can be.&amp;#160; A list however, can have any of its items accessed at any time.&amp;#160; So that&amp;#39;s what we&amp;#39;ll use.&amp;#160; The &amp;lt;T&amp;gt; designation means to use the exact same type that was used when we defined the cardstack class - string, card, whatever.&amp;#160; We are also creating a new instance of the list on the same line.&amp;#160; Since we are going to use it right away, we can do this.&lt;/p&gt;

&lt;p&gt;Line 28 (T ...): This might look a little odd because there is no data type T.&amp;#160; But this is actually a reference to the &amp;lt;T&amp;gt; we&amp;#39;ve been using all over the place.&amp;#160; Whatever we use when we create CardStack&amp;lt;T&amp;gt; gets used here.&amp;#160; CardStack&amp;lt;string&amp;gt; defines currentCard as string, CardStack&amp;lt;card&amp;gt; would define currentCard as card.&lt;/p&gt;

&lt;p&gt;Line 29 (int...): This is used to hold a random number when we pull from the temporary list.&amp;#160; We can&amp;#39;t do magic like on line 21, because we need this number on two different lines.&lt;/p&gt;

&lt;p&gt;Line 31 (while...): We&amp;#39;re going to loop through all the items in the current stack and put them in the temporary list.&amp;#160; We&amp;#39;ll keep pulling items out until its empty.&amp;#160; There are lots of other clever ways to do this and later code will probably use them.&lt;/p&gt;

&lt;p&gt;Line 32 (cards.add...): This is just like line 21, except instead of adding to a stack, we&amp;#39;re adding to a list.&amp;#160; It&amp;#39;s nice that a lot of classes use the same method names.&lt;/p&gt;

&lt;p&gt;line 36 (while...): Instead of looping through the internal collection, we&amp;#39;re going to loop through the temporary collection.&amp;#160; We&amp;#39;re going to keep looping through it until its contents have been emptied back into the stack.&lt;/p&gt;

&lt;p&gt;line 38 (randomindex...): We get a random number somewhere between 0 and the number of items in the temporary list.&amp;#160; Every time we loop, that count is going to drop by one, so we need to evaluate it each time.&amp;#160; That&amp;#39;s why we don&amp;#39;t get the count of temporary items before we start looping.&amp;#160; So it will be 0 through 10, then 0 through 9, and so on.&lt;/p&gt;

&lt;p&gt;Line 39 (currentcard=...): This is where we get a random card (or string) from the temp list based on the random number we got.&lt;/p&gt;

&lt;p&gt;line 40 (cards.removeat...): We want to remove the selected item from the temporary list so we don&amp;#39;t get it again.&amp;#160; Don&amp;#39;t worry about losing it, we still have a reference of it in currentCard.&amp;#160; But we need to do something with currentCard, because that&amp;#39;s the last place we can find that card.&lt;/p&gt;

&lt;p&gt;Line 42 (this.push...): And now we&amp;#39;ve secured the card in the current CardStack.&amp;#160; We&amp;#39;ve put the card on the top of the stack, so if we did a .Pop() right now, we would pull it back off.&lt;/p&gt;

&lt;p&gt;So if the stack had 1,2,3,4,5 in it, a pop would pull off 5.&amp;#160; To shuffle, we popped off all the items and put them in a list.&amp;#160; The list looks like 5,4,3,2,1 because we pop from the stack backwards.&amp;#160; Then depending on the numbers from the random generator, the stack may have any order of values because we pulled from the list in a random order, never exceeding the length of the list.&lt;/p&gt;

&lt;p&gt;Wow, that was exhausting.&amp;#160; But we&amp;#39;re not done yet.&amp;#160; Now that we understand the cards are going to be strings, and the deck of cards is based on a Stack class, we can build the game.&amp;#160; Writing an XNA game is different from writing a Windows application.&amp;#160; Windows apps do nothing until something is done to them.&amp;#160; That&amp;#39;s event-driven programming.&amp;#160; XNA games are running all the time, constantly checking for input and drawing the screen.&amp;#160; It takes a different mindset to design a game.&amp;#160; I&amp;#39;m certainly not going to say I&amp;#39;m great at game writing, because I tend to shoehorn in event-driven concepts into the process.&amp;#160; Regardless, let&amp;#39;s see the game code.&amp;#160; A lot of this is boilerplate from the XNA template, so I&amp;#39;ll only highlight the lines that I added:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Audio;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Content;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Graphics;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Input;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Media;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Xna.Framework.Storage;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CardsSample
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        CardStack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; drawCards;
        CardStack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; discardCards;
        &lt;span class="kwrd"&gt;string&lt;/span&gt; currentCard;
        SpriteFont font;
        &lt;span class="kwrd"&gt;bool&lt;/span&gt; isButtonPressed;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; Game1()
        {
            graphics = &lt;span class="kwrd"&gt;new&lt;/span&gt; GraphicsDeviceManager(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
            Content.RootDirectory = &lt;span class="str"&gt;&amp;quot;Content&amp;quot;&lt;/span&gt;;

            drawCards = &lt;span class="kwrd"&gt;new&lt;/span&gt; CardStack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();
            discardCards = &lt;span class="kwrd"&gt;new&lt;/span&gt; CardStack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Initialize()
        {
            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Initialize();
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
        {
            spriteBatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpriteBatch(GraphicsDevice);

            font = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;MainFont&amp;quot;&lt;/span&gt;);

            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 10; i++)
            {
                drawCards.Push(&lt;span class="str"&gt;&amp;quot;Card &amp;quot;&lt;/span&gt; + i.ToString());
            }

            drawCards.Shuffle();
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
        {           
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                &lt;span class="kwrd"&gt;this&lt;/span&gt;.Exit();

            &lt;span class="kwrd"&gt;if&lt;/span&gt; ((!isButtonPressed) &amp;amp;&amp;amp; (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed))
            {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; ((drawCards.Count == 0) &amp;amp;&amp;amp; (discardCards.Count &amp;gt; 0))
                {
                    drawCards = &lt;span class="kwrd"&gt;new&lt;/span&gt; CardStack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;(discardCards);
                    drawCards.Shuffle();
                }

                currentCard = drawCards.Pop();
                discardCards.Push(currentCard);
                
                isButtonPressed = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
            }
            
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (GamePad.GetState(PlayerIndex.One).Buttons.A != ButtonState.Pressed)
                isButtonPressed = &lt;span class="kwrd"&gt;false&lt;/span&gt;;

            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(gameTime);
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(GameTime gameTime)
        {
            &lt;span class="kwrd"&gt;string&lt;/span&gt; msg;

            msg = &lt;span class="str"&gt;&amp;quot;Your card is {0}.&amp;quot;&lt;/span&gt; + Environment.NewLine   
                + &lt;span class="str"&gt;&amp;quot;There are {1} cards remaining&amp;quot;&lt;/span&gt; + Environment.NewLine  
                + &lt;span class="str"&gt;&amp;quot;and {2} cards have been drawn.&amp;quot;&lt;/span&gt;;

            graphics.GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
           
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (currentCard != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
                spriteBatch.DrawString(font, &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(msg, currentCard, drawCards.Count, discardCards.Count), 
                     &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(20, 100), Color.White);
            }&lt;span class="kwrd"&gt;else&lt;/span&gt;
            {
                spriteBatch.DrawString(font, &lt;span class="str"&gt;&amp;quot;Press Center to draw card.&amp;quot;&lt;/span&gt;, 
                       &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(20, 100), Color.White);
            }
            
            spriteBatch.End();

            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Draw(gameTime);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;Line 18 (CardStack...): We get to use our custom class.&amp;#160; This is going to hold the cards that we pull from.&amp;#160; Notice we actually specify a real type - string - in the definition.&amp;#160; When we convert to using a real class for our cards, we only need to change the definition here.&amp;#160; We don&amp;#39;t need to touch the CardStack class at all.&lt;/p&gt;

&lt;p&gt;Line 19 (cardStack...): Another of our classes.&amp;#160; This is going to hold the cards after we pull from the draw stack.&amp;#160; Think of two piles of cards (two &lt;em&gt;stacks&lt;/em&gt; of cards, ahem).&amp;#160; That&amp;#39;s what we have in these two variables.&lt;/p&gt;

&lt;p&gt;line 20 (string...): This is going to be the variable for our currently-drawn card.&amp;#160; We pop from the draw stack and put it into current card and push it onto the discard stack.&lt;/p&gt;

&lt;p&gt;Line 21 (spritefont...): We need a font to display our text.&amp;#160; To add a font, right-click the Content item in Visual Studio&amp;#39;s Solution Explorer and Add New item, choose SpriteFont and give it the name MainFont, then edit the FontName field in the SprinteFont file to say Arial, and the Size to 12.&lt;/p&gt;

&lt;p&gt;Line 22 (bool...): This variable is used to remember when the draw button is pressed.&amp;#160; Remember, XNA is always checking the input and the way our code is, we only want to draw a card when the button is initially pressed, not every time XNA checks to see the button is pressed.&lt;/p&gt;

&lt;p&gt;line 29/30 (drawcards...): We instantiate the variables that will hold our cards.&amp;#160; Like the random variable in our CardStack, if we don&amp;#39;t, we&amp;#39;ll get &amp;quot;Object Reference Not Set&amp;quot; errors.&lt;/p&gt;

&lt;p&gt;Line 42 (font=...): We load the font from the Content manager.&amp;#160; The name we pass in is the same as the filename we saved the spritefont as, &amp;quot;MainFont&amp;quot;.&amp;#160; Notice we indicate what type of content we are loading in the &amp;lt;SpriteFont&amp;gt; designation.&amp;#160; When we get to having graphics in the game, we&amp;#39;ll use different values.&lt;/p&gt;

&lt;p&gt;Line 44 (for...): This is a loop that will put cards into the draw stack.&amp;#160; The parameters of the For loop basically are: use an int and start it at zero, loop while the value is less than 10, and increment it by one each time.&amp;#160; If you wanted number 10 through 20, you&amp;#39;d change the first parameter to initialize the int to 10 and change the second parameter to check for less than 20.&amp;#160; If you only wanted even numbers, change the last parameter to increment by 2 (i+=2).&lt;/p&gt;

&lt;p&gt;line 46 (drawcards...): Here we push the new card onto the draw stack.&amp;#160; remember, we&amp;#39;re just using text right now.&amp;#160; when we use a card class, we&amp;#39;ll do a bit more here.&amp;#160; The card uses the value of the loop variable to uniquely identify it.&lt;/p&gt;

&lt;p&gt;line 49 (drawcards...): We get to use our shuffle method.&amp;#160; During the loop we were putting on cards 0 through 10, so our draw stack would look like 1,2,3,3,4,5,6,7,8,9,10.&amp;#160; The first card we pulled off the deck would be 10, then 9, and anything but random.&amp;#160; So we shuffle it first.&amp;#160; Don&amp;#39;t trust it?&amp;#160; Shuffle it a few more times.&lt;/p&gt;

&lt;p&gt;Line 57 (if ((button...): We&amp;#39;re in the update method.&amp;#160; This gets called very frequently and is used to check for input.&amp;#160; This particular line checks two things: is the draw button already pushed?&amp;#160; Or more accurately, has it been held down?&amp;#160; If it is, don&amp;#39;t bother drawing a card.&amp;#160; If the button was not held down previously and the button is now pushed down, then let&amp;#39;s draw a card.&lt;/p&gt;

&lt;p&gt;line 59 (if ((drawcards...): We know we are going to draw a card, but what if there are no cards to draw - they are all discarded.&amp;#160; Then we reinitialize the draw stack and reshuffle.&amp;#160; Now you see why we created a constructor that accepts a CardStack parameter.&amp;#160; Sure, we could have looped through the discard stack, popped everything off, pushed it onto the draw stack, then shuffled it, but what if you needed to do that somewhere else, like if we had a reset button?&amp;#160; Then you&amp;#39;d have to write the same code again there.&amp;#160; Let the stack class handle it.&lt;/p&gt;

&lt;p&gt;Line 61 (drawcards...): We create a new instance of the CardStack class and pass it the discard stack to build from.&amp;#160; I hear you veterans mumbling about wasting memory by creating another instance.&amp;#160; No one cares at this point in a tutorial.&lt;/p&gt;

&lt;p&gt;line 62(drawcards...): We shuffle the rebuilt draw stack before we draw our first card.&lt;/p&gt;

&lt;p&gt;Line 65(currentcard...): Whether we are dealing with a fresh deck or not, we always pull a card off and store it in the currentcard variable.&amp;#160; We need it stored because another method is going to display its value.&lt;/p&gt;

&lt;p&gt;Line 66(discardCards...): So we don&amp;#39;t lose the card, we put it on the discard stack.&amp;#160; we still have our reference to the card in currentCard.&lt;/p&gt;

&lt;p&gt;Line 68 (isbuttonpressed...): We want to remember that the card has already been drawn for this button push, no matter how long the button is held in for.&amp;#160; Line 57 makes sure we don&amp;#39;t keep drawing cards when we set this variable.&lt;/p&gt;

&lt;p&gt;Line 71 (if (gamepad...): If we don&amp;#39;t reset the isButtonpressed variable, we&amp;#39;ll never get a second press.&amp;#160; So we check to see if the button has been lifted, and if so, reset the variable.&amp;#160; Then line 57 will catch the next button press.&lt;/p&gt;

&lt;p&gt;line 72 (isbuttonpressed...): reset the button check variable to false.&amp;#160; The update method gets called like 60 times a second, so there should be no noticeable delay between button presses.&lt;/p&gt;

&lt;p&gt;line 79 (string...): We are going to use a format string to display the message.&amp;#160; It&amp;#39;s a little cleaner code.&amp;#160; This will be what we store the string in.&lt;/p&gt;

&lt;p&gt;line 81-83 (msg=...): Here we are defining the format string.&amp;#160; We use {0}, {1}, and {2} are placeholders for where we want to insert text.&lt;/p&gt;

&lt;p&gt;Line 86 (spritebatch...): We have to do Begin and end when writing to a spritebatch.&lt;/p&gt;

&lt;p&gt;line 88 (if...): For presentation, we want to check of the current card is nothing.&amp;#160; This only happens the first time you start the program, because you haven&amp;#39;t drawn any cards yet.&amp;#160; So this checks whether the current card is nothing so the right message is displayed.&amp;#160; This will still work when we create a card class.&lt;/p&gt;

&lt;p&gt;line 90 (spritebatch...): We draw the string to the spritebatch for display.&amp;#160; This uses the string.format function, which replaces the placeholders in our format string with the value specified: current card, count of draw stack, and count of discard stack.&amp;#160; Notice we didn&amp;#39;t write any code for counting the size of the stacks.&amp;#160; It came free when we inherited from Stack.&amp;#160; Our .DrawString method is using the spritefont we loaded back on line 42.&amp;#160; We specify a position to write the text using a Vector2 class, and specify a color.&lt;/p&gt;

&lt;p&gt;line 91 (else...): If the first card isn&amp;#39;t drawn yet...&lt;/p&gt;

&lt;p&gt;Line 93 (spritebatch...): Draw the instruction to draw the first card.&amp;#160; This time we are using a regular string instead of the formatted string.&lt;/p&gt;

&lt;p&gt;Line 96 (spritebatch...): You have to do End when done writing to a spritebatch.&amp;#160; If you forget you&amp;#39;ll get a specific error message telling you that you forgot.&lt;/p&gt;

&lt;p&gt;That&amp;#39;s it.&amp;#160; You&amp;#39;re done.&amp;#160; And I&amp;#39;m exhausted.&amp;#160; I&amp;#39;m not sure I&amp;#39;ll be able to keep up this level of detail with future posts, and maybe no one needs it, but you can&amp;#39;t be sure.&amp;#160; This post explained some very basic programming topics and tried to provide as much &amp;quot;why&amp;quot; as possible.&amp;#160; It also gave an example of inheritance and generics, two things not really meant for first-timers, so if you got it, great.&amp;#160; That was part of the &amp;quot;best way to do it&amp;quot; instruction.&lt;/p&gt;

&lt;p&gt;The next step is to create the card class and use it instead.&amp;#160; That&amp;#39;s actually pretty easy, so maybe it will include some other tips.&lt;/p&gt;

&lt;p&gt;During proofreading, I noticed something that might really cause trouble for copy/pasters.&amp;#160; When you create a new C# project, your default namespace is created from the name of the project.&amp;#160; Notice in my code samples, the namespace is CardsSample.&amp;#160; If you create a project with a name that is different from CardsSample, you will get errors when building.&amp;#160; Why?&lt;/p&gt;

&lt;p&gt;As I mentioned earlier, namespaces are used to organize code.&amp;#160; So the cardStack class can exist in many different places in your project as long as each definition is in its own namespace, so they won&amp;#39;t conflict.&amp;#160; If you would copy and paste the code into your project, you now effectively have two namespaces in your single project.&amp;#160; There&amp;#39;s nothing wrong with that, but that&amp;#39;s not what you want or expect.&amp;#160; To fix it, change the namespace in the code you are pasting to match the name of your project.&amp;#160; Or, you can look at the namespace being used in the file Program.cs and copy that to your pasted code.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=257" width="1" height="1"&gt;</description></item><item><title>Too Many Items In Combo Box: When One Is Just One Too Many</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/09/03/too-many-items-in-combo-box-when-one-is-just-one-too-many.aspx</link><pubDate>Wed, 03 Sep 2008 01:46:36 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:256</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I got to troubleshoot a dumb error message today.&amp;#160; The error was &amp;quot;&lt;strong&gt;Too many items in combo box.&lt;/strong&gt;&amp;quot;&amp;#160; The situation was anything but.&amp;#160; I was only adding one item.&lt;/p&gt;  &lt;p&gt;So I got it working and I wanted to find out why it happened in the first place.&amp;#160; The error it should have returned was &amp;quot;&lt;strong&gt;Value cannot be NULL&lt;/strong&gt;&amp;quot; because that was the root of the problem.&amp;#160; So here&amp;#39;s a distilled piece of code to illustrate the problem.&amp;#160; Create a form and put a combo box on it.&amp;#160; The code for the form should look like:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Form1

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Form1_Load(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; sender &lt;span class="kwrd"&gt;As&lt;/span&gt; System.&lt;span class="kwrd"&gt;Object&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; e &lt;span class="kwrd"&gt;As&lt;/span&gt; System.EventArgs) _ 
        &lt;span class="kwrd"&gt;Handles&lt;/span&gt; &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Load

        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; d &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; DisplayItem
        ComboBox1.Items.Add(d)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; DisplayItem
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; Name &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; ToString() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; Name
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The problem is the combo box is trying to display &lt;strong&gt;DisplayItem.Name&lt;/strong&gt;, because that is what the &lt;strong&gt;ToString&lt;/strong&gt; says to do, but the value of &lt;strong&gt;Name&lt;/strong&gt; is Nothing.&amp;#160; You can fix this by setting the value of &lt;strong&gt;Name&lt;/strong&gt; to &lt;strong&gt;String.Empty&lt;/strong&gt; or something else.&amp;#160; The &lt;em&gt;odd &lt;/em&gt;thing is that you can also fix the problem by commenting out the &lt;strong&gt;ToString&lt;/strong&gt; override.&amp;#160; To figure this out, I fired up Reflector and went to see what was going on behind the scenes.&lt;/p&gt;

&lt;p&gt;This particular situation is basically bypassing all the safe value checks done when adding an item to a list control.&amp;#160; I suppose Microsoft should add a test case for this scenario, but really, if the programmer is attentive, this shouldn&amp;#39;t happen.&amp;#160; I, naturally, happen to be inattentive.&lt;/p&gt;

&lt;p&gt;Behind the scenes of the &lt;strong&gt;Add&lt;/strong&gt; method of the Items collection, the first check is in the &lt;strong&gt;AddInternal&lt;/strong&gt; method.&amp;#160; Since we&amp;#39;re passing in an instance of DisplayItem, it passes that check.&amp;#160; The next step is in the &lt;strong&gt;NativeAdd&lt;/strong&gt; method.&amp;#160; At this point, we&amp;#39;ve done our NULL checks and it is assumed we can convert the object to a string.&amp;#160; This method now calls &lt;strong&gt;GetItemText&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GetItemText&lt;/strong&gt; parses the properties of the object passed in and gets the string value.&amp;#160; If the &lt;strong&gt;DisplayMember&lt;/strong&gt; property is not set, the control uses the &lt;strong&gt;ToString&lt;/strong&gt; value of the object itself.&amp;#160; Because we overrode &lt;strong&gt;ToString&lt;/strong&gt;, the control trusts us and returns the value from &lt;strong&gt;ToString&lt;/strong&gt;, the &lt;strong&gt;Name&lt;/strong&gt; value.&amp;#160; This turns out to be Nothing - Oops!&amp;#160; We&amp;#8217;ve already passed the check for Nothing, so this sends bad data to the Win32 API, bubbling a failure error code back to &lt;strong&gt;NativeAdd&lt;/strong&gt;.&amp;#160; If &lt;strong&gt;NativeAdd&lt;/strong&gt; gets anything but a success, it always returns the message &amp;#8220;Too Many Items In Combo Box&amp;#8221;.&amp;#160; But the real reason is that you snuck a Nothing past the initial validation.&lt;/p&gt;

&lt;p&gt;Interestingly, if the &lt;strong&gt;DisplayMember&lt;/strong&gt; &lt;em&gt;is set&lt;/em&gt;, and the value of the property is Nothing, it is handled properly in &lt;strong&gt;GetItemText&lt;/strong&gt;.&amp;#160; If we converted &lt;strong&gt;Name&lt;/strong&gt; to a private field and made a public property, then set the &lt;strong&gt;DisplayMember&lt;/strong&gt; of&amp;#160; ComboBox1, it would work.&amp;#160; If your display member is another object that overrides the &lt;strong&gt;ToString&lt;/strong&gt; function, you can get around that check as well and return Nothing, causing a failure.&lt;/p&gt;

&lt;p&gt;The simple solution for this error message is to avoid NULL values.&amp;#160; The bottom line is to have .&lt;strong&gt;ToString&lt;/strong&gt; always return a string, never Nothing.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=256" width="1" height="1"&gt;</description></item><item><title>IsInRole While Disconnected; No Longer IsInHole</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/08/26/isinrole-while-disconnected-no-longer-isinhole.aspx</link><pubDate>Wed, 27 Aug 2008 00:46:26 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:255</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;In an application I am continuing to write, during startup, the user&amp;#39;s security is determined by the user&amp;#39;s security groups and permissions are granted within the program based on the group membership.&amp;#160; This has worked fine.&amp;#160; Then one day in an airport I wanted to work on some documentation and I quickly discovered that I could not run the program.&amp;#160; I could not run the program because I was not on the VPN and the Active Directory (AD) groups could not be enumerated to check my permissions.&amp;#160; At the time, I was kind of grateful I couldn&amp;#39;t do any work and didn&amp;#39;t give it much more thought. Our application is not designed to be run in a disconnected fashion.&amp;#160; You have to be on the VPN to get to the database server and use it.&amp;#160; Lately, I revisited the problem and decided to resolve it.&lt;/p&gt;  &lt;p&gt;In my particular instance, I had a database running locally, but I was missing an Active Directory server to read from.&amp;#160; I had my cached credentials, shouldn&amp;#39;t that be enough?&amp;#160; Well, it is, if you do it the hard way.&lt;/p&gt;  &lt;p&gt;Everyone should know that a user or a group is just a name.&amp;#160; There is an ID behind that group or user, which allows you to rename the group/user without breaking anything.&amp;#160; Behind the scenes, Windows always uses the ID.&amp;#160; The name is just for your benefit.&lt;/p&gt;  &lt;p&gt;Using the excellent tool &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=3E89879D-6C0B-4F92-96C4-1016C187D429&amp;amp;displaylang=en"&gt;WhoAmI&lt;/a&gt;, I saw the following (don&amp;#39;t chide me for running as Administrator):&lt;/p&gt;  &lt;pre&gt;C:\&amp;gt;whoami /groups /sid

[Group  1] = &amp;quot;700CB\Domain Users&amp;quot;  S-1-5-21-2454202000-1896829455-2950045386-513
[Group  2] = &amp;quot;Everyone&amp;quot;  S-1-1-0
[Group  3] = &amp;quot;DA3\Debugger Users&amp;quot;  S-1-5-21-854245398-1547161642-725345543-1007
[Group  4] = &amp;quot;DA3\Offer Remote Assistance Helpers&amp;quot;  S-1-5-21-854245398-1547161642-725345543-1004
[Group  5] = &amp;quot;BUILTIN\Users&amp;quot;  S-1-5-32-545
[Group  6] = &amp;quot;BUILTIN\Administrators&amp;quot;  S-1-5-32-544
[Group  7] = &amp;quot;NT AUTHORITY\INTERACTIVE&amp;quot;  S-1-5-4
[Group  8] = &amp;quot;NT AUTHORITY\Authenticated Users&amp;quot;  S-1-5-11
[Group  9] = &amp;quot;LOCAL&amp;quot;  S-1-2-0
[Group 10] = &amp;quot;700CB\Domain Admins&amp;quot;  S-1-5-21-2454202000-1896829455-2950045386-512&lt;/pre&gt;

&lt;p&gt;When disconnected form the LAN (you might need to reboot while disconnected to clear the cache), it looks like:&lt;/p&gt;

&lt;pre&gt;C:\&amp;gt;whoami /groups /sid

[Group  1] = &amp;quot;&amp;quot;  S-1-5-21-2454202000-1896829455-2950045386-513
[Group  2] = &amp;quot;Everyone&amp;quot;  S-1-1-0
[Group  3] = &amp;quot;DA3\Debugger Users&amp;quot;  S-1-5-21-854245398-1547161642-725345543-1007
[Group  4] = &amp;quot;DA3\Offer Remote Assistance Helpers&amp;quot;  S-1-5-21-854245398-1547161642-725345543-1004
[Group  5] = &amp;quot;BUILTIN\Users&amp;quot;  S-1-5-32-545
[Group  6] = &amp;quot;BUILTIN\Administrators&amp;quot;  S-1-5-32-544
[Group  7] = &amp;quot;NT AUTHORITY\INTERACTIVE&amp;quot;  S-1-5-4
[Group  8] = &amp;quot;NT AUTHORITY\Authenticated Users&amp;quot;  S-1-5-11
[Group  9] = &amp;quot;LOCAL&amp;quot;  S-1-2-0
[Group 10] = &amp;quot;&amp;quot;  S-1-5-21-2454202000-1896829455-2950045386-512&lt;/pre&gt;

&lt;p&gt;My local group names were resolved to their names, but my domain group names couldn&amp;#39;t resolve because AD was unreachable.&amp;#160; My cached profile still had the SIDs though.&lt;/p&gt;

&lt;p&gt;So, if I could do an IsInRole check using the SID instead of the domain group name, I&amp;#39;d be golden.&amp;#160; And this is just what I did.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Security.Principal
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Threading

&lt;span class="kwrd"&gt;Dim&lt;/span&gt; sid &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; SecurityIdentifier(&lt;span class="str"&gt;&amp;quot;S-1-5-21-1859785585-1835888107-1082013118-1025&amp;quot;&lt;/span&gt;)
&lt;span class="kwrd"&gt;Dim&lt;/span&gt; p &lt;span class="kwrd"&gt;As&lt;/span&gt; WindowsPrincipal = &lt;span class="kwrd"&gt;CType&lt;/span&gt;(Thread.CurrentPrincipal, WindowsPrincipal)

MsgBox(p.IsInRole(sid))&lt;/pre&gt;

&lt;p&gt;So I took the SID for the groups I was testing for and tested for them instead.&amp;#160; Obviously, if an admin deleted and recreated the group thinking nobody would notice, it would be hell to troubleshoot, so if you&amp;#39;re not fully in control of your environment, you might want to steer clear.&amp;#160; Maybe do the SID after checking IsNetworkAvailable to reduce the exposure to failure?&lt;/p&gt;

&lt;p&gt;But for me, it works like a champ, and now I can work in airports.&amp;#160; Hmmm.&amp;#160; Why did I figure this out again?&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=255" width="1" height="1"&gt;</description></item><item><title>Columns Autosize: Listview in List View</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/08/23/columns-autosize-listview-in-list-view.aspx</link><pubDate>Sat, 23 Aug 2008 02:04:56 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:254</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Here&amp;#39;s another little snip of code I couldn&amp;#39;t find online when I needed it.&amp;#160; Geez, when I became a programmer, I wasn&amp;#39;t thinking I&amp;#39;d have to actually figure things out on my own.&amp;#160; That&amp;#39;s a lot of work.&lt;/p&gt;  &lt;p&gt;Anyway, the problem I faced was when I had a Listview control on a form and I changed views from anything to List view, the columns of the items were really small, so I&amp;#39;d get ellipses&amp;#39; after all the entries.&amp;#160; Sure, I could just set the column with to some obnoxious amount like 500, but that&amp;#39;s a waste of space.&amp;#160; So after searching and seeing a bunch of postings about using a Win32 API call to autoresize the column, then getting disappointed because it was for VB 4/5/6, I just hacked through it.&lt;/p&gt;  &lt;p&gt;The Details view has an autosize feature, but apparently they didn&amp;#39;t extend it to the List view.&amp;#160; But we can still make use of it.&amp;#160; Why not switch to detail, set the autosize of column 0 to true, then measure how wide it makes the column, then use that as the column width in List view?&amp;#160; That&amp;#39;s a dumb idea.&amp;#160; Who would do something like that.&amp;#160; Oh, what do you know, it works.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; maxSize &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;

lstItems.View = View.Details
lstItems.Columns(0).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
maxSize = lstItems.Columns(0).Width

lstItems.View = View.List
lstItems.Columns(0).Width = maxSize
lstItems.Refresh()&lt;/pre&gt;

&lt;p&gt;Yay.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=254" width="1" height="1"&gt;</description></item><item><title>Questions; Twenty of 'em</title><link>http://soapitstop.com/blogs/fleamarket/archive/2008/08/12/questions-twenty-of-em.aspx</link><pubDate>Tue, 12 Aug 2008 01:41:02 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:253</guid><dc:creator>anachostic</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I was thinking of that interesting little pocket game, 20Q, where the game knows so many words and is able to figure out what word you&amp;#39;re thinking of in 20 questions.&amp;#160; For the most part, it&amp;#39;s not too bad.&amp;#160; It must really be pretty complex to be able to figure out all those words.&lt;/p&gt;  &lt;p&gt;I&amp;#39;ve brainstormed on this idea before and whenever I start the code for it, I lose interest, but it doesn&amp;#39;t stop me from kicking it around in my head over and over.&lt;/p&gt;  &lt;p&gt;So pretty much, you have a database table full of answer words, which also has a numeric PK, you have a table of questions, with numeric PK, and you have a table with a matrix of every question and every answer PK and a response value (1=yes, 0=no).&amp;#160; That would be the basic data structure.&amp;#160; For data population, you could fill the answer table with random nouns pulled from any dictionary file.&amp;#160; That&amp;#39;s simple.&amp;#160; For the questions table, you&amp;#39;d have to enter as many random questions as you could think of.&amp;#160; Random.&amp;#160; And LOTS.&amp;#160; Don&amp;#39;t think of the answer, just think of any question.&amp;#160; Then after you&amp;#39;ve fried your mind with questions, do a Cartesian join to insert the PKs into the matrix table, leaving the response field null.&amp;#160; Now the fun part.&amp;#160; Make a form with big Yes and No buttons.&amp;#160; The form will read each row in the matrix table, joined with the question and answer table and you respond yes or no to each question/answer combination.&amp;#160; This should take a few days.&lt;/p&gt;  &lt;p&gt;After sleeping off that headache and letting your eyes reset, it would be time to test coverage.&amp;#160; Do all of your questions provide a unique combination for every answer?&amp;#160; Does that get the wheels in your head spinning as to how to create such a uniqueness test?&amp;#160; I&amp;#39;m going to post my untested first idea, because nothing spurs responses better than posting something wrong.&amp;#160; My idea would be have an outer query (this is in .NET) for each answer PK, then an inner query that reads the question PK for each response that is yes (sorted by question PK, filtered by answer PK), loop through the inner query and concatenate the question PKs together into a big string and store it (maybe in a hashtable) with the answer ID as the value.&amp;#160; If you hit a dupe, then you have two answers that have the exact same yes responses.&lt;/p&gt;  &lt;p&gt;After you&amp;#39;ve tested for full coverage and removed any answers that can&amp;#39;t be exclusively answered by a series of responses, the goal is to present the most efficient question first.&amp;#160; What is the most efficient question?&amp;#160; The one that removes the most answers from the potential answer pool.&amp;#160; This is another fun mental exercise.&amp;#160; At the simplest level, it&amp;#39;s the count of answers grouped by question filtered by all previous questions that don&amp;#39;t match previous responses.&amp;#160; Something like (in pseudo-code)&lt;/p&gt;  &lt;p&gt;select Questions.ID,count(Answers.ID) AnswerCount   &lt;br /&gt;from QAMatrix    &lt;br /&gt;join Answers on QAMatrix.AnswersID=Answers.ID    &lt;br /&gt;join Questions on QAMatrix.QuestionsID=Questions.ID    &lt;br /&gt;where Questions.ID=[previous question id] and QAMatrix.Response&amp;lt;&amp;gt;[previous response value]    &lt;br /&gt;and Questions.ID=[previous question id] and QAMatrix.Response&amp;lt;&amp;gt;[previous response value]    &lt;br /&gt;...    &lt;br /&gt;group by Questions.ID    &lt;br /&gt;order by 2 desc&lt;/p&gt;  &lt;p&gt;I think that would do it.&amp;#160; The first question in the results should have the highest answers being trimmed out.&amp;#160; Looking at it now during a proofread, I suppose it might need enhanced so that it identifies the balance of yes/no responses in that count.&amp;#160; It won&amp;#39;t do much good if 90% the answers were yes.&amp;#160; Well... depending on the answer, that&amp;#39;s a potential risk with a potential huge reward - wiping out 90% of the possible answers.&amp;#160; Would an advanced 20Q program take more risks early or later or would it play middle-of-the-road and always shoot for a 50% split?&amp;#160; Design details for the interested.&amp;#160; Another 2nd version detail might be to remove any questions that don&amp;#39;t split the answers well enough.&amp;#160; Maybe that&amp;#39;s how 20Q ended up with such weird questions like &amp;quot;Does it grow hair?&amp;quot;&lt;/p&gt;  &lt;p&gt;And that&amp;#39;s about all the fun stuff I&amp;#39;ve been thinking about and not doing lately.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=253" width="1" height="1"&gt;</description></item><item><title>Why does Vista SP1 show me I have 4 GB of memory available?</title><link>http://soapitstop.com/blogs/soageek/archive/2008/07/24/why-does-vista-sp1-show-me-i-have-4-gb-of-memory-available.aspx</link><pubDate>Wed, 23 Jul 2008 21:01:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:252</guid><dc:creator>tom.fuller</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;This is one of those deep nerdy windows internals posts that will interest some and make other say &amp;quot;wow,&amp;nbsp; Tom needs to spend more time away from his computer&amp;quot;.&amp;nbsp; There is actually a good reason why I&amp;#39;ve dug into this topic and it stems from my recent hardware allocation in my new job.&amp;nbsp; When I started I received a new Lenovo laptop with 4 GB of memory and my base image came with Vista 32 bit.&amp;nbsp; I didn&amp;#39;t think too much of it and loaded everything up.&amp;nbsp; This was about 6 months ago so there was no Vista SP 1 at the time.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;To my dismay when I opened my computer properties I saw that I only had 3070 MB of available physical memory.&amp;nbsp; So somehow I lost an entire gig of RAM.&amp;nbsp; I spoke with some of my other colleagues that were also new hires and they didn&amp;#39;t have the same issue.&amp;nbsp; Many of them had HP or Dell notebooks and they had more like 3.5 GB of available physical memory on their machines.&lt;/p&gt;
&lt;p&gt;It was right around this time that I got my first deep internals class for my new job.&amp;nbsp; In this class we learned all of the tips and tricks for doing advanced perfmon analysis of memory leaks, disk performance issues, CPU bottlenecks, the list goes on and on.&amp;nbsp; Well the instructor was just incredible so when there was a break in the class I asked him about this issue.&amp;nbsp; He very quickly explained that the reason you see different amounts of available ram was because of the chipset and reserved space for device drivers (video cards typically the biggest hogs).&lt;/p&gt;
&lt;p&gt;Ok,&amp;nbsp; no big deal then right.&amp;nbsp; Lesson learned,&amp;nbsp; when you look to purchase a laptop that can support up to 4 GB of physical memory make sure you find out how much it will make available to windows.&amp;nbsp; Then,&amp;nbsp; Vista SP1 came out and I couldn&amp;#39;t help but notice when I went to the computer properties the window now said I had 4 GB of ram available.&amp;nbsp; Well isn&amp;#39;t that interesting,&amp;nbsp; someone must have lied to me!!!!&amp;nbsp; Actually it turns out that with SP1 we decided to show the installed RAM instead of the available RAM.&amp;nbsp; This is very confusing as it&amp;#39;s never been that way for any previous OS version.&amp;nbsp; If you want to learn some more about it you can take a look at this kb article &lt;a href="http://support.microsoft.com/kb/946003/"&gt;http://support.microsoft.com/kb/946003/&lt;/a&gt;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Now my story here is one of confusion and ignorance.&amp;nbsp; If you&amp;#39;re the type that wants to really understand this stuff then you have to go the best windows internals guy on the planet.&amp;nbsp; Of course I&amp;#39;m talking about Mark Russinovich,&amp;nbsp; he wrote a long blog post just this week (which is what reminded me I wanted to blog about this) that gives a complete explanation of this issue along with some other really cool stuff about RAM limits on other OS versions.&amp;nbsp; He also shows a screenshot from the largest test server in the world used to define the limits for memory in 64 bit windows (currently 2 TB because there was nothing any higher available that we could test with).&amp;nbsp; This is a must read &lt;a href="http://blogs.technet.com/markrussinovich/archive/2008/07/21/3092070.aspx"&gt;http://blogs.technet.com/markrussinovich/archive/2008/07/21/3092070.aspx&lt;/a&gt;.&amp;nbsp; If you just want the technical detail behind the 4GB issue I talked about above then scroll down to the &amp;quot;32-bit Client Effective Memory Limits&amp;quot; section.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;So the main lessons here are:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1) If you are still planning to run 32-bit windows clients and you plan to install more than 3 GB of RAM then not all&amp;nbsp;hardware is&amp;nbsp;created equal.&amp;nbsp; Make sure you ask how much physical RAM is made available to windows.&amp;nbsp; This becomes even more important when you start doing a lot of Virtual Lab work as I often do.&amp;nbsp; It&amp;nbsp;sure would be nice to get that third Win 2K3 server running with&amp;nbsp;a full GB of&amp;nbsp;RAM.&amp;nbsp; Why didn&amp;#39;t I install the 64 bit version of&amp;nbsp;Vista Ultimate&amp;nbsp;again?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2) The 64-bit client version of windows is really the only way to ensure you can start to have enough RAM for doing a lot of virtualization work.&amp;nbsp; It&amp;#39;s also the only way to get in and see how many gaps your hardware has left in the device driver ranges (see Mark&amp;#39;s review of this).&amp;nbsp; 64-bit is the future ... embrace it ... love it ... live it.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=252" width="1" height="1"&gt;</description><category domain="http://soapitstop.com/blogs/soageek/archive/tags/Windows+Internals/default.aspx">Windows Internals</category></item><item><title>Blog consolidation complete.</title><link>http://soapitstop.com/blogs/soageek/archive/2008/07/24/blog-consolidation-complete.aspx</link><pubDate>Wed, 23 Jul 2008 20:01:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:251</guid><dc:creator>tom.fuller</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;There are two reasons why I went ahead and deleted my other blogs today.&amp;nbsp; The first is that I finally came to grips with the reality that managing three blogs is not very realistic.&amp;nbsp; That was probably evident by the fact that each of those blogs had about 2 postings.&amp;nbsp; So I&amp;#39;ll be adding some new blog posts in my good old SOA focused blog that have maybe some out of bounds topics (like support, debuggin, and SQL BI).&lt;/p&gt;
&lt;p&gt;The second reason for consolidating my other blogs&amp;nbsp;is that I&amp;#39;m finding that the technology gods, in their infinite wisdom, have carved out a path for me in my new role at Microsoft that leads to SOA.&amp;nbsp; Three areas I&amp;#39;ve been spending a lot of time on the last two months are WCF, BizTalk&amp;nbsp;and the Managed Services Engine (codeplex.com/servicesengine).&amp;nbsp; You may have noticed this in the form of some more technical postings on WCF and BizTalk recently.&amp;nbsp; Well that&amp;#39;s only the beginning.&amp;nbsp; I have quite a few more active support cases where I&amp;#39;m learning really interesting stuff and I&amp;#39;m about to head out to Seattle for 10 days of training.&amp;nbsp; Half of that training will be on all of our SOA related bits from the SOA Solutions team.&amp;nbsp; Needless to say I&amp;#39;m really stoked.&lt;/p&gt;
&lt;p&gt;Expect to see a lot of posts coming from me in the near future about all this stuff I&amp;#39;m working on.&amp;nbsp; I know I&amp;#39;ve said this before but I&amp;#39;m now focused and very excited because of the technology area I&amp;#39;m beginning to venture into.&amp;nbsp; Mr. SOAPitStop is back baby!!!!&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=251" width="1" height="1"&gt;</description><category domain="http://soapitstop.com/blogs/soageek/archive/tags/General/default.aspx">General</category><category domain="http://soapitstop.com/blogs/soageek/archive/tags/Announcements/default.aspx">Announcements</category><category domain="http://soapitstop.com/blogs/soageek/archive/tags/SOA/default.aspx">SOA</category></item><item><title>Gnarly issue setting up BizTalk 2006 R2 in a 2-Tier configuration on Virtual Server 2005</title><link>http://soapitstop.com/blogs/soageek/archive/2008/07/09/gnarly-issue-setting-up-biztalk-2006-r2-in-a-2-tier-configuraiton-on-virtual-server-2005.aspx</link><pubDate>Wed, 09 Jul 2008 18:08:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:250</guid><dc:creator>tom.fuller</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Before I forget to do this I wanted to post about this issue because it cost me a 1/2 day of debugging and I hope to save you this agony.&amp;nbsp; It actually turns out there&amp;#39;s a nasty little issue when you use Virtual Server 2005 differencing disks to quickly stand up lab enviorments.&amp;nbsp; In my new role with Microsoft support I&amp;#39;m constantly setting up 1, 2, 3, and sometimes 4 node lab environments so differencing disks are pretty much a must.&amp;nbsp; Of course if you want to do this and still need the ability to join these servers to a test domain, then&amp;nbsp;you have to make sure you rename / resid them.&amp;nbsp; That&amp;#39;s where I&amp;#39;ve always relied on newsid from the sysinternals tools.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;I&amp;#39;ve always wondered if this was a hack (versus a full sysprep)&amp;nbsp;and I wondered when it would jump up and bite me.&amp;nbsp; I noticed in IIS that the anonymous user account does not get recreated to use the new computer name but that&amp;#39;s never impacted me (I sort of figured a local account really isn&amp;#39;t going to make much of a diff).&amp;nbsp; Well,&amp;nbsp; I have now finally found an issue that is ugly.&amp;nbsp; It turns out that if you are going to build an app server / db server both from the same base OS image you are going to have some issues with MSDTC.&amp;nbsp; I ran into this as I started to run through the BizTalk configuration tools.&amp;nbsp; I kept getting the following error:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;2008-07-08 17:44:58:0896 [ERR] WMI Failed in pAdmInst-&amp;gt;Create() in CWMIInstProv::PutInstance(). HR=c0c025b3&lt;br /&gt;2008-07-08 17:44:58:0906 [ERR] WMI WMI error description is generated: Exception of type &amp;#39;System.EnterpriseServices.TransactionProxyException&amp;#39; was thrown.&lt;br /&gt;2008-07-08 17:44:58:0936 [INFO] WMI CWMIInstProv::PutInstance() finished. HR=c0c025b3&lt;br /&gt;[5:44:58 PM Error BtsCfg] d:\depot2300\mercury\private\mozart\source\setup\btscfg\btswmi.cpp(358): FAILED hr = c0c025b3&lt;/p&gt;
&lt;p&gt;[5:44:58 PM Error BtsCfg] Exception of type &amp;#39;System.EnterpriseServices.TransactionProxyException&amp;#39; was thrown.&lt;/p&gt;
&lt;p&gt;At least this error helped me narrow down the root cause of the problem which is obviously System.EnterpriseServices.&amp;nbsp; So I started looking at all my settings for MSDTC and nothing seemed out of the norm.&amp;nbsp; All the right security settings and network dtc access settings were there.&amp;nbsp; So it was time to do a quick search with live.com (that&amp;#39;s right ... I said live.com ... google is so 5 years ago :)&lt;/p&gt;
&lt;p&gt;It turns out the problem is with the underlying CID settings for MSDTC.&amp;nbsp; It looks like newsid doesn&amp;#39;t go in and take care of these.&amp;nbsp; Fortunately I wasn&amp;#39;t the first one to come across this because I don&amp;#39;t know if I would have come up with these steps.&amp;nbsp; A big bravo goes out to Wade Wegner who wrote a great overview of the problem and the steps to fix it.&amp;nbsp; You can read the full&amp;nbsp;description and steps to identify wether or not this is&amp;nbsp;your issue&amp;nbsp;here:&amp;nbsp; &lt;a href="http://blog.wadewegner.com/2007/08/13/quotWarningTheCIDValuesForBothTestMachinesAreTheSamequot.aspx"&gt;http://blog.wadewegner.com/2007/08/13/quotWarningTheCIDValuesForBothTestMachinesAreTheSamequot.aspx&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you just want the solution you can follow these quick steps on both machines. (taken from Wade&amp;#39;s blog).&lt;/p&gt;
&lt;ol&gt;
&lt;div&gt;
&lt;li&gt;Use Add Windows Components, and remove Network DTC.&lt;/li&gt;&lt;/div&gt;
&lt;li&gt;Go to the command line and run: MSDTC -uninstall 
&lt;li&gt;Go to the registry and delete the MSDTC keys in HKLM/Software/Microsoft/Software/MSDTC, HKLM/System/CurrentControlSet/Services/MSDTC, and HKEY_CLASSES_ROOT\CID. * 
&lt;li&gt;Reboot 
&lt;li&gt;Go to the command line and run: MSDTC -install 
&lt;li&gt;Use Add Windows Components, and add Network DTC. 
&lt;li&gt;Go to the command line and run: net start msdtc **&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;* for me the CID settings were deleted when I&amp;nbsp;did the -uninstall so you&amp;nbsp;shouldn&amp;#39;t need to remvoe the last ones.&amp;nbsp; Also,&amp;nbsp; this does mean you should delete the key at&amp;nbsp;the top level and purge all the subkeys,&amp;nbsp; that&amp;nbsp;freaked me&amp;nbsp;out a little at first but of course we&amp;#39;re just talking about a test lab here so&amp;nbsp;I figured why not try it.&amp;nbsp; It worked like a champ.&lt;/p&gt;
&lt;p&gt;** This step wasn&amp;#39;t required for me as the service was already running following me adding the network DTC access from the windows components installer.&lt;/p&gt;&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=250" width="1" height="1"&gt;</description><category domain="http://soapitstop.com/blogs/soageek/archive/tags/BizTalk/default.aspx">BizTalk</category></item><item><title>Yet Another Ambitious Project (YAAP)</title><link>http://soapitstop.com/blogs/generative-programmer/archive/2008/06/29/yet-another-ambitious-project-yaap.aspx</link><pubDate>Sun, 29 Jun 2008 11:17:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:249</guid><dc:creator>bobcalco</dc:creator><slash:comments>0</slash:comments><description>Lately I have been working in Erlang (forgive me, Tom), and in particular on a distributed web-based system that exposes much of its functionality over  a RESTful interface. The web server has been Yaws and the database Mnesia, both of which are also implemented in Erlang, and are quite complex and interesting in their own right. Using Erlang&amp;#39;s OTP (Open Telecom Platform) framework it&amp;#39;s relatively easy to create and configure complex, robust client-server/state-machine applications, taking advantage of Erlang&amp;#39;s native support for massive concurrency, high-availability and fault tolerance.

Anyway, building such a system gives you a sense of where the grunt work is, and grunt work is a great candidate for code generation to mitigate the problem of time-to-market. This time-to-market problem is an issue that is felt quite poignantly when you are working in a startup on borrowed time, as I am at the moment. :)

So currently I am devoting a little R&amp;amp;D time each day to the development of a code generation tool for RESTful applications in Erlang, which I call &amp;quot;shenkuo.&amp;quot; The reason for the name is two fold: 1.) I plan to release it as open source via the Erlware project (www.erlware.org), and 2.) they like funky and exotic names based on real people that have some (obscure or obvious) relevance to the functionality of the project. The reason for Erlang is that it rocks for any problem involving concurrency and network communications--I mean really ROCKS. (More on that subject will be forthcoming in my Functional Programmer blog.)

Shen Kuo was an ancient Chinese polymath who, among other things, came up with the concept of &amp;quot;due north&amp;quot; (a great aid to the compass, by the way), described the first ideas behind movable type, and invented several branches of what we in the west today call the social sciences. Whatever do these wacky and diverse things have to do with what I&amp;#39;m building?

Well, RESTful applications are all the rage in social networking sites, and they are used to do all kinds of various and sundry things in real world &amp;quot;Web 2.0&amp;quot; architectures. REST has attained something of the aura of &amp;quot;due North&amp;quot; as a philosophy behind web application design. So the name &amp;quot;shenkuo&amp;quot; for a code generation framework for RESTful applications based on Erlang/OTP seems as good as any. And it just so happens that my first such distributed application is in the sphere of web publishing, so the reference to movable type in Shen Kuo&amp;#39;s curriculum vitae seems to close the deal for me.

I will be posting more about this YAAP in this space as I work on it. One of its key requirements will be to generate client proxy libraries to consume the generated RESTful interfaces in addition to the interfaces themselves, and I&amp;#39;ll be focusing in this space on .NET clients (particularly WPF and Silverlight), for those of you here wondering why Tom tolerates my presence on his Microsoft SOA blog site. :)&lt;img src="http://soapitstop.com/aggbug.aspx?PostID=249" width="1" height="1"&gt;</description><category domain="http://soapitstop.com/blogs/generative-programmer/archive/tags/Frameworks/default.aspx">Frameworks</category><category domain="http://soapitstop.com/blogs/generative-programmer/archive/tags/Generative+Programming/default.aspx">Generative Programming</category><category domain="http://soapitstop.com/blogs/generative-programmer/archive/tags/Nutty+Ideas/default.aspx">Nutty Ideas</category><category domain="http://soapitstop.com/blogs/generative-programmer/archive/tags/Erlang/default.aspx">Erlang</category></item><item><title>What happened to the PreAuthenticate flag in WCF?</title><link>http://soapitstop.com/blogs/soageek/archive/2008/06/27/what-happened-to-the-preauthenticate-flag-in-wcf.aspx</link><pubDate>Fri, 27 Jun 2008 19:44:00 GMT</pubDate><guid isPermaLink="false">c84fe871-9be4-49f2-9abc-6118b8cda40a:248</guid><dc:creator>tom.fuller</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Ok,&amp;nbsp; I&amp;#39;ve been working in Premier support for exactly 5 months now (to the day).&amp;nbsp; I must have learned something interesting I can share with everyone.&amp;nbsp; I mean it looks like the only blogger left alive on SOAPitStop&amp;nbsp;is Anachostic (good stuff by the way David).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Well there is some good news,&amp;nbsp; I&amp;#39;ve been spending time working on the distributed services PSS team as I continue to ramp up on dedicated engagements with custom solutions customers.&amp;nbsp; That means I&amp;#39;ve been getting some really cool deep WCF issues to dig into.&amp;nbsp; One of the most recent ones was a seemingly simple question about what happened to the PreAuthenticate switch between ASMX and WCF.&amp;nbsp; I was sure I had just missed this property on some internal Http channel class .... wrong!&amp;nbsp; I also figured this would be something plenty of people have already blogged about so there&amp;#39;d be no point in doing a long write up about this .... wrong!&lt;/p&gt;
&lt;p&gt;Enough build up,&amp;nbsp; here&amp;#39;s the short of it.&amp;nbsp; It turns out the PreAuthenticate flag is not something that can be easily controlled by you as a develoeper in the world of WCF.&amp;nbsp; In fact,&amp;nbsp; the underlying default channel factories will use your security settings to decide exactly how this setting should be set (ex. Basic auth gets PreAuthenticate by default,&amp;nbsp; NTLM gets UnsafeAuthenticatedConnectionSharing by default).&amp;nbsp; This is good and bad,&amp;nbsp; on one hand I think there were a lot of developers that were unaware of the ov