Windows Vista Blog Gets a Facelift

Jim Allchin has re-released the Windows Vista blog with more of a ‘Vista’ feel. The previous site was infrequently posted to, and this time Jim is pushing his co-workers to improve the content. If you’re already running Vista, they are looking for feedback.

Better Presentation Tips

Kathey Sierra has posted an article on how to start a presentation, book, article… and as usual it’s worth reading. A few hilights are:

You should not:

  • Start at the beginning
  • Tell, show instead
  • Start with history
  • Start with pre-requisites

You should:

  • Begin with a question
  • Be provocative
  • Evoke empathy
  • Do something surprising
  • Start with something funny
  • Start with a dramatic key event or turning point
  • Have some mystery, suspense or intrigue

The T-SQL Way: Converting Integers to Binary Strings

A post over at Rob Farley‘s blog on converting integers to binary using Transact-SQL caught my eye and it reminded me of a challenge a fellow developer gave me a few years ago, namely that I couldn’t speed up one of the VB functions he had written with the condition that it must still be written in VB. I did, by 3 orders of magnitude!, but that’s another story…

My solution is probably not quite as elegant as Rob’s CTE solution but I reckon it might be faster and use less memory. In fact there are two very similar function based solutions. One is certainly more readable than it’s slightly faster counterpart.

Originally, I used the formula as the basis for a computed column, like so:

CREATE TABLE #t (

intVal int ,

binaryStr AS (

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , (IntVal / 268435456) * 4 + 1, 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , ((IntVal / 16777216) & 15) * 4 + 1 , 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , ((IntVal / 1048576) & 15) * 4 + 1 , 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , ((IntVal / 65536) & 15) * 4 + 1 , 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , ((IntVal / 4096) & 15) * 4 + 1 , 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , ((IntVal / 256) & 15) * 4 + 1 , 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , ((IntVal / 16) & 15) * 4 + 1 , 4 ) +

SUBSTRING( ‘0000000100100011010001010110011110001001101010111100110111101111’ , (IntVal & 15) * 4 + 1 , 4 )

)

);

— Test values

insert into #t VALUES (0)

insert into #t VALUES (1)

insert into #t VALUES (2)

insert into #t VALUES (3)

insert into #t VALUES (4)

insert into #t VALUES (15)

insert into #t VALUES (31)

insert into #t VALUES (65)

insert into #t VALUES (127)

insert into #t VALUES (128)

insert into #t VALUES (129)

insert into #t VALUES (65535)

insert into #t VALUES (65536)

insert into #t VALUES (166754132)

insert into #t VALUES (1073741824)

— int’s are signed, max value is therefore, 2^31 – 1 = 2147483647

insert into #t VALUES (2147483647)

select * from #t

The first function version is:

CREATE FUNCTION IntegerToBinaryString(@intval int)

RETURNS char(32)

AS

BEGIN

DECLARE @bincode char(64)

SET @bincode = ‘0000000100100011010001010110011110001001101010111100110111101111’

RETURN (

SUBSTRING( @bincode, (@IntVal / 268435456) * 4 + 1, 4 ) +

SUBSTRING( @bincode, ((@IntVal / 16777216) & 15) * 4 + 1 , 4 ) +

SUBSTRING( @bincode, ((@IntVal / 1048576) & 15) * 4 + 1 , 4 ) +

SUBSTRING( @bincode, ((@IntVal / 65536) & 15) * 4 + 1 , 4 ) +

SUBSTRING( @bincode, ((@IntVal / 4096) & 15) * 4 + 1 , 4 ) +

SUBSTRING( @bincode, ((@IntVal / 256) & 15) * 4 + 1 , 4 ) +

SUBSTRING( @bincode, ((@IntVal / 16) & 15) * 4 + 1 , 4 ) +

SUBSTRING( @bincode, (@IntVal & 15) * 4 + 1 , 4 )

)

END

GO

This uses a concatenated string of the 4-bit binary string representations of 0 to 15, indexed by the 32-bit input integer broken into 4-bit chunks. You can reduce the number of arithmetic operations still further by creating a combined string of the 256 8-bit combinations:

CREATE FUNCTION IntegerToBinaryString2(@intval int)

RETURNS char(32)

AS

BEGIN

DECLARE @bincode256 char(2048)

SET @bincode256 = ‘00000000000000010000001000000011 …’

RETURN (

SUBSTRING( @bincode256, (@IntVal / 16777216) * 8 + 1, 8 ) +

SUBSTRING( @bincode256, ((@IntVal / 65536) & 255) * 8 + 1 , 8 ) +

SUBSTRING( @bincode256, ((@IntVal / 256) & 255) * 8 + 1 , 8 ) +

SUBSTRING( @bincode256, (@IntVal & 255) * 8 + 1 , 8 ) )

