I'm preparing a class on Visual Basic 2005 targeting Visual Basic 6 programmers migrating to the .NET platform.
I would like a word of advice about whether to recommend them to always enable Option Strict or not.
I've worked exclusively with C-style programming languages, mostly Java and C#, so for me explicit casting is something I always expect I have to do, since it's never been an option.
However I recognize the value of working with a language that has built-in support for late-binding, because not having to be excessively explicit about types in the code indeed saves time. This is further proved by the popular diffusion of dynamic typed languages, even on the .NET platform with the Dynamic Language Runtime.
With this in mind, should someone who is approaching .NET for the first time using VB.NET and with a VB6 background be encouraged to get into the mindset of having to work with compile-time type checking because that's the "best practice" in the CLR? Or is it "OK" to continue enjoying the benefits of late-binding?
Description. Option Strict prevents VB from making any implicit data type conversions that are narrowing since narrowing conversions may involve data loss.
Introduces a statement that specifies a compiler option that applies to the entire source file.
When you set Option Infer to On , you can declare local variables without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression. In the following illustration, Option Infer is turned on.
Yes! Option Strict is definitely a best practice with .Net. Emphasize that .Net is at it's core a strongly typed platform, and will be until the DLR is more completely supported. With few exceptions, every Dim
and Function
should have an explicit type declared to go with it. Things like LINQ or Boo and JScript are the exceptions that prove the rule.
Here are some other things to point out. I'm sure you're well aware of all this, but I've had to work with and maintain a lot of VB.Net code written by former VB6ers, and so this is something of a sore spot for me:
LEN()
, REPLACE()
, TRIM()
, etcoMyObject
and sMyString
are not kosher. Show them the reference in Microsoft's Design Guidelines if they don't believe you.AndAlso
/OrElse
logical operatorsCreateObject()
again.IEnumeralbe(Of T)
), and learn why they should never use an ArrayList
again.I could keep going, but I'll just point you to the Hidden Features of VB.Net Question to close out this rant.
Time spent developing with Option Strict enable will save you tremendous amount of debugging time later on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With