Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong number of argument or invalid property assignment collection adding

Tags:

excel

vba

I've been getting a

wrong number of argument or invalid property assignment collection

error for a long time now, but can't figure out what's wrong. I have a class and a Collection inside that class and a Sub to add values to that collection.

Private sumLosses As Collection

Private Sub Class_Initialize() 
    Set sumLosses = New Collection
End Sub

Public Property Get getSumLosses()
    getSumLosses = sumLosses
End Property

Inside main module:

For Each clientCopy In clientsColl
        clientCopy.getSumLosses.Add 200  'error
        clientCopy.getSumLosses.Add (200) 'error
Next

Why does this fail and how do I add the items to a class' collection?

like image 627
Ans Avatar asked Nov 22 '17 08:11

Ans


2 Answers

sumLosses is of type Collection therefore it is an object and has to be Set to another variable/function.

With using Set it should work:

Public Property Get getSumLosses() As Collection
    Set getSumLosses = sumLosses
End Property

Also defining the property As Collection might be no bad idea (but this didn't cause the error).

like image 132
Pᴇʜ Avatar answered Nov 02 '22 16:11

Pᴇʜ


You need to declare Public Property Get getSumLosses() as a Collection and use Set :

Private sumLosses As Collection

Private Sub Class_Initialize() 
    Set sumLosses = New Collection
End Sub

Public Property Get getSumLosses() as Collection
    Set getSumLosses = sumLosses
End Property

:)

like image 21
ashleedawg Avatar answered Nov 02 '22 17:11

ashleedawg