Karla Carter's profileKarla's Coding and Stuff...BlogLists Tools Help

Karla's Coding and Stuff Blog

A .NET coding-themed blog for the whole world, if it wants
April 11

Reviving the Blog!

I decided I wasn’t quite busy enough ;-)

With VS 2010 set to launch in beta in May, and given that this blog barely covered VS 2005, let alone VS 2008, I figured it would be good for me to return and see what I can come up with.

Perhaps I can find a way to integrate ASP.NET with social networking and ethics :-D

As an interesting aside, my childhood kitty cat, Smokey, was born on April 11, 1975. (Why I can remember that, but not remember whether I’ve answered an email from the last few days I’ll never know…)

September 20

Comments Turned Off

I'm getting too many spamming comments, so, sadly, the only way to prevent that is to completely turn off comments. Sorry about that!

Yes, I'm Still Alive

My days are spent with my eternal companion, Blackboard. I'll occasionally point students to this blog, sadly outdated. In my vast amounts of spare time I could start making posts again...
August 14

Windows Live Writer

Microsoft has just released a nifty little blogging tool as beta: http://windowslivewriter.spaces.live.com/

I'm using it here for the first time, so I can't report much, but the download, installation and configuration experience was pleasant, as was composing this post.

I'm using it here with my Spaces account, but I could have used it with any of my numerous Blogger.com accounts, as well. In fact, I can wire it up to *all* of my blogs.

Very cool!

March 16

Not Only MMN...

I love the designation "Mystery Meat Navigation" from the folks at Web Pages That Suck. Part of my loathing of Flash sites is from MMN. (Mainly it's because Flash takes away my beloved right-click.)
 
One of the worst offenders I've seen is Chipotle. Try making your relatives in their 70s navigate through that site.
 
I was writing up a post about MMN and Web Pages That Suck in an online ASP.NET class I'm teaching and thought I'd see if Chipotle uses ASP.NET. I tested this by typing http://www.chipotle.com/default.aspx into the browser. Yep, they use it. Well, the server they are on has it installed (the 1.1.4322.2300 version).
 
Even if they aren't using ASP.NET functionality, they really should do something about the default ugly ASP.NET 404 screen (yes, I know most web developers don't anticipate folks randomly typing wrong addresses into the browser, but default 404 - any error, really - pages are a pet peeve of mine):
 

Server Error in '/' Application.

The resource cannot be found.

March 15

Learn New Stuff (Free), Get New Stuff (Free)...

 
Attend 3 ASP.NET 2.0 webcasts (free) or complete one Visual Basic E-Learning course (free) and receive a Visual Studio 2005 Discovery Pack (free).
 
The Visual Studio 2005 Discovery Pack includes:
  •     Microsoft® Visual Studio® 2005 Professional Edition 90-day DVD trial
  •     Microsoft Visual Basic® 2005 Jumpstart book from O’Reilly
  •     Discount voucher for Visual Studio 2005 Professional Edition with MSDN® Professional Subscription
  •     Microsoft Developer Security Resource Kit
  •     Discount voucher for Microsoft Certified Professional Exam
  •     Free 30-day hosting account

The site notes the offer is good until April 30, 2006 or while supplies last.

IdeaBlade-Lite for .NET 1.1 (because not everyone can move to .NET 2.0/VS2005...)

 
"IdeaBlade continues to offer "IdeaBlade-Lite" for .NET 1.1 development. IdeaBlade-Lite is the predecessor of  DevForce Express, and targets developer who have not moved to .NET 2.0 and Visual Studio 2005."
 
