What do Option Strict and Option Explicit do?

yretuta picture yretuta · Mar 16, 2010 · Viewed 43.2k times · Source

I saw this post:

Typos… Just use option strict and explicit please.. during one software development project, which I was on as a consultant, they were getting ridiculous amounts of errors everywhere… turned out the developer couldn’t spell and would declare variables with incorrect spelling.. no big deal, until you use the correct spelling when you’re assigning a value to it… and you had option explicit off. Ouch to them…"

What is Option Strict and Option Explicit anyway? I have googled it up but can't get the idea (because mostly it's Visual Basic, I'm doing PHP).

Answer

TLiebe picture TLiebe · Mar 16, 2010

Option Explicit means that all variables must be declared. See here. Without this, you can accidentally declare a new variable just by misspelling another variable name. This is one of those things that cause a lot of grief as you're trying to debug VB programs and figure out why your program isn't working properly. In my opinion, this shouldn't even be an option - it should always be on.

Option Strict "restricts implicit data type conversions to only widening conversions". See here. With this option enabled, you can't accidentally convert one data type to another that is less precise (e.g. from an Integer to a Byte). Again, an option that should be turned on by default.