option strict

  • Thread starter Thread starter crain
  • Start date Start date
It forces you to write type strict code and avoid late binding.

For example, without option explicit you could write code like:

Dim a As Integer = "1234"

With option explicit on, you can't do that, you would have to write:

Dim a As Integer = CInt("1234")

Writing code such as in the first example, is more likely to crash during
runtime due to type inconsistencies, since these inconsistencies are not
validated during compile time. Using option explicit typically amounts to
better and more reliable code.
 
Hi Marina,
It forces you to write type strict code and avoid late binding.
For example, without option explicit you could write code like:
Dim a As Integer = "1234"
With option explicit on, you can't do that, you would have to write:
Dim a As Integer = CInt("1234")

Is Dim a As Integer = 1234 also valid?

I was writting this message below to you when I saw this above it is just
for.

I hate this kind of errors, it seems that this will be (suggested)
autocorrected in VB.2005, that I find a real advantage in my eyes, more than
operator overloading and things like that.

Cor
 
what is the optiion strict used for anybody give me an example?

Option Strict is a "strict" enforcement of the type system, you should
*always* have option strict on. It stops you from making stupid mistakes and
keeps your code from getting too ambigous (by stoping implicit convertions).

Read about it, there is a good description in the .NET Docs (with examples):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmOptionStrict.asp

----------------------------------------
' Force explicit variable declaration.
Option Strict On

' Declare variables.
Dim MyVar As Integer
Dim Obj As Object

' Declared variable does not generate error.
MyVar = 1000

'Attempting to convert to an Integer generates an error.
MyVar = 1234567890.987654321

' Late-bound call generates an error
'Call Obj.Method1()
 
Of course setting an integer to an actual integer is valid. It's always
valid no matter what option strict is set to.

This was a very simple example, but very often you are dealing with various
variables and calling methods - so it is not as trivial to see that you
should assign an actual integer. Option strict catches cases where you need
to do explicit type conversion, and makes you think about what code you are
writing. I think this is especially invaluable for VB6 programmers, who
were used to not caring about types, creating Variant variables, which let
them use a variable in one part of their code as an integer, another part as
a string , and another part as an array, without any type conversions.
 
hey thats great .
thanx for the explainiation.
its unbelievable that vb can allows this

dim asdf as integer ="12"

it is so wierd.
 
Back
Top