Thursday, January 29, 2009

Using the checked context in C#

In C# you can place an assignment in a checked context to force an exception at runtime if the value being assigned is outside the range of the data type being assigned to.

void foo(int m) // assume that m is +2,147,483,647 (i.e. max value for an usigned Int32)
{
   int j = m * 4; // will not throw an exception but value in j will be truncated
   checked
   {
      int i = m * 4; // will throw an exception because it's in a checked context
   }
}

This assumes that you're using the C# compiler's default option which is /checked-. If you had set the compiler's /checked+ option then the first assignment to j would have also caused an exception at runtime in the example above. In a checked context, arithmetic overflow raises an exception.

However, if you attempt to assign a value that's tool large for a data type which is known at compile time this will cause a compiler error. For example:

const int x = 2147483647;   // Max int 
const int y = 2;
int z = x * y;

will cause the compiler to generate the following error: "The operation overflows at compile time in checked mode"
 
To prevent this and allow trucation during the assignment to z you should wrap the assignment to z in an unchecked context.
 
 

Wednesday, January 21, 2009

The SOLID Principle

I've just been reading Uncle Bob's Principles of OOD and came across the SOLID principle. I'd heard this principle mentioned before but had no idea what it was. It's actually an acronym for 5 principles which are:

S - Single Responsibility Principle

O - Open Closed Principle

L - Liskov Substitution Principle

I - Interface Segregation Principle

D - Dependency Inversion Principle

 

Friday, January 16, 2009

Friday, January 9, 2009

Learning jQuery

I've been trying to get into jQuery for a while now and last time I took a look at it I blogged some jQuery Select Notes here. I've now started working throught a book called JavaScript: The Missing Manual by David Sawyer McFarland. The second half of this book is about jQuery and is by far the best resource that I've found for learning jQuery. I'm usually not a fan of books (because they normally aren't that great) and would rather find it on the web but this book is truly amazing and I would encourage anyone learning jQuery to get it.

Javascript: The Missing Manual

Why I like this book so much:

  1. He explains each concept clearly and unambiguously.
  2. He doesn't assume that you remembered what you read in a previous chapter so when he brings up the same concept he will repeat it briefly so that you don't have to page back and re-read that part but he also gives you a page number in case you want to.
  3. He has samples that you can download which included hands-on labs while working through the book as well as the completed pages.
  4. The samples are good. They cover realistic pages and code that you are highly likely to need.

Enough gushing about this book already, back to learning jQuery...

Friday, January 2, 2009

Are you logging in OR logging on OR signing in?

There's a good post on Stack Overflow titled UI Terminology: Logon vs Login and a great answer by Jeff Atwood. Jeff uses the number of hits when searching for each of the terms "login" (2,020,000,000), "sign in" (430,000,000), "logon" (27,700,000), and "log on" (18,200,000) to indicate what his preference would probably be.
There're a number of alternative uses that Google serves for this type of information. If I can't remember how to spell a word I'll enter it into the Google search box. If I get a "did you mean...." under the search box then I probably mispelled it. I also use it get the context correct if I'm battling between words such as effect and affect.
One use that I had for Google Trends recently was to try and work out when ObservableCollection became available in the .NET framework. It looks like the end of March 2008 marks that date.




Thursday, January 1, 2009

Canon PIXMA iP1600 from Server 2008 x64

I finally got my Canon iP1600 printer that's connected to my Server 2003 x86 machine via USB to work from my Server 2008 x64 machine and wanted to note how I did this in case I need to do it again in the future.

From Canon's PIXMA iP1600 page select the Drivers & Downloads tab. From the OS drop down select Windows XP (x64). Install driver with filename ip1600x64190dej.exe

Now find the printer on the network and add as printer.

Before doing the above I'd selected the Windows Vista x64 OS and had installed the Add-on Module for Printer Driver (aomvstea23us.exe) but I don't believe that this was neccessary.