I’m trying to create a class with a
Collection in it that will hold other
CASN (kind of like a linked list), I’m not sure if my instantiation of the class is correct. But every time I try to run my code below, I get the error
Object variable or With block not set
CODE BEING RUN:
If (Numbers.count > 0) Then Dim num As CASN For Each num In Numbers If (num.DuplicateOf.count > 0) Then 'ERROR HERE Debug.Print "Added " & num.REF_PO & " to list" ListBox1.AddItem num.REF_PO End If Next num End If
CLASS – CASN:
Private pWeek As String Private pVendorName As String Private pVendorID As String Private pError_NUM As String Private pREF_PO As Variant Private pASN_INV_NUM As Variant Private pDOC_TYPE As String Private pERROR_TEXT As String Private pAddressxl As Range Private pDuplicateOf As Collection '''''''''''''''' Instantiation of String, Long, Range etc. '''''''''''''''' Which I know is working fine '''''''''''''''''''''' ' DuplicateOf Property '''''''''''''''''''''' Public Property Get DuplicateOf() As Collection Set DuplicateOf = pDuplicateOf End Property Public Property Let DuplicateOf(value As Collection) Set pDuplicateOf = value End Property ''''' What I believe may be the cause
Basically what I’ve done is created two
Collections of class CASN and I’m trying to compare the two and see if there are any matching values related to the variable
.REF_PO and if there is a match I want to add it to the
cthisWeek‘s collection of class
CASN in the
DuplicateOf collection of that class.
Hopefully this make sense… I know all my code is working great up to this point of comparing the two
Collection‘s. I’ve thoroughly tested everything and tried a few different approaches and can’t seem to find the solution
I found the error to my first issue but now a new issue has appeared…
This would be a relatively simple fix to your Get method:
Public Property Get DuplicateOf() As Collection If pDuplicateOf Is Nothing Then Set pDuplicateOf = New Collection Set DuplicateOf = pDuplicateOf End Property
EDIT: To address your question – “So when creating a class, do I want to initialize all values to either Nothing or Null? Should I have a Class_Terminate as well?”
The answer would be “it depends” – typically there’s no need to set all your class properties to some specific value: most of the non-object ones will already have the default value for their specific variable type. You just have to be aware of the impact of having unset variables – mostly when these are object-types.
Whether you need a
Class_Terminate would depend on whether your class instances need to perform any “cleanup” (eg. close any open file handles or DB connections) before they get destroyed.