加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB6 Can IsNumeric会出错吗?

发布时间:2020-12-17 07:27:59 所属栏目:百科 来源:网络整理
导读:是否可以使用IsNumeric()测试字符串并返回true,但是当您使用CInt()将相同的字符串转换为整数并将其分配给integer类型的变量时,它会给出类型不匹配错误? 我问因为我得到了一个类型不匹配错误,所以我在尝试强制转换之前使用IsNumeric()检查字符串是否为数字,
是否可以使用IsNumeric()测试字符串并返回true,但是当您使用CInt()将相同的字符串转换为整数并将其分配给integer类型的变量时,它会给出类型不匹配错误?

我问因为我得到了一个类型不匹配错误,所以我在尝试强制转换之前使用IsNumeric()检查字符串是否为数字,但我仍然得到错误.

我正用这个撕掉我的头发.

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

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

strMaxAlternatives = ReadStringKeyFromRegistry("SoftwareTLConnection Strings" & sConn & "HH","MaxAlt")

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

解决方法

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

根据IsNumeric的MSDN文档:

>如果数据,IsNumeric返回True
表达式的类型是布尔值,字节,
十进制,双精度,整数,长,
SByte,短,单,UInteger,
ULong,或UShort,或者对象
包含其中一种数字类型.
如果表达式是,它也返回True
可以是Char或String
成功转换为数字.
>如果表达式,则IsNumeric返回False
是数据类型日期或数据类型
对象,它不包含
数字类型. IsNumeric返回False
如果Expression是Char或String
无法转换为数字.

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

您可以尝试将其转换为double,然后转换为整数.

' 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))

编辑:澄清后,您似乎正在进行溢出转换.首先尝试使用Long和CLng()代替CInt().但是仍然有可能输入Int64,使用VB6更难.

我已将以下代码用于LARGE_INTEGER和Integer8类型(均为Int64),但它可能不适用于您的情况:

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

这个例子来自LDAP password expiration example,但就像我说的那样,它可能会或可能不适用于您的场景.如果您没有LARGE_INTEGER类型,它看起来像:

Private Type LARGE_INTEGER
    LowPart As Long
    HighPart As Long
End Type

搜索LARGE_INTEGER和VB6以获取更多信息.

编辑:对于调试,暂时避免错误处理,然后在通过麻烦的线后重新打开它可能是有用的:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读