Why we need Option Strict Off
Posted
Friday, December 31, 2004 1:07 AM
by
bill
I was reading Bill Ryan’s blog rant about Option Strict Off, and although I whole heartedly agree with him when it comes to writing .NET code, (that is turn Option Strict On and leave it On !!), I think some folk have missed the point as to why VB.NET has Option Strict Off.
Now if you had said it was because of VB6 code, well you would be partially correct. The real answer is to make it possible to work with COM as we know it, and most importantly to make it simple and elegant to work with COM objects. Like it or not, history has bequeathed us with a plethora of IUnknown, of COM interfaces that by definition are late bound. Even in Visual Studio .NET, the macro recorder relies on this very thing to bridge the world of COM to the world of .NET. (hint, record a heap of macros then turn Option Strict On and see what happens )
That’s not to say that you can’t work with the COM objects without Option Strict Off --- In fact you can, BUT it requires more code. And I must admit I think I have just about always ended up turning Option Strict On, even in my VS.NET macros…. but sometimes I think that’s just the pedantic in me
The real problems lay not in the ability to use Option Strict Off/On, but in its lack of visibility (sometimes) and it’s lack of granularity.
To address the visibility, make the declaration explicit in each and every one of your code files. Oh and send emails to the VB team asking them to make it visible if set at project level either in a toolbar button or even status bar etc.
To address the granularity, well at present try to factor your code out to a separate code file that has Option Strict Off in it. Usually though it’s not worth the pain. IN VB.NET 2005 you can get a similar effect a bit nicer by using partial classes, thus splitting your class file into separate code files, hence allowing you to compile some of the class’s code with Option Strict On, and some with it Off.
Oh, and long term, well keep sending the VB team those emails. I would suggest making suggestions such as :
#Begin Option Strict Off
#End Option Strict Off
Or some block construct like that so as we can use Option Strict Off like it was intended to be used without forcing a whole heap of our code to also be Option Strict Off.