UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Problem Trying To Make A Property Of A Class The Default.    
 
   
MayasPapaS
post Nov 7 2018, 08:13 PM
Post#1



Posts: 3
Joined: 6-October 18



Caveat: As this is my first post ever, on any forum, I would also appreciate any input regarding the post itself as well!

In a Class Module, clsCollection (a wrapper of the VBA Collection object), a code snippet of which appears below,
I wanted to cause the 'Item' property to be set as the default property/procedure so that I can use the oCollection(Index) syntax instead of oCollection.Item(Index) syntax.
I also wanted the ability to enumerate the items in the collection using the For Each Object In oCollection … Next construct.
According to the Wrox publication "Access 2007 VBA: Programmer's Reference", both of these tasks are doable using the code below, which I copied from the .txt file that I imported into my project.
I also found the same kind of thing in a Wiki article Wiki.Classes.Collection.

End Result:
The default property Attribute doesn't work, but the enumeration one does!
It is worth noting that if I export the file again, after having imported the code below, the NewEnum() function shows it's attribute but the Item() property does not!

Code snippet:
Private mColl As Collection

Public Property Get Item(vIndex As Variant) As Variant
Attribute Item.VB_UserMemId = 0 'Set as default property?? -- Doesn't work!
Set Item = mColl(vIndex)
End Property

Public Function NewEnum() As Iunknown
Attribute NewEnum.VB_UserMemId = -4 'This one works; allowing a For … Each construct!!
Set NewEnum = mColl.[_NewEnum]
End Function

Thanks!!
Go to the top of the page
 
cheekybuddha
post Nov 7 2018, 09:21 PM
Post#2


UtterAccess VIP
Posts: 10,548
Joined: 6-December 03
From: Telegraph Hill


welcome2UA.gif

Try saving the code as you have it to a text file: clsCollection.cls

Go to the VBA editor and remove your class module, don't bother to export if it asks you.

Then import the class text file.

The attributes won't show up in your VBA code but the default should now work.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
MayasPapaS
post Nov 8 2018, 08:57 AM
Post#3



Posts: 3
Joined: 6-October 18



Thanks for your response!
I tried porting it in as a .cls text file. It still doesn't work.
As before, when I export it out again, the exported file shows the Attribute for the NewEnum() function, but not for the .Item() property.

Related to this:
I spent some time searching Docs.Microsoft.com for information on VB Attributes, specifically this one 'Attribute .VB_UserMemId', and was unable to find anything.

(I'm beginning to wonder if the value, 0, for this Attribute, 'VB_UserMemId', has changed!)
Go to the top of the page
 
cheekybuddha
post Nov 8 2018, 02:42 PM
Post#4


UtterAccess VIP
Posts: 10,548
Joined: 6-December 03
From: Telegraph Hill


Hi,

My apologies - it was very late last night when I read/responded to your post and I failed to take in that you had already imported the class file. blush.gif

Can I check - are you doing this in Access? I have just noticed the forum is for Visual Basic.

I have just tried it using Access 2007 and it works as intended.

These are the steps I took:
1. Copy and paste all the code in the wiki article you linked to in to a plain text file.
2. Saved the text file as: MyCol.cls
3. Go to the VBA editor.
4. File menu -> Import File ...
5. Navigate to the saved text file (MyCol.cls)
6. Click OK to import the file.
7. Debug menu -> Compile
8. In a new standard module add the following code:
CODE
Function ColWrapTest() As Boolean

  Dim cw As New CollectionWrapper
  
  With cw
    .Add "Hello", "one"
    .Add "World", "two"
    .Add "Utter", "three"
    .Add "Access", "four"
  End With
  
  Debug.Print cw("one"), cw(2)
  With cw
    Debug.Print .Item(3) & .Item("four")
  End With
  
  ColWrapTest = (Err = 0)
  Set cw = Nothing
  
End Function

9. In the Immediate window (Ctrl + G) type:
CODE
?ColWrapTest

10. Hit Return, and see the following output:
CODE
Hello         World
UtterAccess
True

So, I have just used simple strings for the collection items, but once added, I can the refer to the items using index or string key, and with and without explicit reference to the .Item property.

If you follow the same steps what results do you see?

d

--------------------


Regards,

David Marten
Go to the top of the page
 
MayasPapaS
post Nov 8 2018, 09:10 PM
Post#5



Posts: 3
Joined: 6-October 18



Success!!!!! You might be interested in what I found!

As you suggested I copied the sample Wrapper class into a text file, imported it, compiled and it worked!
It then took me quite awhile to find the difference.
I'm still not sure I believe the difference, but I had a comment line immediately after the property declaration (meaning, obviously, that it was before the Attribute statement), I removed that and it worked!

Does this make sense to you, or should I be looking further (although I have no idea where, and it is working)?


Also, you mentioned that you just noticed that the forum was Visual Basic. Should I have posted this in a different Forum? My decision to post under VB was due to the fact that this was something that was being done outside of the Access application (could be done under VS).


Thank-you for pointing me in the right direction!


Tom Simms
Go to the top of the page
 
cheekybuddha
post Nov 9 2018, 04:30 AM
Post#6


UtterAccess VIP
Posts: 10,548
Joined: 6-December 03
From: Telegraph Hill


Well done,Tom!

Yes, that would make sense.

I hadn't twigged that the comments in the code you posted were verbatim - I just thought you were indicating what the code did to readers of your post. blush.gif

The attributes are parsed differently from the rest of the code when it is imported (notice they do not appear in your VBA code in the module) and so I guess are more sensitive to being in an exact format without distraction. (ie without VBA comments!)

The Visual Basic forum is technically for what is often now referred to as 'Classic Visual Basic', like VB6 (now deprecated by MS). Don't worry - most of the code applies to VBA anyway, and the mods can move your post to the Access Modules noard if they feel it necessary. I just wanted to make sure I understood your environment.

Goof luck with your project! thumbup.gif

d


--------------------


Regards,

David Marten
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    20th November 2018 - 09:16 PM