rspec/rules/S3867/vbnet/rule.adoc

35 lines
979 B
Plaintext
Raw Normal View History

For the sake of backward compatibility, Visual Basic .NET continues to offer a set of functions that convert from Object to different primitive types: ``++CChar, CStr, CBool, CDate, CSng, CDbl, CDec, CByte, CSByte, CShort, CUShort, CInt, CUInt, CLng, CULng++``. However, using these functions is misleading, because it suggests a cast. It is better to cast explicitly using ``++CType()++``, or use ``++Convert.To()++`` when the value should be converted.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,vbnet]
----
Public Class Foo
Public Sub Bar(value as Object)
Dim stringValue As String = CStr(value)
End Sub
End Class
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,vbnet]
----
Public Class Foo
Public Sub Bar(value as Object)
Dim stringValue As String = CType(value, String)
End Sub
End Class
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]