Skip to main content

Option Smug Off

I looked at VB.NET for about 5 minutes but I can’t see a single compelling reason to use it. A few syntax constructs remain but basically it’s a new language and one that does not appear to be as well supported as C#. Whichever language you use, you still have to learn the Winforms way and the .NET API. The extra learning involved in C# syntax is negligible


C# provides excellent support for early binding to COM. You just import the COM reference, some magic wrapper code is created and you pretend you're using a .NET object.

So far so good.

Unfortunately, in some circumstances you don't want early binding, you want late binding.

There are scenarios for which late binding is justified. You may not know the exact binary interface until runtime. The object may exist in multiple versions and have improperly adapted interfaces between the versions.


This is where things become a pain in C#. You have to use reflection to invoke everything, producing such beautiful code as:



// overloaded method, can pass the server name as second
// parameter if necessary.
Type myType = Type.GetTypeFromProgID("TestObject.TestClass");

// heavily overloaded method call. Can also be used with
// assembly file names if you have them.
object myObject = Activator.CreateInstance(myType);

// method parameters are passed in an object array.
// Must set these up before invocation
object [] oParms = new Object[]{"Echo This"};

// Invoke the method and cast the result to the correct
// object type.
string myString = (string) myType.InvokeMember("Echo1",BindingFlags.InvokeMethod,null,myObject,oParms);

MessageBox.Show(myString);



That's a lot of typing. Now I'm a VB6 man and that code above in VB6 can be sumarized as:



Dim MyObject As Object
Dim MyString as string

Set MyObject = CreateObject("TestObject.TestClass")
MyString = MyObject.Echo1("echo this back to me")

MsgBox MyString



The difference is rather shocking. When I saw this reflection nonsense I was struck immediately that this would never fly in VB land. Of course it doesn't and VB.NET has a similar syntax to VB6. You just need to turn Option Strict to Off - you can write code in all it's late bound glory.

All of a suden my smug assertion that there are compelling reasons to use VB.NET looks a little weak.

The ultra infuriating thing about this of course is that VB.NET produces the same code as C#. There's no reason that C# shouldn't be able to do this too. Once again we see that .NET's menu of languages isn't an advantage. You only notice that it exists when you come across some feature that the language you are writing in can't manage that some other language can.

It's yet another reason to hate .NET, as if I needed one of those.

Comments