Home > Software engineering >  A little doubt about VB Trim function
A little doubt about VB Trim function

Time:11-17

Use the Trim function in VB, there is a little small doubt

The following code exception happens:
If "" & lt;> 12 then
XXXXX
End the if

But after using the trim function, there will be no exception, can normal logic into the XXXXX
If the trim (" ") & lt;> 12 then
XXXXX
End the if

What is the principle? After the Trim can be compared to digital?

The same code, running exception occurs in VB.NET environment

Consult everybody elder

CodePudding user response:

"" should be of type String;
Trim (" ") should be the Variant/type String;

"" & lt;> 12 is a string and number, so type mismatch;
Trim (" ") & lt;> 12 Trim (" ") of the return value is the Variant types of variables, I think it will in the process of comparing is converted to a digital, since is equivalent to:
Val (Trim (" ")) & lt;> 12 or Val (" ") & lt;> 12 and the Val (" ")=0, so the comparison operations can be executed correctly;

But in the VBS script, Trim (" ") & lt;> 12 still complains: type does not match;
This suggests that the VB compiler and the VBS compiler approach is not the same;
Different types should be avoided, such as values or variable comparison and assignment operation;

CodePudding user response:

reference 1st floor milaoshu1020 response:
"" should be of type String;
Trim (" ") should be the Variant/type String;

"" & lt;> 12 is a string and number, so type mismatch;
Trim (" ") & lt;> 12 Trim (" ") of the return value is the Variant types of variables, I think it will in the process of comparing is converted to a digital, since is equivalent to:
Val (Trim (" ")) & lt;> 12 or Val (" ") & lt;> 12 and the Val (" ")=0, so the comparison operations can be executed correctly;

But in the VBS script, Trim (" ") & lt;> 12 still complains: type does not match;
This suggests that the VB compiler and the VBS compiler approach is not the same;
Different types should be avoided, such as values or variable comparison and assignment operation;

Thank you for your reply,,,,,,,
  • Related