Common Bug #23

A common bug occurs when we need to check if a collection has at least one element.

bug2.JPG

Note thatĀ if there are no elements in the collection (ArrayList) an ArgumentOutOfRangeException is thrown (compared to an IndexOutOfRangeException).

The correct way to check would be to use,

bug3.JPG

Note that you should also check that myArrayList is not null before accessing the Count property.

Casting in C# (Part II)

One caveat to remember while type checking in C#, suppose you have a ‘Derived’ class that derives from a ‘Base’ class, then there is a difference between the following,
base1.JPG
The ‘GetType()’ check fails and the ‘is’ check is possibly what you want.

Casting in C# – the ‘as’ and ‘is’ operators (Part I)

There are different ways to cast one type to another in C#. Lets try compile-time first,

cast5.JPG

This gives us an error at compile-time, so we know we need an explicit cast. Lets try the old C style,

cast1.JPG
This causes a CLR type safety check (CLR checks if ‘obj’ is cast-able) and an invalid cast exception is thrown if ‘obj’ is not cast-able to ‘XmlDocument’. Lets check if it can be cast ourselves,

cast2.JPG

This way, even though there are two type checks, we gain by not having to worry about an exception being thrown if the cast fails – we know it won’t fail.
C# has introduced two new keywords that make casting simpler. The ‘is’ keyword pretty much does what we did in the example above (see this for an important difference), it checks if ‘obj’ is cast-able to XmlDocument,

cast3.JPG
The snippet above actually causes two CLR type safety checks. This can be optimized by using the ‘as’ keyword.

cast4.JPG

The ‘as’ keyword is guaranteed to never throw an InvalidCastException. If the cast is unsuccessful the result is null. This way only one type check is required to cast and also we don’t need to worry about exceptions being thrown.

Uncommon Windows XP Keyboard Shortcuts

Some of the more uncommon shortcuts that I use when navigating Windows XP,

  1. Alt + Enter [Display properties for the selected item]
  2. Alt + Esc [Cycle through items]
  3. Alt + Spacebar [System menu for active window]
  4. Alt + F4 [Close active window]
  5. Shift + Del [Delete item permanently (without putting in RecycleBin)]
  6. Pressing Ctrl while drag&drop copies the item instead of moving it.
  7. Ctrl + Shift + Esc [Processes tab of the Windows Task Manager]
  8. Win + L [Lock Desktop]
  9. Win + M (or) Win + D [Minimize all windows]
  10. Win + E [Windows Explorer]

Events and Derived Classes

Events defined in the base class cannot be raised by a derived class. Events can only be invoked by the class that declared them. For example, the following gives a compile-time error,

Event1.JPG
To get over this, we can simply define a protected method in the base that will raise the event. This protected method can then be called by the derived class, like this,

Event2.JPG

If the ‘RaiseMyEvent’ method in the base class is declared virtual then the derived class can override it and provide custom processing before or after the base class raises the event,

Event4.JPG

Also, note that the derived class can, of course, hook up to the base’s events. Heres the complete example,

Event3.JPG