Wednesday, January 30, 2008

C# 3.0 - New Language Features - Part 2

Implicitly Typed Local Variables

Implicitly typed local variables is a new compile time feature. Here is an example:

var implicitVariable = "Hello Implicitly Type Variable";
Debug.WriteLine(implicitVariable.GetType());



The above code snippet's output will be "System.String"



When one looks at the code above - the obvious question that comes up is how is "var" different from using object. Because the above code could be re-written as follows:




object oldVariable = "Hello old variable declaration";
Debug.WriteLine(oldVariable.GetType());



And the output would be exactly what the implicit type variable code would output "System.String".


So what is the point?


First of all - var - implicitly typed variables - are a compile time feature. Hence one needs to assign the var variable when its used. Thus the following are illegal statements:



var illegalStatement1;
var illegalStatement2 = null;



The reason that the above statements are illegal is that the compiler decides what the type of var variable is based on the assignment statement. In fact if you were to look at the IL code - using var or the actual data-type would result in the exact same code.




var implicitVariable = "Hello Implicitly Type Variable";
string oldVariable = "Hello old variable declaration";

Converts to

L_0008: ldstr "Hello Implicitly Type Variable"
L_000d: stloc.0
L_000e: ldstr "Hello old variable declaration"
L_0013: stloc.1



Another cool features connected with the var keyword? Because the implicitly typed variable is a compile time feature and the variable is assigned a type based on the assignment statement, one has full intellisense available for that variable.



Hence - in the following screen shot - intellisense for the string datatype members is available for the variable implicitVariable, but not for the variable oldVariable - which is of type object.



5 6



The place that I have found the var keyword most useful is when I need to enumerate over a collection. Often times one doesn't know what the type of the returned elements are when performing a foreach enumeration. By using the var keyword - one doesn't need to know the exact type of the returned objects and yet intellisense will be available for the objects - making it easy to discover methods and write code.




Dictionary<string, string> simpleDictionary = new Dictionary<string, string>();
simpleDictionary.Add("Hello", "A greeting in english");
simpleDictionary.Add("Raj", "Name of owner of this blog");
simpleDictionary.Add("brachypterous", "Having very short or rudimentary wings, as certain insects");

//old form of iteration
foreach (KeyValuePair<string, string> kyPair in simpleDictionary)
Debug.WriteLine(kyPair.Key + " " + kyPair.Value);
//new form of iteration using var keyword.
//use of var - makes intellisense available on the values of the enumeration
foreach (var dictionaryItem in simpleDictionary)
Debug.WriteLine(dictionaryItem.Key + " " + dictionaryItem.Value);



7



 



 



Intellisense available on the dictionaryItem as it was defined as a implicitly typed variable (var).



 



Finally, as this is a C# 3.0 feature - it can be used with modules that have been built against version 2.x of the .NET framework and is not isolated only to the 3.x versions of the framework.



The above is an example of a very simple use of the var type. People get most excited when it is used with LINQ - language integrated queries.



Next up - Automatic Properties.

Tuesday, January 29, 2008

Visual Studio 2008 - New Features - Part 1

In the next few posts - I am going to discuss the new features that are available in Visual Studio 2008, as well as those that are part of C# 3.0 language specification.

VS 2008 Feature : Multi-Targeting Support

Most of us are familiar with platform targeting. This is where we can build a module so that it can run on "Any CPU", "x86" machines and "x64" machines. This platform targeting can be done via the project's "Build" property page.

1

But to build .NET modules that work with a certain .NET framework - required you to use the appropriate version of Visual Studio

Visual Studio Version .Net Framework Version
VS 2002 1.0
VS 2003 1.1
VS 2005 2.0

This made it a royal pain if you had to support multiple .NET frameworks (which typically required one to keep different versions of project and solution files - for each version of Visual Studio/.Net Framework that one wished to use).

With VS2008 - the multi-targeting support feature allows one to use the IDE to define the target framework. This makes it extremely easy to create .NET modules that target different versions of the .NET framework.

The only caveat - versions 1.0 and 1.1 are not supported. VS2008 allows one to re-target to 2.0, 3.0 and 3.5 versions of .NET.

There are two ways in which one can specify the target .NET framework. When you create a new project, the new project dialog has a combo box that allows you to specify the framework version.

2

The second method is used to re-target framework version once the project has been already created: Accessed via the project's "Application" property page.

3 

Being able to re-target the framework - doesn't only simplify project management - but also allows you to use all the great features that are part of VS 2008.

The re-targeting is very well integrated into IDE. Once you choose a new framework target the "Add References" dialog will gray out dlls that aren't made for that framework.

4

In addition - the toolbox will show only those controls that are again meant for that framework.

For more information about this feature check - Scott Gu's blog - VS 2008 Multi-Targeting Support

Thursday, January 24, 2008

An Extensive Examination of Data Structures

An older set of articles that goes into depth on data-structures used often by software engineers. These were authored by Scott Mitchell from 4GuysFromRolla.com

Part 1: An Introduction to Data Structures
Part 2: The Queue, Stack, and Hashtable
Part 3: Binary Trees and BSTs
Part 4: Building a Better Binary Search Tree
Part 5: From Trees to Graphs
Part 6: Efficiently Representing Sets

More C# examples can be found at http://msdn2.microsoft.com/en-us/library/aa289528(VS.71).aspx

Will mouse clicks go the way of the dinosaurs?

