VB6可能是错误的吗?

VB6可能是错误的吗?,vb6,casting,isnumeric,Vb6,Casting,Isnumeric,是否可以使用IsNumeric()测试字符串并使其返回true,但当您使用CInt()将同一字符串转换为整数并将其分配给integer类型的变量时,会出现类型不匹配错误 我之所以问这个问题,是因为我遇到了一个类型不匹配错误,所以在尝试强制转换字符串之前,我使用IsNumeric()检查字符串是否为数字,但我仍然得到了错误 我正在用这个把头发扯下来 这是有问题的代码。 iGlobalMaxAlternatives=CInt(strMaxAlternatives)是发生错误的地方 Dim strMa

是否可以使用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 Strings\" & sConn & "\HH", "MaxAlt")

If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
    iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If
将strMaxAlternatives设置为字符串
Dim iGlobalMaxAlternatives作为整数
iGlobalMaxAlternatives=0
bSourceIncinfare=真
strMaxAlternatives=ReadStringKeyFromRegistry(“软件\TL\Connection Strings\”&sConn&“\HH”、“MaxAlt”)
如果为数字(strMaxAlternatives)且strMaxAlternatives为“”,则
iGlobalMaxAlternatives=CInt(strMaxAlternatives)
如果结束

是,“3.41”是数字,但不是整数。

根据VB6文档,如果表达式的数据类型是布尔型、字节型、十进制、双精度、整数型、长型、SByte型、短型、单精度、UInteger型、ULong型或UShort型,或者包含其中一种数值类型的对象,则IsNumeric返回True。如果表达式是可以成功转换为数字的字符或字符串,它也会返回True。”

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

其中许多无法转换为整数。例如,“1.5”是数字,但不是整数。因此,您可以将其转换为数字,但不一定是整数。

由于最大整数大小,您可能会出现溢出;货币类型实际上适用于大数(但请注意任何区域问题)。有关Int64的讨论,请参见下面的编辑

' 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))
根据MSDN文件,关于:

  • 如果数据为 表达式的类型为布尔型,字节, 十进制、双精度、整数、长、, SByte,短,单,单整数, ULong或UShort,或 包含这些数字类型之一。 如果表达式为,则它也返回True 可以使用的字符或字符串 已成功转换为数字

  • IsNumeric返回False if表达式 数据类型为日期或数据类型 对象,并且它不包含 数值类型。IsNumeric返回False 如果表达式是字符或字符串 无法转换为数字的

由于类型不匹配,可能是双精度字符干扰了转换。IsNumeric不能保证它是一个整数,只能保证它与一个数字匹配。如果数字是双精度字符,则可能是区域设置(逗号与句点等)导致了异常

您可以尝试将其转换为双精度,然后再转换为整数

' 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 and CLng()而不是CInt()。但仍有可能输入Int64,这在使用VB6时更为困难

我对大整数和整数8类型(都是Int64)使用了以下代码,但它可能不适合您的情况:

testValue = CCur((inputValue.HighPart * 2 ^ 32) + _
                  inputValue.LowPart) / CCur(-864000000000)
此示例来自,但正如我所说,它可能在您的场景中起作用,也可能不起作用。如果您没有大整数类型,则它看起来像:

Private Type LARGE_INTEGER
    LowPart As Long
    HighPart As Long
End Type
有关详细信息,请搜索大整数和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
如果是数字(strMaxAlternatives)和strMaxAlternatives“”,则
出错时继续下一步
iGlobalMaxAlternatives=CInt(strMaxAlternatives)
如果错误号为0,则
Debug.Print“转换错误:&strMaxAlternatives&_
“-”错误描述(&R)
恩迪夫
错误时转到先前的错误处理程序
如果结束

以下代码在Visual BASIC 6中工作时不会出现类型不匹配错误

Dim I As Integer
I = CInt("3.41")
这个变体也是如此

Dim I As Integer
Dim TempS As String
TempS = "3.41"
I = CInt(TempS)
发布相关代码将有助于回答您的问题。基本上,VB6中有几个函数用于将字符串转换为数字

CInt和Int转换为数字,但处理舍入不同。直接赋值有效,相当于使用CInt。但是,我建议继续使用CInt,以使您和您的开发伙伴在将来清楚地了解操作

CInt可用于带有逗号(如“3041.41”)的数字,但VB6在处理区域设置时存在问题,因此,如果您使用的是标准美式英语以外的符号,则会出现奇怪的结果和错误。

是。请尝试以下方法:

If IsNumeric("65537") Then
    Dim i As Integer
    i = CInt("65537") 'throws an error on this line!
End If

这是一个溢出,但我认为它说明了IsNumeric()通常的不可靠性(特别是对于整数-对于双精度,它更可靠)。

VB6没有提供保证CInt不会失败的好方法。我发现最简单的方法就是使用错误处理

function TryParse(byval text as string, byref value as integer) as boolean
  on error resume next
  value = CInt(text)
  TryParse = (err.number = 0)
endfunction

当然,您的错误处理首选项可能会有所不同。

您最好的办法是开始使用它正在使用的实际值记录错误,以便您可以了解发生了什么。

如果IsNumeric可以将字符串转换为数字,它将返回true。即使字符串中有非数字字符。我始终循环字符串一次测试一个字符,然后测试每个字符。如果一个字符失败,那么我可以返回一个错误。

两个选项

改变

If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
    iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If
如果是数字(strMaxAlternatives)和strMaxAlternatives“”,则
iGlobalMaxAlternatives=CInt(strMaxAlternatives)
如果结束

如果是数字(strMaxAlternatives)和strMaxAlternatives“”,则
iGlobalMaxAlternatives=CDbl(strMaxAlternatives)'改为双精度'
如果结束

如果是数字(strMaxAlternatives)和strMaxAlternatives“”,则
如果CDbl(strMaxAlternatives)Mod 1=0,则“确保没有小数点”
iGlobalMaxAlternatives=CInt(strMaxAlternatives)
如果结束
如果结束

刚刚找到这个金块。如果运行以下命令,脚本#1将返回TRUE
SELECT ISNUMERIC('98,0') AS isNum   -- Fails

SELECT CONVERT(INT, '98,0')   -- Fails

SELECT CONVERT(NUMERIC(11,4), '98,0')     -- Fails