9.1.08

Method Hiding

I ran into a surprise today in C#, with a probably overly-complex system of interfaces and abstract classes. Here's some sample code:
    class Program
{
static void Main(string[] args)
{
IFoo b = new B();
b.DoWork();
((B)b).DoWork();

Console.ReadKey();
}
}

interface IFoo
{
void DoWork();
}

class A : IFoo
{
public void DoWork()
{
Console.WriteLine("A Does Work");
}
}

class B : A
{
public new void DoWork()
{
Console.WriteLine("B Does Work");
}
}

Output is:
A Does Work
B Does Work


I mistakenly expected both lines to say "B Does Work." After all, that's the point of inheritance, right? I instantiated a B, so it should map to B.DoWork(). Well, it doesn't.

The explanation is in the vtables AND the fact that I defined B.DoWork as new. I explicitly stated (without realizing) that it was not connected to IFoo's method. The vtable tried to resolve DoWork and got as far as it could..to A.

The correct way to implement this is by defining DoWork as virtual in A, then marking it override in B.

The code I present here would be useful if one wanted to define an implementation of a method that executed only when the method was casted to that specific type, rather than as a base class or interface. Not that I know of any such scenarios ..

8.1.08

Visual Studio 2008 as a WPF editor

I've started learning WPF and writing XAML in Visual Studio 2008. Granted, I've haven't used any other editors like Expression Blend, but I find VS2008 extremely lacking, in both the visual editor and the text editor.

I never liked using Visual Studio's (2003 and 2005) visual web page editor either. It never really created the HTML the way I wanted, and it tended to destroy my beautifully-indented and formatted markup. Moreover, once you start doing complex Ajax events, everything falls to pieces. But the real problem is handling CSS.

The WPF editor is terrible for essentially the same reason--it's trying to model the old-school absolute positioning in a language which is designed to be non-absolute. You can drag and drop all of your elements onto a window, but they get ridiculous margins. Instead, you need to add some StackPanels, DockPanels, and so forth. Can you do this with the editor? Yes. Is it easier by hand? Yes.

Then my second complaint is that the Intellisense sucks. It works for most elements and attributes, but there is no popup documentation provided inline, like it is for C# and ASP. And the Intellisense just don't work for anything in strings.

<Button Margin="0,0,3,0" Content="{Binding Foo.Bar}" />


It doesn't inform you that Binding is possible inside the brace. It will actually color-code the first thing you type in a brace, regardless of whether it's actually valid. Secondly, this will only work if the DataContext for the Button (or its parent) has a Foo property, and if that Foo property has a Bar property. You will get a nice compile-time error if that's not the case, BUT I would really appreciate a nice intellisense list so I didn't spend half my time looking up class definitions.

I'm really liking WPF's model, and it only gets more interesting as I learn more. However, I wish the tool was a little more advanced so that it could actually enable me.

5.1.08

Browser Wars

I was doing some reading about the recent re-ignition of the browser wars (okay, it's a 2004 article, but still relevant). One argument for the still-dominating popularity of Internet Explorer is that it comes installed on most computers sold in the world.

That's true, it does, and until Microsoft is slapped on the wrist again for monopolizing the market, it will continue to do so.

I think that while this was a huge factor in the past, we are going to see a distinct change in the next few years. In fact, this has already happened. Ever since the spyware boom, Firefox has been handed to the masses as a, "Here, this will keep your computer from getting infected," preventative measure. And it worked.

IE6 is the most popular browser in the world, but also the most spyware/malware-riddled. Firefox may have many slick features that IE6 doesn't, but it's possibly best known as a way to keep your computer running smoothly. And those features don't hurt, either, which is why average computer users are downloading it to install on their newly purchased computers.

IE7 was a much needed upgrade, fixing countless CSS problems, improving rendering and DOM performance, and tightening security all around. However, Microsoft didn't require the upgrade, and releasing it instead as a downloadable installer. This is critical because it places IE7 on the same level as Firefox--users have to look for it, download it, and install it themselves.

Similarly, there are also photo-enthusiasts who download software like Google's Picasa to edit their digital photos and upload them to the web. They aren't necessarily technologically-saavy, but they have heard about this software/service, and want it.

I think it's clear that we are in a period where ordinary people expect to get software and services off the web. Vista does include IE7, so Internet Explorer's market share isn't going anywhere too quickly. However, I think the domination of the OEM bundle is nearing its end, and this will have a significant effect on browser popularity trends.