1If this site has its way - then in the future we may no longer have to perform mouse clicks. The dont click it site promotes a no-click mouse input interface as a much more efficient and ergonomic way to give input to a computer.

Instead of mouse clicks - one uses the hover time or mouse gestures to determine if an object has been selected or "clicked".

Having tried the site - I found it hard to stop clicking on links and buttons out of force of habit. But I am sure such an interface would definitely keep my thumbs going strong for many more years.

www.dontclick.it

 

2 A statistic from the Dont Click It site. It is interesting to see how many of us have become hard-wired to use mouse-clicks even on a site that requires no mouse clicks.

Tuesday, January 22, 2008

Compiler Features in C#3 and VB9

from: The Moth: Compiler Features in C#3 and VB9

OpenSource project - SpatialViewer

Spatial Data Viewer


A C# OpenSource project for easy viewing of spatial data.
The current release has the following functionality: (from summary page)

  1. Draw points, Lines and polygons
  2. Auto close lines and polygons
  3. Draw with Freehand or with fixed lines(using shift key)
  4. Zoom in and out of drawing (scroll bars not yet implemented)
  5. Enter 1 or geometries by text.
  6. Text Geometries are validated and errors displayed
  7. Display geometries in text and graphical form
  8. Select and highlight geometries from a grid
  9. Select geometries using drawing pane by using another geometry (lasso style selection)
  10. Snap to grid. Restricts points to whole numbers

The Spatial Data Viewer project was started by SimonS whose blog is a good resource on SQL Server 2008 - Katmai. (Many of his latest entries are on using SQL Server 2008 with spatial data). http://sqlblogcasts.com/blogs/simons/

Friday, January 18, 2008

ZIP Code Information

In connection with my previous post on visualizing US ZIP codes, here are some links to ZIP code data.

From: http://www.sdc.ucsb.edu/holdings/zip_codes.txt

The Census Bureau has been providing ZIP Code area centroids which is probably the dataset used in the distance calculation routines. To overcome this mapping difficulty, the Census Bureau has created the ZIP Code Tabulation Area (ZCTA) by using aggregations of Census blocks. But they warn that there is no correlation between the US Postal Service ZIP Codes and the US Census Bureau-created geography of those ZIP Codes. Each of datasets should be used with full knowledge of their method of creation and intended uses.

US POSTAL SERVICE ZIP Code Lookup and Address Information page http://www.usps.com/ncsc/ziplookup/lookupmenu.htm

ZIP Code FAQ
http://www.usps.com/zip4/zipfaq.htm

ZIP Code lookup service
http://www.usps.com/zip4/welcome.htm

US CENSUS BUREAU ZIP Code information and FAQs http://www.census.gov/geo/www/tiger/tigermap.html#ZIP
http://www.census.gov/cgi-bin/geo/tigerfaq?Q16
http://www.census.gov/cgi-bin/geo/tigerfaq?Q17
http://www.census.gov/cgi-bin/geo/tigerfaq?Q18
http://www.census.gov/cgi-bin/geo/tigerfaq?Q19
http://www.census.gov/cgi-bin/geo/tigerfaq?Q20

Census 2000 U.S. Gazetteer Files
http://www.census.gov/tiger/tms/gazetteer/
http://www.census.gov/geo/www/gazetteer/places2k.html

ZIP Code Tabulation Areas
http://www.census.gov/geo/ZCTA/zcta.html
http://www.census.gov/prod/2001pubs/mso01zcta.pdf

2000 5-Digit ZIP Code Tabulation Areas
http://www.census.gov/geo/www/cob/z52000.html

2000 3-Digit ZIP Code Tabulation Areas
http://www.census.gov/geo/www/cob/z32000.html

Text file of ZIP Codes and centroids
http://www.census.gov/ftp/pub/tiger/tms/gazetteer/zips.txt http://www.census.gov/geo/www/gazetteer/newpagefiles/zip90r.txt

1990 ZIP Code file with lat/lon centroids
http://www.census.gov/geo/www/tiger/zip1999.zip

MISCELLANEOUS

Zip Codes are Not Areas! http://exchange.manifold.net/manifold/manuals/5_userman/mfd50Zip_Codes_are_Not_Areas.htm

As with most public domain data, there are many commercial enhancements and services created with specific uses of the data in mind. Here is just one such company:
http://www.zipdatafiles.com/data/

Zip Codes - the method behind the madness

Ever wondered if there is pattern behind the U.S zip code system? Here is a very nice interactive tool that allows you to discover the pattern behind the pin code system. zipdecode | ben fry

1 

Once you have figured out how the zip codes are spread out across the U.S, check out this site, which visualizes the pin codes by connecting them up in ascending order. (ZIPScribbled)

scribbled

And in this map, the zip codes were unraveled using a Hilbert Curve. (Travelling Presidential Candidate Map)

scribbled2

Thursday, January 17, 2008

Making the Most of Maps - Google Docs

A very good presentation covering what one should do at a minimum to make their mashup maps better.

Making the Most of Maps - Google Docs

Wednesday, January 16, 2008

Registry 101

An excellent article on some basic information every developer should know about the registry. The article touches on information such as how the data is stored, where its stored as well as where some of the important pieces of information that developers typically need access to are stored.

For example:

Did you know that the CLASSES_ROOT reflects the CLASSES information from the local machine and current user hives?

Read more at : Ask the Performance Team : Windows Architecture - Registry 101