Donahue, Crash Scene Investigator

Red Gate Support Engineer

Thanks for nothing!

Published Wednesday, September 05, 2007 9:13 AM

When Fibonacci introduced the number zero to Europe, it must have blown their minds. People in the Fertile Crescent had been using zeros, probably for centuries, but for the people of this continent the concept of a symbol that meant 'nothing' was probably hard to grasp.

Enter the modern computer, which has the capacity to bewilder me on a daily basis. It can not only calculate numbers on both sides of 'nothing', but it also has many wholly separate ways of expressing 'nothing', none of which are compatible with each other. So for me, a hobbyist C programmer in my distant past, grappling with .NET's multiple ways of expressing 'nothing' was akin to the feeling that ancient people had when a mathematician attempted to express the idea of zero to civilizations used to counting in Roman numerals.

For instance, thanks to implicit type conversions in the C compiler (at least the one I was using), you could do this:

int i=0;
if (!i) printf("true");
if (i==0) printf("true");
if (i==NULL) printf("true");

This logic does not compute in .NET, however. I first came across this when trying to integrate an ActiveX web browser into a Windows Forms application a few years ago. All that I wanted to do was a simple navigation:

axWebBrowser1.Navigate("about:blank",null, null, null,null);

The last four arguments to the Navigate method were not important to me. I only wanted to make a GET request for "about:blank", but the line above caused a compiler error: Cannot convert from '<null>' to 'ref object'. I need to pass a ref object, which is clearly 'something', but it must have a value of 'nothing'! After pondering this for a bit, it was necessary to lie on the sofa with a damp washcloth on my forehead and turn out all the lights to soothe my aching head.

After a recovery period and a beer, I beset Google to try to find the answer and uncovered some information about the trinity of 'nothings' in .NET: Missing, Nothing (null), and Zero.

Nothing seems to be imbued with magickal powers that can cause real objects to be disposed of by the runtime. For instance, a static object will never be garbage collected unless its' value is set to null, or Nothing in VB .NET. Missing is a special type of object in .NET that surprisingly doesn't cause a universe-crushing paradox by simultaneously being 'nothing' and 'something' at the same time. Zero is our old, comforting friend: although just a number, one that signifies an absence of quantity.

My determination was that a 'Missing' object was the right choice for signifying to the Navigate method that I didn't want to argue unnecessarily:

public object empty = System.Reflection.Missing.Value;
axWebBrowser1.Navigate("about:blank",ref empty, ref empty, ref empty,ref empty);

Coincidentally, I had come across a use for null just yesterday, when creating a sample project to demonstrate finding a memory leak using ANTS Profiler. Needless to say, the demonstration was a success, although not in the way that I had planned.

using System;
using
System.Collections.Generic;
using
System.Windows.Forms;
using
System.Threading;
namespace
EventsExample

{

static class Program

{

static Form1 theForm;
[STAThread]
static void Main()
{

Control.CheckForIllegalCrossThreadCalls = false;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

theForm = new Form1();
Thread workThread = new Thread(new ThreadStart(DoWork));
workThread.Start();
Application.Run(theForm);
theForm.Dispose();

}

public static void DoWork()

{

CacheManager c = new CacheManager();
c.CacheClear += new CacheEventHandler(theForm.c_onCacheClear);
c.CheckCache();
}
}

The demonstration was meant to prove that if the DoWork method was running in a background thread, then it was holding a reference to an instance of Form1's c_onCacheClear method and therefore theForm would not be garbage collected after the window is closed, but I got more than I bargained for. Have you spotted it yet? Yes, the 'theForm 'object is declared as static and ANTS Profiler reports that the object is live after closing the window. Adding 'theForm=null;' after Application.Run(theForm); will get the garbage collector to dispose theForm.

Understanding the meanings behind .NET's three nothings is another way to help improve the code that you write.

Comments

 

Phil Factor said:

I'm very suspicious of your link that argues that Fibonacci introduced the Zero concept to Europe. He introduced the word 'zephyrum' certainly, but the concept was already well entrenched. The way that a "calculating-table" (abacus) works depends on the idea.  These devices were probably used in their original form by the ancient egyptians, and were adopted enthusiatically by the Phonecians, Greeks and Romans. Many have been discovered by archaeologists but were often originally mistaken for gaming boards. The Hindu and Arabs were very late in the game, but their decimal notation system certainly caught on in the end.
September 10, 2007 12:00 PM
 

Brian Donahue said:

I was just surprised to learn that the use of the abacus was not confined to the Far East.

By the way, what's the plural of abacus? abaci? abcusses? :-)
September 11, 2007 4:23 PM
You need to sign in to comment on this blog


















<September 2007>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Finding Stuff in SQL Server Database DDL
 You'd have thought that nothing would be easier than using SQL Server Management Studio (SSMS) for... Read more...

Mission Critical: SQL Server 2008 Performance Tuning Task List
 In which Buck Woody imagines how the US military would have tackled DBA checklists for... Read more...

Simple Query tuning with STATISTICS IO and Execution plans
 A great deal can be gleaned from the use of the STATISTICS IO and the execution plan, when you are... Read more...

Switching rows and columns in SQL
 When they use SQL Server, one the commoner questions that Ms Access programmers ask is 'Where's the... Read more...

Writing Efficient SQL: Set-Based Speed Phreakery
 Phil Factor's SQL Speed Phreak challenge is an event where coders battle to produce the fastest code to... Read more...