« Smart Client Offline Application Block (SCOAB) Demo | Main | Scott and Rory sitting in a tree »

March 16, 2005

Granular late binding : VB Whidbey Fun

I was doing some stuff with performance counters in Whidbey when I realized that they just didn't work for a couple of things that I wanted to do. % Disk Time is just broken, and I couldn't use a network performance counter because .NET truncates the instance field to 64 characters.

No biggie. I'll just inherit PerformanceCounter, and make a couple of specializations that work for network and disk usage. Crap. PerformanceCounter is sealed.

Ok, generics to the rescue. I'll just make a:

public void DoAnalysis(PerfCounterType counter)
{
// some stuff
counter.NextValue();
// some stuff
}

Won't compile because the compiler can't verify that what's passed in actually exposes a NextValue method (well, in this case, the compiler could verify it, but it couldn't in every case). Ok, this is what constraints are for. I should be able to do something like:

public void DoAnalysis(PerfCounterType counter) where PerfCounterType: PerformanceCounter, CustomPerfCounter
{
// some stuff
counter.NextValue();
// some stuff
}


Crap. PerformanceCounter is sealed and you can't use a sealed class as a constraint.

Ok, the following is not, (I repeat, NOT) what I actually did, but what did pop into my head was something interesting that VB + Whidbey makes possible.

I could have a partial class where most of my code is, and have Option Strict On in that class (early binding).

Then, I could make another file with another portion of the partial class that contains:

Option Strict Off
public void DoAnalysis(Object counter)
{
// some stuff
counter.NextValue();
// some stuff
}

In other words, with partial classes and VB.NET, most of your code can be option strict on, but you can set up individual methods to be option strict off (late bound). You couldn't do this with VS2003, because you couldn't break a class across multiple source files. Fun.

Posted on March 16, 2005 at 03:21 PM | Permalink

Comments

Revenge of the Variant

I've often thought it would be cool to mark one code section "Option Strict Off".

It'd also be cool to embed C# and VB.NET side by side, the same way we already kinda-sorta do with regular expressions and SQL. Those are really languages in their own right, and incredibly useful ones.

Posted by: Jeff Atwood | Mar 17, 2005 3:01:13 PM

The comments to this entry are closed.