Results 1 to 9 of 9

Thread: [RESOLVED] Val(True) = 0. Why?

  1. #1

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Resolved [RESOLVED] Val(True) = 0. Why?

    We all know that in vb6, boolean values are True = -1 and false = 0.

    But if you wrap a boolean value in a Val function, it always returns 0, whatever it's original value.

    Val(-1) = -1.

    Val(True) = 0.

    That can't be right.

    (Okay, you might ask why I'm wrapping a boolean in a Val function; it's a long story which I won't go into now as it's besides the point).
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  2. #2
    Addicted Member Wolfgang Enzinger's Avatar
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    160

    Re: Val(True) = 0. Why?

    Quote Originally Posted by simonm View Post
    We all know that in vb6, boolean values are True = -1 and false = 0.

    But if you wrap a boolean value in a Val function, it always returns 0, whatever it's original value.

    Val(-1) = -1.

    Val(True) = 0.

    That can't be right.
    The Val function accepts a *string* as input. So the following calls are equivalent:

    Code:
    ?Val(True)
    ?Val(CStr(True))
    ?Val("True")
    Since there aren't any digits in the string "True", zero is returned and that's correct.

    HTH,
    Wolfgang

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Val(True) = 0. Why?

    easy

    val takes a string, so the boolean is first cast to string and then passed to val. val("true") is zero

  4. #4
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: Val(True) = 0. Why?

    you can use -cInt(True)

  5. #5

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: Val(True) = 0. Why?

    Okay, that explains it.

    Thanks.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Val(True) = 0. Why?

    The Val() function should be avoided anyway. It is obsolete and has side-effects that can be harmful unless you intentionally want these side effects.

    It is almost always better to use one of the "grown up" conversion functions such as CLng(), CInt(), etc. that were meant to replace crusty old and slow Val(), which is really only in VB6 at all to make it easier to port old MS Basic code to VB6.

  7. #7

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: [RESOLVED] Val(True) = 0. Why?

    The advantage of Val() though is that you can chuck anything at it (nearly) and get a number at the end without errors, even NULLs. In my case, I had a variant. A limitation with VB6 is you have no TryCast() functions (although there is an IsNumeric() function).
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,747

    Re: [RESOLVED] Val(True) = 0. Why?

    Quote Originally Posted by dilettante View Post
    The Val() function should be avoided anyway. It is obsolete and has side-effects that can be harmful unless you intentionally want these side effects.

    It is almost always better to use one of the "grown up" conversion functions such as CLng(), CInt(), etc. that were meant to replace crusty old and slow Val(), which is really only in VB6 at all to make it easier to port old MS Basic code to VB6.
    The other numeric conversion methods are all locale aware.
    I need to deal with a lot of text based data files in which numeric values are stored in the "US" format, that is with a decimal point.
    So I always use Val() function to parse these values.
    I wrote my own CStrVal() funtion which is also locale aware to write numeric values in these data files.

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Val(True) = 0. Why?

    Quote Originally Posted by Arnoutdv View Post
    The other numeric conversion methods are all locale aware.
    Yes, and this is the side effect of Val() and Str$() that can be useful at times. However you don't reach for these functions first, they're a last resort if you want locale-blind conversions.

    People should not get into the bad habit of defaulting to these functions for general use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width