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?

  • 2
    Congratulations for recognizing the trap. Most users of "dynamic" typing still have that before them.
    – ThomasX
    Commented Apr 11, 2012 at 14:29

2 Answers 2


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."

  • Actually Option Explicit has been in since the first release of VB.NET (2003)
    – Matt Wilko
    Commented Jun 24, 2014 at 9:39

If I remember correctly, Strict On will make VB.net requires an type for every operation, so code like

DIM SB as New StringBuilder 

will not work as it disables late binding completely.

It makes life unnecessarily bureaucratic. If you want an strong typed language on VS.NET, use C# (VB.NET is really an unwanted renter on the building - it remained on VS because of its popularity). Strong-typed without losing late binding.

  • 6
    Actually, Dim SB as New StringBuilder will work just fine - the compiler can easily infer the proper type and will do so.
    – tdammers
    Commented Apr 10, 2012 at 20:02
  • True. I think what he meant was something like: <code> Dim x As Object x = New StringBuilder() </code> Which doesn't work. Commented Apr 10, 2012 at 20:35
  • 2
    @SystemDown that works too. What doesn't work is Dim x or Private x followed many lines later by x = New StringBuilder. Or Dim x As Integer followed by putting a date or a string in it later. Commented Apr 10, 2012 at 21:14
  • 2
    Dim SB As New StringBuilder doesn't involve any inference. In VB .NET, it's just a shorter way of saying Dim SB As StringBuilder = New StringBuilder. Dim SB = New StringBuilder does involve type inference, and requires Option Infer to be turned on.
    – Kyralessa
    Commented Apr 11, 2012 at 13:59
  • 2
    -1 Incorrect. You are confusing it with Option Infer you example is nothing to do with late binding
    – Matt Wilko
    Commented Jun 24, 2014 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.