By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

IsNumeric gives error

P: n/a
eje
IsNumeric(value) should return false if value "can not be
successfully converted to a Double." Instead I get the
following error message: "Input string was not in a
correct format."

I use the following function in a validating class to use
when needed. (Value = H880118A gave the error (like
other 'unconvertible' strings))

Public Function Numeric(ByVal value) As Boolean
If CType(value, String).Length = 0 Then
m_msg = "Value is empty"
Return False
End If
If IsNumeric(value) Then
Return True
Else
m_msg = "Value isn't numeric"
Return False
End If

End Function

What to do?
Eje
Jul 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi Eje,

You can set Option Strict On to solve this problem, I think that that makes
it probably direct clear to you.

Cor

IsNumeric(value) should return false if value "can not be
successfully converted to a Double." Instead I get the
following error message: "Input string was not in a
correct format."

I use the following function in a validating class to use
when needed. (Value = H880118A gave the error (like
other 'unconvertible' strings))

Public Function Numeric(ByVal value) As Boolean
If CType(value, String).Length = 0 Then
m_msg = "Value is empty"
Return False
End If
If IsNumeric(value) Then
Return True
Else
m_msg = "Value isn't numeric"
Return False
End If

End Function

What to do?
Eje

Jul 21 '05 #2

P: n/a
-----Original Message----- Hi Cor
I want to validate a value of unknown datatype. That's
why I can't use Option Strict On

Eje
Hi Eje,

You can set Option Strict On to solve this problem, I think that that makesit probably direct clear to you.

Cor

IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the
following error message: "Input string was not in a
correct format."

I use the following function in a validating class to use when needed. (Value = H880118A gave the error (like
other 'unconvertible' strings))

Public Function Numeric(ByVal value) As Boolean
If CType(value, String).Length = 0 Then
m_msg = "Value is empty"
Return False
End If
If IsNumeric(value) Then
Return True
Else
m_msg = "Value isn't numeric"
Return False
End If

End Function

What to do?
Eje

.

Jul 21 '05 #3

P: n/a
You should still always have option strict on.

You can either make the parameter an Object, or have several different
versions of the method, with each one having a parameter of a different type
(and then potentially have them all forwarded to one method that contains
all the actual logic).

Now, is what being passed here an actual string, or something else? Can you
reproduce this behavior by just calling IsNumeric from a little test program
and passing it in some string that cannot be converted to a numeric?

<an*******@discussions.microsoft.com> wrote in message
news:27*****************************@phx.gbl...
-----Original Message-----

Hi Cor
I want to validate a value of unknown datatype. That's
why I can't use Option Strict On

Eje
Hi Eje,

You can set Option Strict On to solve this problem, I

think that that makes
it probably direct clear to you.

Cor

IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the
following error message: "Input string was not in a
correct format."

I use the following function in a validating class to use when needed. (Value = H880118A gave the error (like
other 'unconvertible' strings))

Public Function Numeric(ByVal value) As Boolean
If CType(value, String).Length = 0 Then
m_msg = "Value is empty"
Return False
End If
If IsNumeric(value) Then
Return True
Else
m_msg = "Value isn't numeric"
Return False
End If

End Function

What to do?
Eje

.

Jul 21 '05 #4

P: n/a
Hi Eje,

That was what I was thinking however would let you give the answer.

As usuall would my answer have been almost the same as from Marina when I
had answered you before her, and therefore I add nothing to her answer.

:-)

Cor
Jul 21 '05 #5

P: n/a
:)

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uM***************@TK2MSFTNGP11.phx.gbl...
Hi Eje,

That was what I was thinking however would let you give the answer.

As usuall would my answer have been almost the same as from Marina when I
had answered you before her, and therefore I add nothing to her answer.

:-)

Cor

Jul 21 '05 #6

P: n/a
Thanks for helping me Marina and Cor.
First to answer your question, Marina. In my actual
application the passed values are strings. I import
textfiles with delimiter separated records and have to
check every value before I pass the values on to the
database.
I made the following tests (See my comments in the
function Test):

Option Strict On
'...

Public Function test() As Boolean
Dim indata As Object
Dim MyCheck As Boolean
indata = "53"
MyCheck = Numeric(indata) 'No problem
indata = "45 abc"
MyCheck = Numeric(indata) 'Still gives error
indata = "abc"
MyCheck = Numeric(indata) 'Still gives error
End Function

Public Function Numeric(ByVal MyVar As Object) As
Boolean
Dim MyCheck As Boolean
MyCheck = IsNumeric(MyVar)
Return MyCheck
End Function

Compare this example from the Help:

Dim MyVar As Object
Dim MyCheck As Boolean
' ...
MyVar = "53" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "459.95" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "45 Help" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns False.

-----Original Message-----
You should still always have option strict on.

You can either make the parameter an Object, or have several differentversions of the method, with each one having a parameter of a different type(and then potentially have them all forwarded to one method that containsall the actual logic).

Now, is what being passed here an actual string, or something else? Can youreproduce this behavior by just calling IsNumeric from a little test programand passing it in some string that cannot be converted to a numeric?
<an*******@discussions.microsoft.com> wrote in message
news:27*****************************@phx.gbl...
>-----Original Message-----

Hi Cor
I want to validate a value of unknown datatype. That's
why I can't use Option Strict On

Eje
>Hi Eje,
>
>You can set Option Strict On to solve this problem, I

think that that makes
>it probably direct clear to you.
>
>Cor
>
>
>> IsNumeric(value) should return false if value "can not
be
>> successfully converted to a Double." Instead I get
the >> following error message: "Input string was not in a
>> correct format."
>>
>> I use the following function in a validating class

