Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
2  
Congratulations for recognizing the trap. Most users of "dynamic" typing still have that before them. –  ThomasX Apr 11 '12 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."

share|improve this answer
    
Actually Option Explicit has been in since the first release of VB.NET (2003) –  Matt Wilko Jun 24 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.

share|improve this answer
4  
Actually, Dim SB as New StringBuilder will work just fine - the compiler can easily infer the proper type and will do so. –  tdammers Apr 10 '12 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. –  System Down Apr 10 '12 at 20:35
1  
@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. –  Kate Gregory Apr 10 '12 at 21:14
    
@Kate Gregory - Are you sure? Because I tried my code and it doesnt work. It compiles, but if you add a line to try to use x you get a late binding error. –  System Down Apr 10 '12 at 21:31
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 Apr 11 '12 at 13:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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