For Each looping through own collection class

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
Hello there. Most puzzling.

The following worked first time I tried it, now it fails on the highlighted line with
Object does not support this property or method

Simple test Hotel class:
Code:
Option Explicit

Private pName As String




Public Property Get Name() As String
    Name = pName
End Property


Public Property Let Name(Value As String)
    pName = Value
End Property

Hotels collection class:

Code:
Option Explicit

Private pHotels As Collection
Private Sub Class_Initialize()


    Set pHotels = New Collection
    
End Sub


Public Sub Add(Item As Hotel, Key As String)


    pHotels.Add Item, Key
    
End Sub

Public Function NewEnum() As IUnknown
    Set NewEnum = Hotels.[_NewEnum]
    Attribute NewEnum.VB.UserMemID = - 4
End Function

The class was exported
It was then deleted
Attribute line of code was added within Notepad
The class module was reimported

When I first ran the code it worked. Subsequently, it has failed as though the Attribute line had never been added...?

Test sub:

Code:
Option Explicit

Public oHotels As Hotels


Sub Initialise()
    Dim oHotel As Hotel
    Dim r As Range
    
    Set oHotels = New Hotels
    
    For Each r In [a1:a8]
        Set oHotel = New Hotel
        oHotel.Name = r.Value
        oHotels.Add oHotel, oHotel.Name
    Next
    
    [I][B]For Each oHotel In oHotels[/B][/I]
        Debug.Print oHotel.Name
    Next
    
End Sub

I have made great strides over the last few weeks in writing and using classes. It is a little frustrating to not have the liberty to use For Each in my own collection classes. Why would it work once only? That's the mystery.

Thanks for your time, it is much appreciated.

Nigel Foster
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
The puzzle remains - why it ever worked. I have amended the code in the Hotels collection class module to:

Code:
Public Property Get NewEnum() As IUnknown
    Attribute NewEnum.VB_UserMemID= -4
    Attribute NewEnum.VB_MemberFlags= "40"
    Set NewEnum = pHotels.[_NewEnum]
End Property

and it now works reliably.

Thanks for your interest!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top