在我使用VB6的10多年里,我偶尔得到一个"ByRef参数类型不匹配"错误,我根本找不到不匹配.经过一段时间的挣扎之后,我总是以这种或那种方式强迫这种类型,但这次我想我会问.我包含了我认为可能与此有关的所有代码; 但是你现在可以跳过它并在我演示问题后引用它:
Public Type PBufferType Location(9) As Integer ' code location ValueHi(9) As Integer ' Vhi code ValueLo(9) As Integer ' Vlo code Locked(9) As Integer ' State of pair Gamma(9) As Single ' Gamma between this segment and next End Type Public GammaBuffer(1) As PBufferType ' The main data type Public SelectedBank as Integer Function MeasureLuxAtCode(code As Integer) As Single Call TestPatternForm.DrawTestWindow(3, code) MeasureLuxAtCode = MeasureLux(1) End Function
问题发生在下面."LuxMinTarget = MeasureLuxAtCode(FirstLevel)"行生成"ByRef参数类型不匹配"错误,指示FirstLevel不是整数.
Sub DetermineIdealLuxCurve() Dim FirstLevel, FirstDACtoMeasure As Integer FirstDACtoMeasure = 0 FirstLevel = GammaBuffer(SelectedBank).Location(FirstDACtoMeasure) LuxMinTarget = MeasureLuxAtCode(FirstLevel) End Sub
但是它,FirstLevel 是一个整数,不是吗?它是一个虚拟的int,它的值由一个返回int的UDT设置,那么我哪里出错?如果我强制它为这样的int:
LuxMinTarget = MeasureLuxAtCode(Int(FirstLevel))
编译器/解释器很高兴.但我不是.
这是编译器中的一个错误还是我的密集?
问题出在这里:
Dim FirstLevel, FirstDACtoMeasure As Integer
这实际上声明FirstLevel
为a Variant
而不是Integer
你所期望的.
这是经典的VB6陷阱!(而且你不是第一个被它咬伤的人).
每行声明一个变量可以避免这个问题:
Dim FirstLevel As Integer Dim FirstDACtoMeasure As Integer