我问因为我得到了一个类型不匹配错误,所以我在尝试强制转换之前使用IsNumeric()检查字符串是否为数字,但我仍然得到错误.
我正用这个撕掉我的头发.
这是有问题的代码.
iGlobalMaxAlternatives = CInt(strMaxAlternatives)是发生错误的地方.
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
解决方法
根据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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。