G
Guest
All,
I think I may have uncovered a bug in the IsNumeric function, or at least a
misunderstanding on functionality.
I am writing a Schedule Of Classes Application for our campus and have a
section that lists the Times, Buildings and rooms for courses.
In this section I have a function called PadCell that takes3 parameters, one
the value, one a padd count and one a a boolean input called StripNumeric,
that if true, checks if the value is numeric so I can strip off leading 0's
on a value to make it more readable.
Private Function PaddCell(ByVal sValue As String, ByVal PaddCount As
String, Optional ByVal StripNumeric As Boolean = False) As String
Dim tString As String = sValue
If StripNumeric And IsNumeric(tString) Then
tString = CStr(CInt(tString))
While Left(tString, 1) = "0"
tString = Mid(tString, 2)
End While
End If
Dim i As Integer = Len(tString)
Dim j As Integer
For j = i + 1 To PaddCount
tString += " "
Next
Return tString
End Function
This function works fine when StripNumeric is False, and IfNumeric evaluates
properly, which is where I am having the problem.
The problem I have found is that if I have a room that starts with #E##,
where # is a number, such as 0E202, 1E100, or 0E500, IsNumeric returns this
value as true!. I have tested this in .NET 2.0. I am not sure if it occurs
in 1.1. If I pass in a literal "string" value, I would expect it to treat
it as a string, ot as another object type., To work around the problem, I had
to write a function, which I call IsNumeric2, which does a for loop on each
character in the string and checks if it IsNumeric and performs a boolean and
on a return value. If false found it bails out early as not to take up much
processing:
The function looks like:
Private Function IsNumeric2(ByVal sString) As Boolean
If Len(sString) = 0 Then Return False
Dim bNumber As Boolean = True
Dim iInt As Integer
For iInt = 1 To Len(sString)
bNumber = bNumber And IsNumeric(Mid(sString, iInt, 1))
If bNumber = False Then Exit For
Next
Return bNumber
End Function
Was this a known bug somewhere that I missed, or is this a special case I
may not be aware of. Note in the PaddCell function, the tString is being
passed as a string value, not numeric so IsNumeric should treat it as a
string I would think and not something in Scientific Notation, which is the
only use of E I could think of and I think that is what it is doing since
the function CInt("1E1") returns 10 instead of an InvalidCastException. If
this is the case, is there a way I can FORCE it to evaluate as string
directly?
Thanks,
Nathan
I think I may have uncovered a bug in the IsNumeric function, or at least a
misunderstanding on functionality.
I am writing a Schedule Of Classes Application for our campus and have a
section that lists the Times, Buildings and rooms for courses.
In this section I have a function called PadCell that takes3 parameters, one
the value, one a padd count and one a a boolean input called StripNumeric,
that if true, checks if the value is numeric so I can strip off leading 0's
on a value to make it more readable.
Private Function PaddCell(ByVal sValue As String, ByVal PaddCount As
String, Optional ByVal StripNumeric As Boolean = False) As String
Dim tString As String = sValue
If StripNumeric And IsNumeric(tString) Then
tString = CStr(CInt(tString))
While Left(tString, 1) = "0"
tString = Mid(tString, 2)
End While
End If
Dim i As Integer = Len(tString)
Dim j As Integer
For j = i + 1 To PaddCount
tString += " "
Next
Return tString
End Function
This function works fine when StripNumeric is False, and IfNumeric evaluates
properly, which is where I am having the problem.
The problem I have found is that if I have a room that starts with #E##,
where # is a number, such as 0E202, 1E100, or 0E500, IsNumeric returns this
value as true!. I have tested this in .NET 2.0. I am not sure if it occurs
in 1.1. If I pass in a literal "string" value, I would expect it to treat
it as a string, ot as another object type., To work around the problem, I had
to write a function, which I call IsNumeric2, which does a for loop on each
character in the string and checks if it IsNumeric and performs a boolean and
on a return value. If false found it bails out early as not to take up much
processing:
The function looks like:
Private Function IsNumeric2(ByVal sString) As Boolean
If Len(sString) = 0 Then Return False
Dim bNumber As Boolean = True
Dim iInt As Integer
For iInt = 1 To Len(sString)
bNumber = bNumber And IsNumeric(Mid(sString, iInt, 1))
If bNumber = False Then Exit For
Next
Return bNumber
End Function
Was this a known bug somewhere that I missed, or is this a special case I
may not be aware of. Note in the PaddCell function, the tString is being
passed as a string value, not numeric so IsNumeric should treat it as a
string I would think and not something in Scientific Notation, which is the
only use of E I could think of and I think that is what it is doing since
the function CInt("1E1") returns 10 instead of an InvalidCastException. If
this is the case, is there a way I can FORCE it to evaluate as string
directly?
Thanks,
Nathan