Val returns 0 when it's not getting numbers

Everything else that doesn't fall into one of the other PB categories.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Val returns 0 when it's not getting numbers

Post by fsw »

The problem is when evaluating a char (as string) Val returns 0:

Code: Select all

Val("0") ; returns 0
Val("1") ; returns 1
Val("2") ; returns 2
Val("3") ; returns 3
etc.
Val("a") ; returns 0
Wouldn't a -1 return value be better?
(since Val("-1") doesn't work anyway...)

Thanks for reading
fsw
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

nope, because the Value of the string is Zero, not minus one.
it has always been this way since the Function that returns a numerical Value of an alphanumerical string was invented.

try:

Code: Select all

Debug Val("-1")
... dunno what you have messed up.

note:

Code: Select all

Debug Val("$C85F")

Code: Select all

Debug Val("%101010101")
oh... and have a nice day.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Val returns 0 when it's not getting numbers

Post by UserOfPure »

Val, in all Basics, has always returned 0 if it starts with a non-digit. Many apps rely on this expected behavior, and to change it now would break a lot of code for no good reason.
fws wrote:(since Val("-1") doesn't work anyway...)
Huh? Returns -1 here, as expected. Works just fine.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Val returns 0 when it's not getting numbers

Post by fsw »

Okay then return a #PB_ERROR constant value instead of zero.

Only because Basic's do something in a certain way doesn't mean it's the best way of doing things.

---

Now repeat after me: "Purebasic is NOT Basic" it's different - and that's why most users love it :mrgreen:
Fred
Administrator
Administrator
Posts: 18556
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Val returns 0 when it's not getting numbers

Post by Fred »

The problem is you can't return a special value, as all values in the range are already possible. You would need another function (CheckNumber() or something) to do it properly.
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Val returns 0 when it's not getting numbers

Post by DarkDragon »

fsw wrote:Okay then return a #PB_ERROR constant value instead of zero.

Only because Basic's do something in a certain way doesn't mean it's the best way of doing things.
But thats definately bad what you suggest. How would you differ from the value of the constant and the right value? Why don't you check yourself if the number is okay?

Lets say #PB_ERROR = -1234. How could you differ between Val("-1234") and Val("blubb")?
bye,
Daniel
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Val returns 0 when it's not getting numbers

Post by fsw »

Will create my own function, thanks.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Val returns 0 when it's not getting numbers

Post by UserOfPure »

.
Post Reply