Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

collection question 1

Status
Not open for further replies.

bbolte

Programmer
Joined
Sep 30, 2002
Messages
113
Location
US
this is crazy and i can't figure it out. i've implemented a collection in my main class and populated it. there are 4 collections (collectionobject.count verified it), each with a couple of dozen items. i'm trying to get to the items in another class. i passed in the collection index number and am using this to get the item(s):

Code:
coljob.item(ColJobNum).KernPairNegLC

also tried

Code:
coljob(ColJobNum).KernPairNegLC

i also instantiated the collection object (coljob) at the top of the class with this:

Code:
Public coljob As colArtJob

but i get this error at that line: Run-time error '91': object variable or with block variable not set

so, what have i missed? :(
 
Public coljob As colArtJob

That statement just declares the variable, reserves 4 bytes of memory for the pointer to the object and initializes it to Nothing. You also need:

Set coljob = New colArtJob

Paul Bent
Northwind IT Systems
 
i still get the same error as soon as it hits this line:

If Len(coljob(ColJobNum).KernPairNegLC) > 0 Then arKernPairPosL = Split(coljob(ColJobNum).KernPairPosLC, " ")
 
I would add a watch and set a breakpoint. Make sure the index value, ColJobNum, is returning an object from the collection.

Paul Bent
Northwind IT Systems
 
Break your If statement i.e.

If Len(coljob(ColJobNum).KernPairNegLC) > 0 Then
arKernPairPosL = Split(coljob(ColJobNum).KernPairPosLC, " ")
End If

I suspect that one of your properties ("KernPairNegLC" or "KernPairPosLC") is set but the other isn't. You should be able to uniquely identify which property is giving you the problem if they are on different lines.
 
it has something to do with my collection class - colartjob. it bounces through to here:

Public Property Get item(vntIndexKey As Variant) As clsArtJob
Set item = mCol(vntIndexKey)
End Property

and throws and inproper procedure error. i don't think i'm calling my collection items properly. hmmmm...
 
The colartjob class should look like this:
Code:
Option Explicit

Private mCol As Collection
'_______________________
Public Property Get Count() As Long
    
    Count = mCol.Count
    
End Property
'_______________________
Public Function Add(ByVal Prop1 As Whatever, _
ByVal Prop2 As Whatever, _
ByVal Prop3 As Whatever) As clsArtJob

    'Create a new ArtJob object
    Dim objArtJob As clsArtJob
    Set objArtJob = New clsArtJob
    
    'Set the properties passed into the method
    With objArtJob
        .Prop1 = Prop1
        .Prop2 = Prop2
        'and so on...
    End With

    'Add the object, Prop1 is the optional key
    mCol.Add objArtJob, Prop1
    
    'Return the new ArtJob object
    Set Add = objArtJob
    Set objArtJob = Nothing

End Function
'____________________
Public Sub Remove(ByVal Index As Variant)
    
    mCol.Remove Index
    
End Sub
'_____________________
Private Sub Class_Initialize()
    
    'Initialise the collection
    Set mCol = New Collection
    
End Sub
'______________________
    
    'Destroy the collection when the class goes out of existence
    Set mCol = Nothing
    
End Sub
[code]

Then providing you call the Add method to populate the collection, there shouldn't be any problem returning objects from the class via the Item property.

Paul Bent
Northwind IT Systems
[URL unfurl="true"]http://www.northwindit.co.uk[/URL]
 
i have that all in there. that's why i can't figure out what is going on. it's got to be something stupid that i'm missing somewhere...

thanks for the help
 
ok, i'm still struggling with this. here's the latest:

from within the class that the collection(s) are created in, i can access the items fine. but i still can't get to them from a different class.

in the main class, i create 4 collections, each with about 24 items. i then do a for-each loop to access the collection, i pass the collection index number to a different procedure (within the main class) and it works like i want - i can access the invidual items for that collection.

however, when i try to pass the index number to a different class and retrieve an item from that collection within the 2nd class, i get "run-time error 5 - invalid procedure call or argument". but i have it set up almost exactly in both classes.

here is my question though, when i instantiate the collection object in the second class with:

Code:
Private Sub Class_Initialize()
    '// set up a collection
    Set coljob = New colArtJob
End Sub

isn't that creating a new collection? that's what it appears to be doing as i'm stepping through the code. don't i just want to get the existing collections?

when i make a call to an item with: LINE1_W = coljob(ColJobNum).Copy1_W, i can watch it go to the collection class and as soon as it hits:

Code:
Public Property Get item(vntIndexKey As Variant) As clsArtJob
  Set item = mCol(vntIndexKey)
End Property

is when the error is thrown. what am i missing here?
 
The object reference in the main class which points to one of the 4 collection classes, obviously has private scope within the main class.

In the 2nd class, you're just instantiating a new collection class object but not adding anything to it. Therefore a call to the Item property raises error 5.

If the 4 collections are populated in the main class and you want to access them from outside, create public properties in the main class to expose the 4 collections.

So in the main class you might already have:

Private coljob As colArtJob

and have populated it. To make this collection available outside the main class:

Public Property Get ArtJobs As colArtJob
Set ArtJobs = mCol
End Property

I think that will work, given that mCol is typed as a Collection and ArtJobs is a colArtJob. I just typed a similar construct into an existing collection class and didn't get a compile error.

Paul Bent
Northwind IT Systems
 
What am I talking about, you've got me going too :-) s/be

Public Property Get ArtJobs As colArtJob
Set ArtJobs = coljob
End Property

And ignore the last paragraph.

Paul Bent
Northwind IT Systems
 
i must be really thick. i'm still getting the error. how do i reference "artjobs" from the second class?
 
Following on from my example where the private collection object in the main class is exposed as a public property. In the second class you need to create an instance of the main class and access the property. This presupposes instantiating the main class automatically populates its private collection of ArtJobs and you don't have to call a method to do so.

Dim objMain As MainClass
Set objMain = New MainClass
objMain.ArtJobs.Item(ColJobNum) 'returns an item from the collection.

Effectively, this saves replicating the code that populates the colArtJobs collection in both the main and second classes.

Paul Bent
Northwind IT Systems
 
interesting. i'll keep plugging away at it. thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top