Table of Contents

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.4 does not match: 5.4 becomes the Integer 5, and 5 < 5 is False. The expression i < 5.4 is True.
  • With b declared As Boolean and holding False, Case Is < 1 does not match: 1 becomes True, and False < True is False.
  • With s declared As String and holding "10", Case Is < 5 matches: 5 becomes "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


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca