RDC11027: String compared with a number as a Double
Severity: Warning · Family: RDC (Rubberduck Core analyzers) ·
Raised by: StringComparedWithANumberAsADoubleAnalyzer
A comparison operator - =, <>, <, >, <=, >= - compares a String with a Byte, an Integer or a Long. Or it compares a Variant
that holds a String with a Single or a LongLong that is not a Variant.
VBA compares the two as Double values: it reads the String as a Double, and the number too. So, with n declared As Long and holding 2:
"2.5" = nisFalse, and"2.5" > nisTrue."40000" > iisTrue, withianInteger: no overflow.v = sisFalse, withvaVariantholding"0.1"andsaSingleof0.1: theSingleis not theDouble0.1.
This is what Microsoft Office VBA does. The language specification says otherwise:
MS-VBAL §5.6.9.5 compares the two in the number's type. It converts "2.5" to the Long 2,
which makes "2.5" = n True, and "40000" to an Integer, which overflows (run-time error 6). RD-VBA runs the code as VBA does, and this warning
says that the result can differ from the one the specification gives.
It is not raised where the two answers are the same, whatever the number:
- a
Stringthe code gives a known value that is a whole number of the number's type ("2" = n), or that is no number at all ("abc" = nis a type mismatch either way); - an operator that answers alike for every number. The specification converts
"2.5"to2, and"2.5" < nis2 < nfor every whole numbern, as"2.5" >= nis2 >= n; but"2.5" > nis not2 > nwhennis2."2.6"is3, so there>and<=answer alike; - two known values with the same answer:
"2.5" > 7isFalseeither way.
A variable declared As String, or a parameter, has no value before the code runs, so a comparison of one with a number is always reported. A Case
clause compares as the operator does: Case 2 and Case 1 To 2 of a selector holding "2.5" are reported.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
| Text that holds a number is compared with a whole number. | If txtQuantity.Text > 10 Then |
Convert the text first, in the type it is meant to be: If CLng(txtQuantity.Text) > 10 Then, or CDbl to keep its decimals. |
| A whole number is meant to be compared as text. | If code = 5 Then, with code a String |
Compare text with text: If code = "5" Then. |
See also
- RD-VBAL §2.6.4 Rubberduck Core Diagnostics
- RD-VBAL §5.6.9.5 Relational Operators
RDC11013: aVariantnumber compared with aStringas textRDC11020: aVariantStringthat is no number, compared with a number
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca