• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Where class property is an array, looping through values

dan_l

Active Member
Hi,

This one is vexing me.

Class property:
Code:
Public Property Get avInputs() As Variant()
  m_avInputs = Me.InputRange.value
  avInputs = m_avInputs

End Property


Inputrange.value is a valid assignment set in class initialize.


The precursors seem to be ok:
Code:
Sub test()
Dim OPR As New cOutputRoutine

Debug.Print "My range is" & vbTab & OPR.InputRange.Address
Debug.Print "My ubound for the array is" & vbTab & UBound(OPR.avInputs, 1)

Results in:
My range is $A$1:$C$21
My ubound for the array is 21

But:
Debug.Print "My first value in the array is" & vbTab & OPR.avInputs(1, 1)

Throws error:

"Compile Error: Wrong number of arguments or invalid property assignment"

It's got to be either:
A) I just can't do this (in my fucking around, I tried calling it Inside the class using me. and it didn't work either.
B) I'm missing a piece of syntax somewhere.

The only other hint I've got:
Within the sub if I create another array (dim avTest() as variant) and pull values from the one that's stored in the class (avtest = opr.avinputs) it seems to work ok, but I can't think of a reason why I wouldn't be able to reference it without the copy. That and for this specific application I could see this turning into a performance issue whereas the "test" version of the data is 20+ records, the real version is going to be quite a few more.

EDIT - I should add: the ultimate goal is to loop through the array within the class, not really in sub that calls it. I just did the explanation this way because I think it makes the problem a little bit easier to show.
 
@dan_l
I haven't done much class programming, but to me it seems you need a method in class that can take the parameters (row num, column num) and return the value at intersection.

for eg. try this:

Code:
Public function getAVInputs(r as long, c as long) AsVariant()
  getAVInputs = m_avInputs(r,c)
End function
 
Last edited by a moderator:
Try using this:

Code:
Debug.Print "My first value in the array is" & vbTab & OPR.avInputs()(1, 1)
 
Your property doesn't take any arguments, so you cannot put the indices inside the first parentheses. You have to leave those empty so that it is clear to the compiler that you are simply calling the property with no arguments. Given that that property returns an array, you can then access the array items using a second set of parentheses. It's effectively the equivalent of doing this:

Code:
Dim arr
arr = OPR.avInputs()
debug.Print arr(1, 1)

You'll see similar constructions when trying to access the Items or Keys of a Dictionary.
 
Back
Top