Re: Numbers higher but the computer dosn't think so



> 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


.



Relevant Pages

  • Re: searching for the highest index within a directory
    ... (I used to write code in the Ada programming language... ... Because "testFile_34" is a string, ... there is no way to compare them as numbers. ... means we look at the first character in each string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delimiting problems
    ... > So how can I delimit the " character? ... Now when I have a piece of string such as ... If you want to compare against several values, ...
    (comp.lang.c)
  • Re: Quandry with the following C code (Intermediate)
    ... It doesn't do a compare the usual way. ... a string with Unicode characters over 256 in it. ... This tallies the number of occurrences of each separate character value ... of the same character at all in the first string, ...
    (comp.lang.c)
  • Re: 32-bit programs on Windows x64
    ... tiny individual sequences. ... to compare its input string against every possible ... compare one string against one string, ...
    (microsoft.public.vc.mfc)
  • Re: 32-bit programs on Windows x64
    ... THE AMOUNT OF RAM ON THE MACHINE IS COMPLETELY, ... nothing in that article that implies any concurrency. ... to compare its input string against every possible valid ...
    (microsoft.public.vc.mfc)