to use
>> when needed. (Value = H880118A gave the error (like
>> other 'unconvertible' strings))
>>
>> Public Function Numeric(ByVal value) As Boolean
>> If CType(value, String).Length = 0 Then
>> m_msg = "Value is empty"
>> Return False
>> End If
>> If IsNumeric(value) Then
>> Return True
>> Else
>> m_msg = "Value isn't numeric"
>> Return False
>> End If
>>
>> End Function
>>
>> What to do?
>> Eje
>
>
>.
>

.

Jul 21 '05 #7

P: n/a
Hi Anon,

Why not as this with option Strict On
See inline bellow

I hope this helps?

Cor

Option Strict On
'...

Public Function test() As Boolean
Dim indata As Object
Dim MyCheck As Boolean
indata = "53"
MyCheck = Numeric(indata.ToString) 'No problem
indata = "45 abc"
MyCheck = Numeric(indata.ToString) 'Still gives error
indata = "abc"
MyCheck = Numeric(indata.TosString) 'Still gives error
End Function Public Function Numeric(ByVal MyVar As String) As
Boolean
Dim MyCheck As Boolean
MyCheck = IsNumeric(MyVar)
Return MyCheck
End Function

Compare this example from the Help:

Dim MyVar As Object
Dim MyCheck As Boolean
' ...
MyVar = "53" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "459.95" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "45 Help" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns False.

-----Original Message-----
You should still always have option strict on.

You can either make the parameter an Object, or have

several different
versions of the method, with each one having a parameter

of a different type
(and then potentially have them all forwarded to one

method that contains
all the actual logic).

Now, is what being passed here an actual string, or

something else? Can you
reproduce this behavior by just calling IsNumeric from a

little test program
and passing it in some string that cannot be converted

to a numeric?

<an*******@discussions.microsoft.com> wrote in message
news:27*****************************@phx.gbl...

>-----Original Message-----
Hi Cor
I want to validate a value of unknown datatype. That's
why I can't use Option Strict On

Eje

>Hi Eje,
>
>You can set Option Strict On to solve this problem, I
think that that makes
>it probably direct clear to you.
>
>Cor
>
>
>> IsNumeric(value) should return false if value "can not be
>> successfully converted to a Double." Instead I get the >> following error message: "Input string was not in a
>> correct format."
>>
>> I use the following function in a validating class to use
>> when needed. (Value = H880118A gave the error (like
>> other 'unconvertible' strings))
>>
>> Public Function Numeric(ByVal value) As Boolean
>> If CType(value, String).Length = 0 Then
>> m_msg = "Value is empty"
>> Return False
>> End If
>> If IsNumeric(value) Then
>> Return True
>> Else
>> m_msg = "Value isn't numeric"
>> Return False
>> End If
>>
>> End Function
>>
>> What to do?
>> Eje
>
>
>.
>

.

Jul 21 '05 #8

P: n/a
Hi cor,
If you still check this. I found the error. I had to
change an option in the menu Debug/Exceptions

Thanks for your help

Eje
-----Original Message-----
Hi Anon,

Why not as this with option Strict On
See inline bellow

I hope this helps?

Cor

Option Strict On
'...

Public Function test() As Boolean
Dim indata As Object
Dim MyCheck As Boolean
indata = "53"
MyCheck = Numeric(indata.ToString) 'No problem
indata = "45 abc"


MyCheck = Numeric(indata.ToString) 'Still gives

error
indata = "abc"
MyCheck = Numeric(indata.TosString) 'Still gives

error
End Function

Public Function Numeric(ByVal MyVar As String) As
Boolean
Dim MyCheck As Boolean
MyCheck = IsNumeric(MyVar)
Return MyCheck
End Function

Compare this example from the Help:

Dim MyVar As Object
Dim MyCheck As Boolean
' ...
MyVar = "53" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "459.95" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "45 Help" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns False.

>-----Original Message-----
>You should still always have option strict on.
>
>You can either make the parameter an Object, or have

several different
>versions of the method, with each one having a parameter
of a different type
>(and then potentially have them all forwarded to one

method that contains
>all the actual logic).
>
>Now, is what being passed here an actual string, or

something else? Can you
>reproduce this behavior by just calling IsNumeric
from a little test program
>and passing it in some string that cannot be converted

to a numeric?
>
><an*******@discussions.microsoft.com> wrote in message
>news:27*****************************@phx.gbl...
>>
>> >-----Original Message-----
>> Hi Cor
>> I want to validate a value of unknown datatype.
That's >> why I can't use Option Strict On
>>
>> Eje
>>
>> >Hi Eje,
>> >
>> >You can set Option Strict On to solve this problem, I >> think that that makes
>> >it probably direct clear to you.
>> >
>> >Cor
>> >
>> >
>> >> IsNumeric(value) should return false if value "can not
>> be
>> >> successfully converted to a Double." Instead I
get the
>> >> following error message: "Input string was not
in a >> >> correct format."
>> >>
>> >> I use the following function in a validating class to
>> use
>> >> when needed. (Value = H880118A gave the error

(like >> >> other 'unconvertible' strings))
>> >>
>> >> Public Function Numeric(ByVal value) As Boolean
>> >> If CType(value, String).Length = 0 Then
>> >> m_msg = "Value is empty"
>> >> Return False
>> >> End If
>> >> If IsNumeric(value) Then
>> >> Return True
>> >> Else
>> >> m_msg = "Value isn't numeric"
>> >> Return False
>> >> End If
>> >>
>> >> End Function
>> >>
>> >> What to do?
>> >> Eje
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Jul 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.