Table of Contents

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" = n is False, and "2.5" > n is True.
  • "40000" > i is True, with i an Integer: no overflow.
  • v = s is False, with v a Variant holding "0.1" and s a Single of 0.1: the Single is not the Double 0.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 String the 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" = n is a type mismatch either way);
  • an operator that answers alike for every number. The specification converts "2.5" to 2, and "2.5" < n is 2 < n for every whole number n, as "2.5" >= n is 2 >= n; but "2.5" > n is not 2 > n when n is 2. "2.6" is 3, so there > and <= answer alike;
  • two known values with the same answer: "2.5" > 7 is False either 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


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