Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 - ByRef argument type mismatch error - where is it?

Tags:

vb6

For the 10+ years I've been using VB6, every now and then I get a "ByRef argument type mismatch" error where I simply can't find the mismatch. After struggling for a while I've always punted by forcing the type one way or another, but this time I thought I'd ask. I'm including all the code I think could have anything to do with this; but you can skip it now and refer to it after I demonstrate the problem:

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

The problem occurs below. The "LuxMinTarget = MeasureLuxAtCode(FirstLevel)" line generates a "ByRef argument type mismatch" error, indicating that FirstLevel is not an integer.

Sub DetermineIdealLuxCurve()
  Dim FirstLevel, FirstDACtoMeasure As Integer
  FirstDACtoMeasure = 0
  FirstLevel = GammaBuffer(SelectedBank).Location(FirstDACtoMeasure)
  LuxMinTarget = MeasureLuxAtCode(FirstLevel)
End Sub

But dang it, FirstLevel is an integer, isn't it? It's dim'ed an int, its value is set by a UDT that returns an int, so where did I go wrong? If I force it to an int like this:

  LuxMinTarget = MeasureLuxAtCode(Int(FirstLevel))

the compiler/interpreter's happy. But I'm not.

So is this a bug in the compiler or just me being dense?

like image 597
Fred Hamilton Avatar asked Mar 27 '09 01:03

Fred Hamilton


People also ask

How do you fix a ByRef argument type mismatch?

So, if you try to run the code, we will get a VBA Error as “ByRef Argument Type Mismatch.” As you can see above, variable “B” gets highlighted because of the variable name type mismatch. Solution: To overcome this issue, we must ensure that variable names in both procedures are exact.

What is argument type mismatch?

You passed an argument of one type that could not be coerced to the type expected. For example, this error occurs if you try to pass an Integer variable when a Long is expected. If you want coercion to occur, even if it causes information to be lost, you can pass the argument in its own set of parentheses.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.


2 Answers

The problem is here:

Dim FirstLevel, FirstDACtoMeasure As Integer

This actually declares FirstLevel as a Variant and not as an Integer as you might expect.

This is a classic VB6 gotcha! (and you are not the first to be bitten by it).

Declaring a variable per line avoids the problem:

Dim FirstLevel As Integer
Dim FirstDACtoMeasure As Integer
like image 200
Mitch Wheat Avatar answered Sep 30 '22 08:09

Mitch Wheat


John T, You actualy can explicitly declare variables in a list.

Dim FirstLevel as Integer, FirstDACtoMeasure As Integer

like image 32
Cidtek Avatar answered Sep 30 '22 08:09

Cidtek