60 lines
1.4 KiB
Plaintext
60 lines
1.4 KiB
Plaintext
![]() |
There are several compilations options available for Visual Basic source code and ``Option Strict`` defines compiler behavior for implicit data type conversions. Specifying ``Option Strict Off`` will allow:
|
||
|
|
||
|
* Implicit narrowing conversions
|
||
|
* Late binding
|
||
|
* Implicit typing that results in an ``Object`` type
|
||
|
|
||
|
This behavior can lead to unexpected runtime errors due to type mismatch or missing members.
|
||
|
|
||
|
``Option Strict`` can be set in project properties or overridden in individual source files.
|
||
|
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
Option Strict Off ' Noncompliant
|
||
|
|
||
|
Public Class KnownType
|
||
|
|
||
|
Public ReadOnly Property Name As String
|
||
|
|
||
|
End Class
|
||
|
|
||
|
Public Module MainMod
|
||
|
|
||
|
Public Function DoSomething(Arg) As String ' Type for "Arg" argument is not defined.
|
||
|
Dim Item As KnownType = Arg ' Implicit narrowing conversion doesn't enforce "Arg" to be of type "KnownType"
|
||
|
Return Arg.Title ' "Title" might not exist in "Arg"
|
||
|
End Function
|
||
|
|
||
|
End Module
|
||
|
----
|
||
|
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
Option Strict On
|
||
|
|
||
|
Public Class KnownType
|
||
|
|
||
|
Public ReadOnly Property Name As String
|
||
|
|
||
|
End Class
|
||
|
|
||
|
Public Module MainMod
|
||
|
|
||
|
Public Function DoSomething(Arg As KnownType) As String
|
||
|
Dim Item As KnownType = Arg
|
||
|
Return Arg.Name
|
||
|
End Function
|
||
|
|
||
|
End Module
|
||
|
----
|
||
|
|
||
|
|
||
|
== See
|
||
|
|
||
|
* https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement
|
||
|
|