[IdeaBlade's DevForce itself is described as "a developer productivity solution for building applications for the Microsoft .NET Framework. DevForce is a 'must have' for developing and deploying .NET applications. With its integrated suite of infrastructure components, an application server, design tools, and full service support, you can create compelling .NET enterprise applications, on time, and on budget."]
 
 

VB.NET MDI version of Notepad

 
Some of my fellow MCTs were discussing this application and I decided to check it out. For those times when you wish Notepad had the ability to create multiple documents from the same window - it's even got the source code included.

Article: Ten of the Biggest Mistakes Developers Make With Databases

 
I've heard tales of overly normalized databases where the official US State abbreviations were in a lookup table...
February 25

Star Trek, Flying Vomit and Internet Explorer

Once upon a time I was trying to explain how Internet Explorer was integrated with Windows. The perfect analogy came to me: Star Trek - the episode where the alien creatures that looked like a flying version of that fake vomit they made when I was a kid attacked Spock and wrapped themselves around his nervous system. They couldn't get rid of the creature because it had become so integrated with Spock. I figured that's what IE did with Windows.
 
Note: I am not calling Internet Explorer flying vomit :-D
 
For years I used this analogy without knowing the name of the episode or the official taxonomy of the creatures.
 
 
Episode: "Operation: Annihilate!"
Creature: "large, amoeba-like aliens that attack humans and intertwine their tentacles with the body's nervous system" from Deneva.
 
Now, the fact that these parasites are described as using "excruciating pain as a means of controlling their victims" has nothing to do with my analogy, really....
 
:-D

The Mysterious "vshost"

 
"Its purpose is to provide support for improved F5 performance, partial trust debugging, and design time expression evaluation."
January 11

ASP.NET 2.0 Quickstart Tutorial

 
"The ASP.NET QuickStart is a series of ASP.NET samples and supporting commentary designed to quickly acquaint developers with the syntax, architecture, and power of the ASP.NET Web programming framework. The QuickStart samples are designed to be short, easy-to-understand illustrations of ASP.NET features. By the time you finish reading this tutorial, you will be familiar with the broad range of the new features in ASP.NET 2.0, as well as the features that were supported in earlier versions."

.NET Inspector for .NET Framework 2.0

 
"Welcome to the .NET Inspector, brought to you by Code Architects and the 2TheMax Team. Use this browser to explore all the assemblies, namespaces, types, and members in version 1.1 and 2.0 of the Microsoft .NET Framework; quickly determine which items have been added, removed, or modified from previous version; explore the .NET class library with search criteria that more flexible than those offered by the Visual Studio .NET object browser; learn more about each item by hovering the mouse on it."
 
 

StrongTypes: Free .NET Generics Help

 
"The focus of StrongTypes is to discuss and provide free, friendly, expert .NET Generics help. Generics is a new addition to the .NET Framework 2.0 and CLR that allows for type-safe code. Strongly typed code will not only decrease the overhead caused by type casting and late binding, but will also reduce the ammount of errors in your code."
 
January 06

A Low-Level Look at ASP.NET Architecture

 
"Many developers are familiar only with the high-level .NET frameworks like Web Forms and Web services that sit at the very top level of the ASP.NET hierarchy. This article discusses the lower-level aspects of ASP.NET and explains how requests move from Web Server to the ASP.NET runtime and then through the ASP.NET HTTP pipeline to process requests."
December 27

The Well-Tempered Exception

A classic article by Eric Gunnerson on building your own exception classes: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp

71-536 Beta

If you're planning on taking the beta of the TS: Microsoft .NET Framework 2.0 - Application Development Foundation exam, 71-536, check here for what you should study for: http://www.microsoft.com/learning/exams/70-536.asp
 
 
I took the test on December 20th. It took me about 2 hours once I got past the introduction screen. You will either know the stuff or you won't...

Hidden Gems in C# 2.0 (Printer-friendly repost)

from http://www.ftponline.com/channels/net/2005_08/wagner_hiddengems/ - requires free registration so I've posted the printer-friendly version here in its entirety
 

Hidden Gems in C# 2.0
Lesser-known features in C# 2.0 will help you write less code that does more.
by Bill Wagner

August 22, 2005

You've likely heard about the four major features added to the C# language in version 2.0: Generics, Iterators, Partial Types, and Anonymous Delegates. But many other features were added to this new version of C#. These other features will help you express your designs more clearly, in less code. You should become familiar with these enhancements to your favorite language now. That knowledge will help you create code today that can be easily enhanced tomorrow. You want your code ready for C# 2.0 when you are.

Static Classes
You've probably already created classes that contain only static methods and fields. These methods are essentially global utilities for your application domain. It's not a sign of a bad design; the .NET Framework System.Math class is an example of this construct. C# 2.0 adds a new use for the static keyword for just this purpose. Static classes have a number of restrictions, which communicate your design intent to other developers:

  1. Static classes cannot have instance constructors. This restriction means that developers cannot create an instance of any object of a static type. You are communicating that your static class is a means of organizing similar functionality. A static class is not an abstraction that will be used to create other types or instances of objects. From this first restriction, others follow:
  2. Static classes are implicitly sealed. No developer can create a class that is derived from a static class.
  3. Static classes cannot have instance members. If you can't create an instance of the object, it makes no sense to define instance members. (Obviously, this also means that static classes cannot have virtual members.)

To achieve similar behavior in C# 1.x, you define a private constructor in your class, and you define your class as sealed and abstract. Defining it as sealed prevents users from creating derived classes; defining it as abstract, combined with the private constructor, prevents users from creating an instance of this class. As you move to C# 2.0, use static classes for this idiom, because the same constructs implement the singleton pattern.

Accessor Accessibility
It's common to create a class where you have a public read-only property, and yet you want to allow derived classes or other classes in your assembly to modify the value of the property. In C# 1.x, you wrote this:

public int MyValue
{
  get { return val; }
}

protected void SetMyValue( 
  int theNewValue ) 
{
  This.val = theNewValue; 
}

In C# 2.0, you can specify a more restrictive access to the set (or get) accessor. This creates much cleaner code that is easier to maintain. The access modifiers can only decrease the visibility of the property; you cannot increase the visibility using the access modifier:

public int MyValue
{
  get { return val; }
  protected set { val = value; }
}

That's a fine start, but there are a few other ways to use property accessor accessibility to write cleaner, easier-to-maintain code. First off, you can create a private property setter to use when you modify the value inside your class. That provides you, as the class author, with a single location in the code to write any validation on modifications to that value:

public int MyValue
{
  get { return val; }
  private set 
  { 
    if ( value  < 0 )
      throw new 
      ArgumentException(
       @" non-negative only" );
    val = value; 
    // Fire change event to
    // notify client code that a
    // value changed.
  }
}

This same technique applies when you implement an interface that contains a read-only property. If the interface does not contain a definition for the property setter, you can add your own, at any access level that is appropriate. However, if the interface did contain both a set and get accessor, you cannot specify different accessibility to the implementations in your class:

public interface IFoo
{
  public int MyValue
  {  get; }
}

// elsewhere:
public int MyValue
{
  get { return val; }
  // Access modifier allowed.
  // IFoo does not define a set 
  // method.
  private set 
  { 
    if ( value  < 0 )
      throw new 
      ArgumentException(
       @" non-negative only" );
    val = value; 
    // Possibly fire events to
    // notify clients of changed
    // state.
  }
}

You can use the same technique with virtual and overridden property accessors. However, the accessibility must be defined in the base class, and cannot be modified in any of the derived classes.

The purpose of access modifiers on properties is to create more consistent code: A property represents a data element exported from a type. Different accessibility can be used on the set and get methods to keep the code that accesses and modifies a value more cohesive.

Friend Assemblies
Friend assemblies provide a method to allow another assembly to be granted access to the internals of a given assembly. You do this through the use of an attribute:

[assembly:InternalsVisibleTo(
  "AFriend")]

This designation provides AFriend.dll with access to all internal types, methods, fields, and properties in any type declared in the assembly where the attribute is defined.

I'd avoid using this in most production code. If you have internal types that need access from another assembly, that often indicates a poor design.

The right solution is to refactor those classes so that you can provide access only through public interfaces, or public classes. However, there is one idiom that I've found useful: unit tests for internal types and methods. I can create a separate assembly for all my unit tests, and make that assembly a friend of the target assembly. I separate all my unit tests in a second assembly, which means I don't need to deliver the unit tests to customers (or limit my unit tests to debug builds). In addition, I can still write unit tests for all my internal classes.

Nullable Types
Nullable types were developed to simplify creating C# logic that uses data retrieved from databases. Most databases provide a facility to define a missing (or null) value in a given column and record. This missing value is difficult to represent in .NET value types: Which integer value should be used to represent a nonexistent value? There is no one universal answer. So, the C# and CLR teams introduced the idea of a nullable value type.

A nullable type is a wrapper around a value type that can represent all the possible values of its underlying type, and can also hold the null value. Nullable types sound simple. But the more you examine the subtleties of the concept, the more complicated they become. The syntax is simple: You append a '?' to a value type to produce a nullable type. (This is a shorthand notation for a generic class, nullable <T>, where T must be a value type.) For example, int?, char?, and bool? all define nullable types for the respective underlying value types. However, because the string class is a reference type, you cannot define a nullable string. Of course, because the string class is a reference type, you don't need to, either.

When you use nullable types, you must remember that the nullable type is not a simple replacement for the value. In fact, nullable types introduce quite a few complications into your program logic. You'd best beware of all the pitfalls before you venture into nullable types where the regular value type would suffice. In these examples, I'll use integer nullables, but any nullable type would exhibit the same problem.

First, a regular integer cannot be assigned the value of a nullable type without a cast, or accessing its underlying Value property:

int? maybeAValue;
int val = maybeAValue.Value;
int val2 = (int ) maybeAvalue;

Both these assignments compile, but a System.InvalidOperationException will be thrown if maybeAValue stores null. That's just extra work. Any time you access the value inside a nullable type, you must check whether the instance has a valid value using the Nullable<T>.HasValue property. In fact, this idiom is common enough that the C# language has included a special syntax to perform assignments with nullable types, the ?? operator.

The ?? operator returns the value of the left operand if the operand has a value, and the right operand otherwise. For instance:

int result = maybeAValue ?? 0;

returns 0 if maybeAValue is null. Otherwise, it returns the value stored in the nullable maybeAValue.

Things get even more complicated when you start boxing and unboxing nullable types. The CLR and C# teams recently made a change to nullable types that creates more cohesive behavior when nullable types are boxed and unboxed. The CTP release that incorporates the change was being released as I was finishing this article, so I have not had the chance to work with the new code yet. Click here for more details.

Miscellaneous
There are a few other additions that warrant mention, but won't affect your daily coding as much. You can now enable and disable warnings inline in code. This will make it easier to get large projects to compile cleanly at higher warning levels. You can selectively disable certain warning conditions after reviewing the specific code that generates the warnings. Similar to the C++ compiler, the syntax is a pre-processor pragma:

#pragma warning disable 3021

C# 2.0 adds the ability to create fixed block arrays, but only in unsafe code blocks. You can use this feature to provide buffers that can be used between managed and unmanaged code layers. For instance, this structure takes 4 bytes on the stack, and the myArray variable is a reference that points to a different location in managed memory:

public struct MyArray
{
  public char MyName[50];
}

However, in C# 2.0, you can ensure that the array is stored inline in the structure, as long as the structure is used only in unsafe code:

public struct MyArray
{
  public fixed char MyName[50];
}

This second declaration takes 100 bytes. (Fixed size char buffers take 2 bytes per char.)

Finally, C# delegate declarations support Covariance and Contravariance. Covariance means that a delegate handler may specify a return type that is derived from the return type specified in the delegate signature. Contravariance means that a delegate handler may specify base classes for derived class arguments specified in the delegate signature. Because of covariance, this compiles:

public delegate object 
Delegate1();

public static string func();

Delegate1 d = new Delegate1(
  func );

func returns a type (string) that is derived from the type defined in the delegate signature (object).

Because of contravariance, this compiles:

public delegate void 
Delegate2( string arg );

public static void func2( 
  object arg );

Delegate2 d2 = new Delegate1(
  func2 );

func2 accepts a base class (object) of the type (string) defined as the argument for the delegate method. These additions will make it easier for you to create event handlers that handle multiple events: You can write one handler that receives a System.EventArgs parameter, and attach it to all events.

I've given you a small taste of some of the new features in C# 2.0 that have seen less coverage compared to the major new initiatives. But these features will change the way you write C# code, and change it for the better. You'll be able to create code that better expresses your designs, and is easier to maintain. By learning these features you'll be able to write less code that does more. That's what we all need.

About the Author
A commercial software developer and co-founder of SRT Solutions, Inc., Bill Wagner facilitates adoption of .NET in clients' product and enterprise development. His principal strengths include the core framework, the C# language, Smart Clients, and service-oriented architecture and design. In 2003 Microsoft recognized his .NET expertise, appointing him Regional Director for Michigan. Bill is the author of Effective C#. Reach him at wwagner@srtsolutions.com.


© Copyright 2001-2005 Fawcette Technical Publications

Passwords, Folders and Tools (oh my)

 
"NirSoft web site provides a unique collection of small and useful freeware utilities, all of them developed by Nir Sofer."
 
The coolest things (IMHO) on NirSoft are the password recovery utilities that allow you to get at all those pesky asterisk-masked passwords you've stored in Outlook, IE, FTP clients, etc.
 
He also includes some code samples so you can see how the magic is accomplished.
November 22

Poking My Head Out Again

Obviously November became a busy month! I'm trying to catch up on posting all these nifty snippets I'm keeping in my Firefox SessionSaver log. Hopefully I will be all up to date by the end of the month.