END

GO

[Note: I originally posted the whole 2048 characters of @bincode256, but it caused my blog some alignment problems! You can either use the technique of small cranes to build big cranes, using the following T-SQL snippet to call the first function to create the string, and then cut and paste it, OR type it in yourself. Personally, I ‘d go with the first suggestion!

DECLARE @i int

DECLARE @res varchar(2048)

SET @i = 0

SET @res =

WHILE @i < 256

BEGIN

SET @res = @res + + CAST(Substring(dbo.IntegerToBinaryString(@i), 25, 8) as varchar(8))

SET @i = @i + 1

END

PRINT @res

GO

I’m not sure what Rob and Omnibuzz (!) use for their timing harness or test dataset but hopefully, I’ll follow up with relative timings.

Rob’s was originally inspired by this post.

Windows Forms

If you write Windows forms applications for a living then you will probably have visited the Windows Forms site before. If not, it has interesting articles, sample code, downloads and upcoming trends you should know about. Worth a visit.

Using ASP.NET 2.0: CompareValidator Controls

You can use a <asp:CompareValidator> control to validate one control against another or against a fixed value. I spent an hour searching for a way to use one to validate a date that should be greater than or equal to today’s date. My initial attempt was

<asp:CompareValidator ID=”cvFromDate “ Text=” Date cannot be less than today’s date!” ControlToValidate=”txtFromDate” Type=”Date” Operator=”GreaterThanEqual” SetFocusOnError=”true” Display=”Dynamic” Runat=”server” ValueToCompare=”<%= DateTime.Today.ToShortDateString() %> />                       

 

But it did not like the fixed date value. Interestingly, several articles I found said this was possible, but I had no luck getting it to work (I suspect this is due to initialisation and binding event order?). The simple solution is to set this value during the page’s load event:

   if (!IsPostBack)

   {

       cvFromDate.ValueToCompare = DateTime.Now.ToShortDateString();

The final ASP.NET code which also includes a compare validator to check the same control for dates in valid formats is:

<asp:CompareValidator ID=”cvFromDate” Text=” Date cannot be less than today’s date!” ControlToValidate=”txtFromDate” Type=”Date” Operator=”GreaterThanEqual” SetFocusOnError=”true” Display=”Dynamic” Runat=”server” />  

 

<asp:CompareValidator id=”CompareValidator1″ Text=”Please enter a valid date format.” ControlToValidate=”txtFromDate” Display=”Dynamic” Type=”Date” Operator=”DataTypeCheck” SetFocusOnError=”true” runat=”server” />

Development Zen

Any task that is repeatable is a candidate for automation

Any coding task that is repeatable is a candidate for code generation

Advanced Article on ASP.NET 2.0 Master Pages

Whilst searching for the solution of referencing a control defined in a content page from JavaScript, I found this great article ASP.Net 2.0 – Master Pages: Tips, Tricks, and Traps by K. Scott Allen. It’s definitely one of the most comprehensive, in-depth articles I’ve come across (despite having bought two fairly advanced books on ASP.NET 2.0!). Scott Guthrie linked to this article a while ago here. The article describes how master pages and content pages are combined, event ordering, interacting between the master page and content pages and vice versa, JavaScript and naming containers, and name mangling (the bit that solved my particular problem).

Free E-Learning Courses:

Microsoft Learning has several introductory courses currently being offered for free:

The Visual Studio section is also providing a series of free e-learning courses centred around .NET 3.0 (Collection 5134 : Developing Rich Experiences with Microsoft® .NET Framework 3.0 and Visual Studio® 2005). These courses introduce working with Windows Presentation Foundation, Windows Workflow Foundation, and Windows Communication Foundation. They are aimed at experienced Developers and Software Architects who are looking to adopt Microsoft’s next generation technology:

Database Refactoring

A post over on Larry O’Brien’s blog prompted me to put together this short list of resources that can assist with database factoring and understanding an unfamiliar database:

  • Visual Studio CTP for Database Professionals, you can download CTP 5 – Beta here.
  • Red-Gate’s SQL Dependency Tracker (14-day trial download available). Larry notes:

    “One limitation is that the tool does not have a “Print” capability. I would
    like to print out a (huge, wall-sized) poster of the dependency for study. It
    does, though, have an “Export to image…” capability. If you save to .PNG it
    does not preserve detail, but if you save to .EMF, you can import it into
    Illustrator and divvy it up there.”

  • The book “Refactoring Databases: Evolutionary Database Design” by Scott W. Ambler and Pramod Sadalage, has received excellent reviews (unfortunately mine is on order). There is a dedicated website here
  • Sparx Systems Enterprise Architect is a comprehensive database design tool.

Having access to an A1 size plotter is also very useful!