why doesn't this work

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

Dim reqCustomerId As String = "uv$?????C??`?????C??@-?"
If Val(reqCustomerId.Substring(0, 10) < 1111111111) Then


the if gives me the error:
Conversion from string "uv$?????C?" to type 'Double' is not valid.

"uv$?????C?" should convert to 0
 
cj said:
Dim reqCustomerId As String = "uv$?????C??`?????C??@-?"
If Val(reqCustomerId.Substring(0, 10) < 1111111111) Then


the if gives me the error:
Conversion from string "uv$?????C?" to type 'Double' is not valid.

"uv$?????C?" should convert to 0

1. Enable Option Strict
2. Enable Option Strict
3. Enable Option Strict
4. The Val function is outdated (ya, I know it still exists)
5. You probably wanted to write:

If Val(reqCustomerId.Substring(0, 10)) < 1111111111 Then

Mind the different location of the closing ")".

6. If you use CDbl or Double.Parse, you get an exception because the string
is not a valid numeric string.


Armin
 
And why is it that you think it should convert to zero?

Tom Dacon
Dacon Software Consulting
 
Val() interprets the contents of a string up to (but not including) the
first character that cannot be interpreted as being part of a number.

Val("uv$?????C?") will return 0 (zero) because the first character cannot be
interpreted as being part of a number.

Val("1uv$?????C?") will return 1

Val("11uv$?????C?") will return 11

etc. ...
 
Armin said:
4. The Val function is outdated (ya, I know it still exists)

While I agree with all your other points, what would you suggest we use
instead of the "outdated" Val() that behaves in exactly the same way?

To the best of my knowledge, there is nothing else..? I personally think
Val() is still a valid and worthwhile function, much like many of the other
VB6-style functions. They're all tools to solve problems, there's no point
stopping using them if they do the job just because they're a bit old, IMO.
 
(O)enone said:
While I agree with all your other points, what would you suggest we use
instead of the "outdated" Val() that behaves in exactly the same way?

To the best of my knowledge, there is nothing else..? I personally think
Val() is still a valid and worthwhile function, much like many of the
other VB6-style functions. They're all tools to solve problems, there's no
point stopping using them if they do the job just because they're a bit
old, IMO.


That's why I wrote "ya, I know it still exists" - so use it if you want. I
myself don't need it because I haven't come across any situation where I
exactly need what Val does. ....... what does it what I can't do the
straight way (CInt, Integer.Parse...)?



Armin
 
Armin got it right.

Simple misplaced ) Darn strange thing, program ran for over a year with
that misplaced ) but we finally got that garbage customer id today.

If Val(reqCustomerId.Substring(0, 10) < 1111111111) Then
If Val(reqCustomerId.Substring(0, 10)) < 1111111111 Then

As for his other opinions, he is free to have them.
 
Armin Zingler said:
That's why I wrote "ya, I know it still exists" - so use it if you want. I
myself don't need it because I haven't come across any situation where I
exactly need what Val does. ....... what does it what I can't do the
straight way (CInt, Integer.Parse...)?

The thing I like it for is that it stops parsing when it reaches a
non-numeric character, without erroring. I've found this useful on numerous
occasions when dealing with user input when you don't want to throw away
whatever they have entered if there are trailing characters after the
supposedly numeric text they have provided.

There are other irritations with it though (like always using a period "."
as a decimal separator, regardless of regional settings) so I don't use it
directly either, but I do have a wrapper function that uses it internally.
 
Back
Top