
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
61 lines
2.5 KiB
Plaintext
61 lines
2.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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
|
|
|
|
[source,vbnet]
|
|
----
|
|
Public Class Foo
|
|
Public Sub Bar(value as Object)
|
|
Dim stringValue As String = CStr(value)
|
|
End Sub
|
|
End Class
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[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)
|
|
|
|
=== on 1 Feb 2017, 17:28:05 Ann Campbell wrote:
|
|
\[~valeri.hristov] it's not clear to me what the forbidden syntax is. Please at a minimum add 'Noncompliant' comments to the Noncompliant Code Example. Also, from the description
|
|
|
|
____
|
|
the naming is misleading
|
|
|
|
____
|
|
|
|
{empty}...the naming of what? The conversion operators? Also, I don't understand your distinction between the old "conversion" and the new "casting". Can you expand on that in the description, pls?
|
|
|
|
=== on 1 Feb 2017, 17:49:50 Valeri Hristov wrote:
|
|
Sure, I will. Thanks for the guidelines!
|
|
|
|
=== on 1 Feb 2017, 18:12:02 Valeri Hristov wrote:
|
|
\[~ann.campbell.2] This and 3866 are contributed by a community member and I just copy-pasted his descriptions. I am no expert in VB.NET and what he is saying seems reasonable (e.g. the functions are provided just for compatibility, hence we should prefer other methods to cast values to other types), but while reading the MSDN documentation, I began to think that we might not want to have this rule: \https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx, see the Remarks section, where they say just the opposite of his description...
|
|
|
|
=== on 1 Feb 2017, 18:17:05 Ann Campbell wrote:
|
|
Agreed [~valeri.hristov]. I'd say we either flip this rule to '"CType" should _not_ be used' or we close it "Won't Fix". WDYT?
|
|
|
|
|
|
BTW, it's probably worth going back to your community member for a response to the Remarks section.
|
|
|
|
=== on 6 Sep 2017, 11:35:10 Jean-Christophe Collet wrote:
|
|
After consideration and some research, it seems very controversial and with little value. So closing this rules as "Won't Fix"
|
|
|
|
endif::env-github,rspecator-view[]
|