BillE said:
I am using vb.net 2005, windows forms.
What expression will cause the Val() function to return NaN?
NaN = Not a Number
Anything that is not an alphanumeric will produce this.
"123" <-- alphanumeric
A123" said:
A user is reporting that this has occurred, but I can't find any test
expression to provide to the Val function which will return NaN. I have
requested the data so I can attempt to reproduce the error, but I don't know
if I will be able to get it.
I am using the Val function to obtain values from an alphanumeric field in
order to sort the rows in a datagrid accordingly.
You must have a non-alphanumeric in a cell.
What strikes me odd is that I only normally see "NaN" in languages
like JavaScript, Jscript or VBScript perhaps. In VB and I just tried
it under VB.NET, you get a ZERO result.
------------ cut here ----------
' file: test_val.vb
Option Strict On
Option Explicit On
Module Module1
function atoi(byval s as string) as integer
return cint(val(s))
end function
sub test(byval s as string)
dim n as integer = atoi(s)
console.writeline("atoi('{0}') ==> {1}",s,n)
end sub
Sub Main()
test("a123")
test("123")
test("+++")
test("789x25")
End Sub
End Module
------------ cut here ----------
result:
atoi('a123') ==> 0
atoi('123') ==> 123
atoi('+++') ==> 0
atoi('789x25') ==> 789
Note, the last one is VALID in the C world, which sort of tells me the
VAL() command is either implemented via the C RTL (Run Time Library)
atoi() function or MS made the VAL behave the same. By design, atoi()
will stop at the first non numeric character. We write a BASIC
compiler for our server product and we also use the Windows RTL atoi()
function in our RTE (Run Time Engine) for the VAL() command.
But again, I never saw or would expect NaN results in VB's VAL
command. That comes from script languages like jscript.
So I think you need to recheck the user's report or check if VBSCRIPT
behaves this way because I would never expect this in VB and I don't
see it in VB.NET as shown above.
--