VB6 Can IsNumeric是否会出错? [英] VB6 Can IsNumeric be wrong?

查看:148
本文介绍了VB6 Can IsNumeric是否会出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用IsNumeric()测试一个字符串,并且返回true,但是当你使用CInt()将同一个字符串转换为一个整数,并将它赋给一个类型为integer的变量时,它会给出一个类型不匹配错误?



我问,因为我得到一个类型不匹配错误,所以我使用IsNumeric()来检查字符串是数字之前,试图把它,得到错误。



我用这个撕掉了我的头发。



这是有问题的代码。
iGlobalMaxAlternatives = CInt(strMaxAlternatives)是发生错误的地方。

  Dim strMaxAlternatives As String 
Dim iGlobalMaxAlternatives As Integer
iGlobalMaxAlternatives = 0
bSurchargeIncInFare = True

strMaxAlternatives = ReadStringKeyFromRegistry(Software\TL\Connection如果IsNumeric(strMaxAlternatives)和strMaxAlternatives<>><> then
iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If


方案

由于最大整数大小,可能有溢出;货币类型实际上对大数字非常好(但要注意任何区域问题)。有关Int64讨论,请参阅下面的编辑。



根据 IsNumeric




  • 如果数据
    ,IsNumeric会返回True表达式的类型是布尔值,字节,
    十进制,双精度,整数,长整型,
    SByte,Short,Single,UInteger,
    ULong或UShort或
    包含的对象这些数字类型之一。
    如果Expression是
    也返回True。可以是
    的字符串或字符串转换成数字。


  • IsNumeric返回False如果表达式
    是数据类型Date或数据类型
    对象且不包含
    数字类型。 IsNumeric返回False
    如果Expression是一个Char或String
    ,不能转换为数字。




由于您遇到类型不匹配,因此Double可能会干扰转换。 IsNumeric不保证它是一个整数,只是它匹配一个数字的可能性。如果数字是双精度,可能是区域设置(逗号与句号等)引起异常。



您可以尝试将其转换为双精度整数。

 '使用几个步骤
Dim iValue As Integer
Dim dValue As Double
dValue = CDbl(SourceValue)
iValue = CInt(iValue)
'或者在一个步骤中(可能使调试更困难)
iValue = CInt(CDbl(SourceValue))

编辑:在您澄清后,您似乎遇到溢出转换。首先尝试使用Long和CLng()而不是CInt()。仍然有一个机会,条目是Int64虽然,这是更困难的使用VB6。



我使用下面的代码为LARGE_INTEGER和Integer8类型(Int64)但不适用于您的情况:

  testValue = CCur((inputValue.HighPart * 2 ^ 32)+ _ 
inputValue.LowPart)/ CCur(-864000000000)



此示例来自 LDAP密码过期示例,但是像我说的,它可能或可能不会在您的方案中工作。如果您没有LARGE_INTEGER类型,它看起来像:

 私有类型LARGE_INTEGER 
LowPart As long
HighPart As Long
结束类型

有关详细信息,请搜索LARGE_INTEGER和VB6。 / p>

编辑:对于调试,暂时避免错误处理,然后在通过令人不安的行之后重新启动它可能是有用的:

 如果IsNumeric(strMaxAlternatives)和strMaxAlternatives<> then 
On Error Resume Next
iGlobalMaxAlternatives = CInt(strMaxAlternatives)
如果Err.Number<> 0 Then
Debug.PrintConversion Error:& strMaxAlternatives& _
- &错误描述
EndIf
错误时转到YourPreviousErrorHandler
如果
结束


Is it possible to test a string with IsNumeric() and for it to return true, but when you cast that same string to an integer using CInt() and assign it to a variable of type integer that it will give a type mismatch error?

I ask because I was getting a type mismatch error, so I used IsNumeric() to check the string was numeric before trying to cast it, but I still get the error.

I am tearing my hair out with this.

Here is the code in question. iGlobalMaxAlternatives = CInt(strMaxAlternatives) is where the error is occuring.

Dim strMaxAlternatives As String
Dim iGlobalMaxAlternatives As Integer
iGlobalMaxAlternatives = 0
bSurchargeIncInFare = True

strMaxAlternatives = ReadStringKeyFromRegistry("Software\TL\Connection Strings\" & sConn & "\HH", "MaxAlt")

If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
    iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If

解决方案

You may have an overflow due the maximum integer size; the currency type actually does very well for large numbers (but beware of any regional issues). See edits below for Int64 discussion.

According to MSDN documentation on IsNumeric:

  • IsNumeric returns True if the data type of Expression is Boolean, Byte, Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or UShort, or an Object that contains one of those numeric types. It also returns True if Expression is a Char or String that can be successfully converted to a number.

  • IsNumeric returns False if Expression is of data type Date or of data type Object and it does not contain a numeric type. IsNumeric returns False if Expression is a Char or String that cannot be converted to a number.

Since you are getting a Type Mismatch, perhaps a Double is interfering with the conversion. The IsNumeric does not guarantee it is an Integer, just that it matches one of the numeric possibilities. If the number is a double, perhaps regional settings (comma versus period and so on) are causing the exception.

You might try converting it to a double and then to an integer.

' Using a couple of steps
Dim iValue As Integer
Dim dValue As Double
dValue = CDbl(SourceValue)
iValue = CInt(iValue)
' Or in one step (might make debugging harder)
iValue = CInt(CDbl(SourceValue))

EDIT: After your clarification, it appears you are getting an overflow conversion. First try using a Long and CLng() instead of CInt(). There is still a chance the entry is Int64 though, which is more difficult using VB6.

I have used the following code for the LARGE_INTEGER and Integer8 types (both Int64), but it may not work for your situation:

testValue = CCur((inputValue.HighPart * 2 ^ 32) + _
                  inputValue.LowPart) / CCur(-864000000000)

This example was from an LDAP password expiration example, but like I said it may or may not work in your scenario. If you don't have the LARGE_INTEGER type, it looks like:

Private Type LARGE_INTEGER
    LowPart As Long
    HighPart As Long
End Type

Search for LARGE_INTEGER and VB6 for more information.

EDIT: For debugging, it may be useful to temporarily avoid error handling and then turn it back on after passing the troubling lines:

If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
    On Error Resume Next
    iGlobalMaxAlternatives = CInt(strMaxAlternatives)
    If Err.Number <> 0 Then
        Debug.Print "Conversion Error: " & strMaxAlternatives & _
                    " - " & Err.Description
    EndIf
    On Error Goto YourPreviousErrorHandler
End If

这篇关于VB6 Can IsNumeric是否会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