Re: Numbers higher but the computer dosn't think so
- From: "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Thu, 25 Aug 2005 09:20:09 -0400
> I have created a program that works accept for this part:
>
> If L <= lblh.Caption Then
>
> cmdNext.Enabled = False
>
> Else
>
> cmdNext.Enabled = True
>
> End If
>
> 'So you know at the time of use (when the program runs) the following
> have the following values:
>
> 'L = 3
>
> 'lblh.caption = 20
>
> 'cmdnext.enabled = true
>
> Please can someone tell me why either my maths skills are useless or
> the computers maths skill are useless!
You haven't shown us how L is declared (String, Integer, Single, etc.).
If it's a String, then the answer obvious... in comparing two String
values, the character "2" comes before the character "3" in the order of
ASCII characters and String comparisons are done character by character.
The "0" in "20" is never examined since if "2" is less than "3", the
whole String starting with "2" is considered less than the whole String
starting with "3" no matter how many characters are in each String
value. On the other hand, if L is declared as one of the numerical data
types, the answer to what is going on is exactly the same as the above,
but the reason for why it applies is not as obvious. When you compare a
number against a string (as you are doing), you have left it up to VB to
decide on how to compare these dissimilar things. VB chooses to convert
numerical values to String values and compare that converted
number-to-String against the given String value; so the reason for your
result is the same as if you gave two String values to compare
originally. By the way, converting the number to a String value and
comparing as Strings is the obvious way to go (in the absences of any
other conditions) because instead of comparing "3" to "20", you might be
comparing "3" to "XYZ" and VB could never convert "XYZ" to a numerical
value in order to compare it against the given number.
Now, for how you handle this problem, force your numerical String values
to be numbers during the comparison. You can use any of the CXXX (that
is, CInt, CLng, CSng, etc.) if you know the data type of the number or
the Val function otherwise. Use
If Val(L) <= Val(lblh.Caption) Then
if L is a String variable (the Caption is always a String value);
otherwise you use
If L <= Val(lblh.Caption) Then
if L is already declared as a numerical data type. You could also use
also use the CDbl function to make the comparison, but if you use this
function, I would think you should use it on both operands no matter
what data type L is (unless you know L is already a Double).
If CDbl(L) <= CDbl(lblh.Caption) Then
If your program is going to be used on computers with non-US regional
settings (that is, the decimal point will not be a "dot" character),
then you will have to use the CDbl function as Val is not
internationally aware.
Rick
.
- References:
- Numbers higher but the computer dosn't think so
- From: James
- Numbers higher but the computer dosn't think so
- Prev by Date: Numbers higher but the computer dosn't think so
- Next by Date: Re: Simple way to find + ?
- Previous by thread: Numbers higher but the computer dosn't think so
- Next by thread: Modularize In .NET
- Index(es):
Relevant Pages
|