VB ByRef error

Status
Not open for further replies.
I'd like to be able to pass an array-within-an-array from a class, but I'm having difficulty. Here's the Class1 code:
Code:
Private m_Records() As Variant

Property Get Records(Optional Index As Integer = 0) As Variant
    Records = m_Records(Index)
End Property
Property Let Records(Optional Index As Integer = 0, newvalue As Variant)
    If Index < LBound(m_Records) Or Index > UBound(m_Records) Then Err.Raise 9
    m_Records(Index) = newvalue
End Property

Public Sub Run()
countr = 0
Do Until criteria
    ReDim Preserve Records(countr) As Variant
    Dim tempArray(4)
    tempArray(0) = "Val1"
    tempArray(1) = "Val2"
    tempArray(2) = "Val3"
    tempArray(3) = "Val4"
    tempArray(4) = "Val5"
    Records(countr) = tempArray
    countr = countr + 1
Loop
End Sub

Now, on my Form1, I have a command button. It's Click event runs the following code:
Code:
Private Sub Command1_Click()
Dim ret As Class1
Set ret = New Class1
ret.Run
For i = 0 To UBound(ret.Records)
    Print ret.Records(i)(0)      ' if I wanted to just print the first value of the interior array
Next
Set ret = Nothing
End Sub

The error I am getting is a "ByRef argument type mismatch" error which points to the Private Sub Command1_Click() line. This error just started to come up unexpectedly. Before that, I was getting a generic "Type mismatch" error on the For loop.

Can anyone help me with this?
 
Ok, I switched over to a two-dimensional array; this has only given me a compiler error "Argument not optional":
Code:
' On Class1
Private m_Records(10, 5) As Variant

Property Get Records(Index1 As Integer, Index2 As Integer) As Variant
    Records = m_Records(Index1, Index2)
End Property
Property Let Records(Index1 As Integer, Index2 As Integer, newvalue As Variant)
    ' If Index1 < LBound(m_Records) Or Index > UBound(m_Records) Then Err.Raise 9
    m_Records(Index1, Index2) = newvalue
End Property

Public Sub Run()
For countr = 0 To 9
    Records(countr, 0) = "Val1"
    Records(countr, 1) = "Val2"
    Records(countr, 2) = "Val3"
    Records(countr, 3) = "Val4"
    Records(countr, 4) = "Val5"
Loop
End Sub

'On Form1
Private Sub Command1_Click()
    Dim abc As Class1
    Set abc = New Class1
    abc.Run
    For i = 0 To UBound(abc.Records, 1)
        Print abc.Records(i)(0)      ' if I wanted to just print the first value of the interior array
    Next
End Sub

For some reason, it seems to think that I need an argument for abc.Records, such as an index.
 

TheJet

Ars Praefectus
4,598
Subscriptor++
"Print abc.Recorcds(i)(0)"

should

"Print abc.Records(i,0)"

The first method would work correctly, but you really should not do all that "ReDim Preserve" stuff, you should determine ahead of time how many records you'll need, or in the worst case, preallocate your array and only redim preserve when you run out of room and just before you exit. Otherwise your performance will go through the floor.

TheJet
 
Thanks for the feedback so far!

You'll notice in my second posting that I removed the ReDim stuff and just pre-assigned the array.

Here's the modified code on Form1:
Code:
Private Sub Command1_Click()
    Dim abc As Class1
    Set abc = New Class1
    abc.Run
    For i = 0 To UBound(abc.Records, 1)
       Print abc.Records(i, 0)     ' if I wanted to just print the first value of the interior array
    Next
End Sub

I was under the impression that you could return the UBound of the first dimension of a two-dimension array by using the code above. However, I am still getting an "Argument not optional" error from the compiler. Am I doing something wrong with the Property Get and Property Let statements of the class?
 

TheJet

Ars Praefectus
4,598
Subscriptor++
UBound(abc.Records, 1)

Won't work because you haven't declared the arguments to the property as optional, so when you try to retrieve the property in the UBound, it just blows up. The problem is that you're trying to utilize the single property in two different ways, in one case you want to return the array, and in another case you want to return a specific item in the array. So you're asking VB to interpret the same property in two different contexts, which it can't (or won't in this case) do.

If you want the encapsulation capabilities, then simply provide a UBound() property which returns the ubound of the object's internal array, and change the For loop to go through abc.UBound(1).

TheJet
 
Status
Not open for further replies.