Turn VB.Net Option Strict On

vb.net

I recently found out about strong typing in VB.Net (naturally it was on here, thanks!) and am deciding I should take another step toward being a better programmer. I went from vba macros -> VB.Net, because I needed a program that I could automate and I never read anything about strong typing, so I kind of fell into the VB.Net default trap. Now I am looking to turn it on and sort out this whole type thing.

I was hoping someone could direct me towards some resources to make this transistion as painless as possible. I have read around some and ctype seems to come up a lot, but past that I am at a bit of a loss. What are the benefits of switching? Is there more to it than just using ctype to cast things? I feel like there is a good article that I have failed to come across and any direction would be great.

Would a good approach to be to rewrite a program that is written with option strict off and note differences?

Best Answer

In the title of your question, you ask:

I want to turn VB.Net Option Strict On

In VB.NET there are 2 compiler options: Option Strict and Option Explicit.

1- Option Strict

By default, the Visual Basic .NET or Visual Basic compiler does not enforce strict data typing. To change this default behavior, see the Change the Default Project Values section.

2- Option Explicit (starting from V 2005)

By default, the Visual Basic .NET or Visual Basic compiler enforces explicit variable declaration, which requires that you declare every variable before you use it.

You can read more about this in here: MSDN-KB Support-Article

When the compiler issue an error or a warning it is for your program's correctness sake. In OO the concept of type goes far beyond primitive data types and is the heart of many concepts in OO.

The following paragraph from History of worst software bugs may be worth noting "June 4, 1996 -- Ariane 5 Flight 501. Working code for the Ariane 4 rocket is reused in the Ariane 5, but the Ariane 5's faster engines trigger a bug in an arithmetic routine inside the rocket's flight computer. The error is in the code that converts a 64-bit floating-point number to a 16-bit signed integer. The faster engines cause the 64-bit numbers to be larger in the Ariane 5 than in the Ariane 4, triggering an overflow condition that results in the flight computer crashing."

Related Topic