Type mismatch accessing variant array too many times

P

peter

I have a problem with a 2D array containing objects
that crashes when I try to read more els out than I put in.
It gives a Type Mismatch.
Here's the main code (everything except the object)
Hope you can help,
Peter.

Const ID_CMD = 1
Const ID_MAC = 2

Const MAX_ARRAY = 5 ' max index of arrays
Const MAX_ROW = 22 ' max index of contents of all arrays

Private aAllArrays (1 To MAX_ARRAY, 1 To MAX_ROW) As Variant
Private aArrayNames (1 To MAX_ARRAY ) As String
Private aSheetNames (1 To MAX_ARRAY ) As String
Private aCurrIndexes(1 To MAX_ARRAY) As Integer
Private aMaxIndexes (1 To MAX_ARRAY) As Integer

aArrayNames(ID_CMD) = "CMD"
aArrayNames(ID_MAC) = "MAC"

aSheetNames(ID_CMD) = "spec6B"
aSheetNames(ID_MAC) = "MAC"

aCurrIndexes(ID_CMD) = 1
aCurrIndexes(ID_MAC) = 1

aMaxIndexes(ID_CMD) = 1
aMaxIndexes(ID_MAC) = 1
'
' addToArray
'
Private Sub addToArray(iID As Integer, _
vItem As Variant)
If aCurrIndexes(iID) > MAX_ROW Then
MsgBox "Too many objects in " & aArrayNames(iID), vbCritical
Else
MsgBox "Adding " & vItem.id & " to " & aArrayNames(iID) & "
array."
Set aAllArrays(iID,aCurrIndexes(iID)) = vItem
aCurrIndexes(iID) = aCurrIndexes(iID) + 1
aMaxIndexes(iID) = aMaxIndexes(iID) + 1
End If
End Sub ' addToArray
'
' getNextOBJ
' If pointer is in range,
' Get the current obj from its array.
' Increment the pointer to the next one.
' Else Return Nothing.
'
Public Function getNextOBJ(iID As Integer) As Variant
If aCurrIndexes(iID) > aMaxIndexes(iID) Then
Set getNextOBJ = Nothing
MsgBox "Too many objects in array " & iID, vbCritical
Else
Set getNextOBJ = aAllArrays(iID, aCurrIndexes(iID)) ' *******
' Type mismatch at runtime.
' debugger:
' iID = 1
' aCurrIndexes(iID) = 3
aCurrIndexes(iID) = aCurrIndexes(iID) + 1
End If
End Function ' getNextOBJ
'
' main
'
Sub main()
Dim oCMD As clsCMDdefn6B
Set oCMD = New clsCMDdefn6B
oCMD.init Me

addToArray ID_CMD, oCMD
addToArray ID_CMD, oCMD

oCMD = getNextOBJ(ID_CMD)
oCMD = getNextOBJ(ID_CMD)
oCMD = getNextOBJ(ID_CMD) ' ****crashes******
End Sub ' main





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
P

peter

UPDATE
It looks like there's a problem with:
Set getNextOBJ = aAllArrays(iID, aCurrIndexes(iID))
It crashes everytime because of the Set which shouldn't be there.
I took it out and it now crashes when returning from getNextOBJ
in this stmt:
oCMD = getNextOBJ(ID_CMD) in main with:
"Object doesn't support this property or method."

I tried
Set oCMD = getNextOBJ(ID_CMD)
but get: "Object required"

But I still don't understand why altho I'll keep at it.
Maybe oCMD should be a Variant?




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
P

peter

UPDATE 2

I've extracted the code relevant to my last post,
but I still don't have a clue as to what's happening.

Private aAllArrays(1 To MAX_ARRAY, 1 To MAX_ROW) As Variant

Sub main()
Dim oCMD1 As clsCMDdefn6B
Dim oCMD2 As clsCMDdefn6B
Dim oVar As Variant

Set oCMD1 = New clsCMDdefn6B
oCMD1.init Me
oCMD1.id = "oCMD1"

Set aAllArrays(1, 1) = oCMD1
Set aAllArrays(1, 2) = oCMD1

Set oCMD2 = aAllArrays(1, 1) ' ok
Set oCMD2 = aAllArrays(1, 2) ' ok
Set oCMD2 = aAllArrays(1, 3) ' obj reqd

Set oVar = aAllArrays(1, 1) ' ok
Set oVar = aAllArrays(1, 2) ' ok
Set oVar = aAllArrays(1, 3) ' type mismatch




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
P

Peter T

Hi Peter,
Dim oCMD2 As clsCMDdefn6B
Dim oVar As Variant

Set oCMD2 = aAllArrays(1, 1) ' ok
Set oCMD2 = aAllArrays(1, 2) ' ok
Set oCMD2 = aAllArrays(1, 3) ' obj reqd

Set oVar = aAllArrays(1, 1) ' ok
Set oVar = aAllArrays(1, 2) ' ok
Set oVar = aAllArrays(1, 3) ' type mismatch

You indicate that aAllArrays(1, 1) & (1, 2) are objects by the fact that Set
oCMD2 works and "clsCMDdefn6B" no doubt your own object type. You haven't
indicated what aAllArrays(1, 3) is but I'm guessing it's one of the Data
arrays you showed being declared in your first post. Whatever it is I assume
it's not an Object. If so -

Set oCMD2 = aAllArrays(1, 3) ' obj reqd
Although use of Set with oCMD2 is correct, it fails because an object
variable cannot accept values or an array of values, or a variant array of
arrays (even if the arrays are object type).

Set oVar = aAllArrays(1, 3) ' type mismatch
Although oVar is declared as a Variant (which can accept anything), it fails
because of use of "Set". Which in VBA is required and only required when
assigning an Object. Try -
oVar = aAllArrays(1, 3)

You can see what type of variables you have by stepping through and looking
in Locals. Even a variant will be appended with its type.

If the default property of the object returns a data type then you could do
both these:

Set var = oRange ' the range object
var = oRange ' value (if single cell) or array of values in the Range

Regards
Peter T
 
P

peter

Thanks for your msg Peter.

aAllArrays(1, 3) is Nothing because I've put nothing there.

The problem is I can't figure out how to test for it.
The foll. crashes with "object reqd":
If aAllArrays(1, 3) Is Nothing Then _
MsgBox "Nothing here"




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
P

Peter T

Hi Peter

Maybe this will give a few options:

Sub DebugArray()
Dim aL(1 To 2) As Long '
Dim aStr(1 To 2) As String
Dim aRng(1 To 2) As Range
Dim vArrMain(1 To 4)
Dim iType As Integer

For i = 1 To 2
aL(i) = i: aStr(i) = i: Set aRng(i) = Cells(i, 1)
Next
vArrMain(1) = aL
vArrMain(2) = aStr
vArrMain(3) = aRng
'vArrMain(4) ?

For i = 1 To 4
iType = VarType(vArrMain(i))
Debug.Print i; iType

If iType >= 8192 Then
Debug.Print , iType - 8192
End If
Debug.Print , Not IsEmpty(vArrMain(i))
Next

End Sub

See VarType in help, could build up a Select Case structure to handle what
it returns. Otherwise just test for IsEmpty.

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top