RDC11014 — Case Is compared in the selector's type
Severity: Warning · Family: RDC (Rubberduck Core analyzers) ·
Raised by: CaseComparedInSelectorTypeAnalyzer
A Case Is clause - Case Is < 5.4, Case Is > 1 - compares a value that is not of the selector's declared type. VBA converts the value to the
selector's type first, then compares the two in that type.
So, with i declared As Integer and holding 5:
Case Is < 5.4does not match:5.4becomes theInteger5, and5 < 5isFalse. The expressioni < 5.4isTrue.- With
bdeclaredAs Booleanand holdingFalse,Case Is < 1does not match:1becomesTrue, andFalse < TrueisFalse. - With
sdeclaredAs Stringand holding"10",Case Is < 5matches:5becomes"5", and"10"comes before"5"as text.
This is what Microsoft Office VBA does. The language specification says otherwise:
MS-VBAL §5.4.2.10 states that the expression select-expression comparison-operator expression
is evaluated, which compares the two in the effective type of their types, as i < 5.4 does. RD-VBA runs the code as VBA does, and this warning stands in for
the comparison the specification states: the code depends on a behaviour the specification does not give it.
A single value (Case 5.4) and a range (Case 1 To 5.4) are converted the same way, but are not reported here: MS-VBAL only says a single value "is compared
with" the selector, without saying how.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
| A number with a fraction is compared with a whole-number selector. | Case Is < 5.4, with the selector an Integer |
Write the value the comparison makes: Case Is < 5, or select on a Double: Select Case CDbl(i). |
A number is compared with a String selector. |
Case Is < 5, with the selector a String |
Compare numbers: Select Case Val(s), or write the text: Case Is < "5". |
A number is compared with a Boolean selector. |
Case Is < 1 |
Compare with True or False. |
See also
- RD-VBAL §2.6.4 Rubberduck Core Diagnostics
- RD-VBAL §5.4.2.10 Select Case Statement
RDC00112: a value converted implicitly into a type that holds less of it
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca