Can you control if a variable type is dynamic or static in VB9?

I would like to use VB9, but I don’t know which syntax to use to say that I want the variable to be statically entered as in C #:

var foo = new Whatever(); 

In previous versions of VB:

 Dim foo = New Whatever() 

created a dynamically typed variable.

Is there a way to get static typing without actually spelling the type in VB9?

+4
source share
1 answer

Yes, you can control this behavior using the Option directives at the beginning of each file or in the project settings:

 Option Strict Off ' The following is dynamically typed: ' Dim x = "Hello" 

 Option Strict On Option Infer On ' This is statically typed: ' Dim x = "Hello" 

It is best to set Option Strict On as the default value for all your projects (this can be done in the options dialog). This guarantees the same input behavior as in C #. Then, if you need dynamic typing, you can disable this option selectively based on each file using the directive above.

+3
source

Source: https://habr.com/ru/post/1277438/


All Articles