rspec/rules/S6145/rule.adoc

60 lines
1.5 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
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:
2021-01-26 04:07:35 +00:00
* Implicit narrowing conversions
* Late binding
2021-01-27 13:42:22 +01:00
* Implicit typing that results in an ``++Object++`` type
2021-01-26 04:07:35 +00:00
This behavior can lead to unexpected runtime errors due to type mismatch or missing members.
2021-01-27 13:42:22 +01:00
``++Option Strict++`` can be set in project properties or overridden in individual source files.
2021-01-26 04:07:35 +00:00
== 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[Visual Basic documentation - Option Strict Statement]
2021-01-26 04:07:35 +00:00