Francesco Tattoli
Francesco Tattoli

Reputation: 82

Evaluation of len(string) that should be False is evaluated as True

I read a file and split fields into variables and if I come into a variable which max length should be 32 I do this:

ragionesociale = Trim(Mid$(lineal, 159, 61))    
TRF_RASO = ragionesociale
If len(TRF_RASO < 32) Then
    TRF_RASO = TRF_RASO & Space(32 - Len(TRF_RASO))
Else
    TRF_RASO = Left$(TRF_RASO, 32)
End If

When it comes to evaluate the first condition len(TRF_RASO < 32) it evaluates as True, while if in the immediate window I put ? len(ragionesociale)

It gives me 38, so only the Else should be evaluated. Why does vb evaluate the first as true? I hope to be clear when explaining

Upvotes: 0

Views: 90

Answers (1)

Alex K.
Alex K.

Reputation: 176016

You have:

len(TRF_RASO < 32)

(TRF_RASO < 32) is a False expression which is 5 characters long when coerced to a string & passed to Len(), hence the incorrect result.

Correct to:

if len(TRF_RASO) < 32

Upvotes: 2

Related Questions