Coder Social home page Coder Social logo

Comments (19)

DecimalTurn avatar DecimalTurn commented on June 16, 2024 6

Here's how VBA-FastDictionary does it : https://github.com/cristianbuse/VBA-FastDictionary/blob/028e151af3436e71802bc6fecf9676fc1e7e3727/Implementation.md#newenum

from vba-dictionary.

skchan2 avatar skchan2 commented on June 16, 2024 1

@dimitropoulos I tried adding your code, but still not able to get it to work. Oddly, the original code is not even working in Windows. Getting stuck in the "For Each" part, weird

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
    'Gets an enumerator that iterates through the collection.
#If Mac Or Not UseScriptingDictionaryIfAvailable Then
    Set NewEnum = dict_pKeyValues.[_NewEnum]
#Else
    Set NewEnum = dict_pDictionary
#End If
End Property

from vba-dictionary.

 avatar commented on June 16, 2024 1

@hrmck I deleted the pull request. But you should be able to find a link to my updated code here

from vba-dictionary.

dimitropoulos avatar dimitropoulos commented on June 16, 2024

The solution to this might look something like:

Public Function NewEnum() As IUnknown 'or IEnumVARIANT
#If Mac Or Not UseScriptingDictionaryIfAvailable Then
    Set NewEnum = dict_pKeyValues.[_NewEnum]
#Else
    Set NewEnum = dict_pDictionary
#End If
End Function

but I can't quite get the syntax to cooperate.

from vba-dictionary.

timhall avatar timhall commented on June 16, 2024

Wow, can't believe I hadn't tested this, great catch @dimitrimitropulos. I'll look into it, but if you find a solution please let me know.

from vba-dictionary.

retailcoder avatar retailcoder commented on June 16, 2024

That requires a VB_UserMemId = -4 member attribute:

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
'Gets an enumerator that iterates through the collection.
    Set NewEnum = dict_pKeyValues.[_NewEnum]
End Property

Member attributes can't be entered directly in the VBE though; needs to be exported, edited in notepad, and then imported back in.

from vba-dictionary.

skchan2 avatar skchan2 commented on June 16, 2024

was this bug ever fixed? Having issues with this in the latest version on a Mac

from vba-dictionary.

dimitropoulos avatar dimitropoulos commented on June 16, 2024

I don't believe it was ever fixed. I would do so myself, but I have long since moved on from VB.

You could patch it yourself by copying the cls file into your project and using it instead, and then add in what I had above and see if you can get it to work.

from vba-dictionary.

nectorprime avatar nectorprime commented on June 16, 2024

from vba-dictionary.

timhall avatar timhall commented on June 16, 2024

@skchan2 This hasn't been fixed just yet, I gave it a good go, but couldn't quite get it working right. Will look into it again in the future, but if you get it working please send a PR or a code sample!

from vba-dictionary.

dimitropoulos avatar dimitropoulos commented on June 16, 2024

it would help a lot if some of this stuff was documented, but my recollection is that it (e.g. IUnknown) either isn't documented at all or is documented in such a way that it assumes you already understand it.

If anyone reading this issue knows of documentation for adding For Each support on custom data structures, I'm sure it would be welcomed if you mentioned it here.

from vba-dictionary.

skchan2 avatar skchan2 commented on June 16, 2024

another thing is that, maybe we can update it so that this class is only referenced when not on a PC (im seeing the for each issue when testing on PC, but run fine when I remove the class).

from vba-dictionary.

dimitropoulos avatar dimitropoulos commented on June 16, 2024

yeah, I'm sorry. I won't be much help to you at this point. My VB days are behind me and I don't really remember how it works in this case. What I do remember is that you're stepping into somewhat uncharted territory so my best suggestion is to try and find (or construct) simpler versions of the same functionality for any other collection and see if you can get it to work. that... or... find some documentation (I was never able to find the documentation for this stuff but that was a few years ago now so maybe it now exists).

from vba-dictionary.

nectorprime avatar nectorprime commented on June 16, 2024

from vba-dictionary.

 avatar commented on June 16, 2024

Final update: I believe I was able to get a version with your original code working. As I noted in my earlier post, your code uses a dictionary object when the library is available, or when the OS isn't a mac. This is an issue because NewEnum can't be used on a dictionary and can only be used on a collection. The solution is to iterate through the keys, add them to a collection, and return that collection from NewEnum.

I believe in your code, when it's used a mac or when ScriptingDictionary isn't available, it store the keys in an array. And when it is available, it stores the keys scripting.dictionary object. So you can do conditional compilation to determine which of these to iterate through, add them to a collection, and return that collection to NewEnum.

I believe my main changes to your code were adding the NewEnum method, adding a private collection field called Coll at the top of this module, and setting coll = nothing in the class_terminate event. It works for me on Windows, but I don't have a mac to test on so I can't confirm.

from vba-dictionary.

hrmck avatar hrmck commented on June 16, 2024

Final update: I believe I was able to get a version with your original code working. Please find the final file here.

@beyphy the link is broken, could you update it?

from vba-dictionary.

Greedquest avatar Greedquest commented on June 16, 2024

This could be achieved by constructing the IEnumVariant struct in memory e.g.: https://stackoverflow.com/a/52261687/6609896

Scripting.Dictionary does not expose [_NewEnum] like Collection does, nor does the array returned by dict.keys(). Therefore there is no way to "forward" for..each calls onto the underlying type - unless it's a collection - so you have to make your own structure to do the enumeration (like in the linked question).

from vba-dictionary.

SHIMADONBEY avatar SHIMADONBEY commented on June 16, 2024

@skchan2 @dimitropoulos made a modification based on the code presented.
The following code was able to verify that it works with the Windows version of Excel VBA. Unfortunately, I do not have a Mac, so I have not been able to verify the operation of the Mac version of Excel VBA.

Public Function NewEnum()
Attribute NewEnum.VB_UserMemId = -4
    'Gets an enumerator that iterates through the collection.
#If Mac Or Not UseScriptingDictionaryIfAvailable Then
    Set NewEnum = dict_pKeyValues.[_NewEnum]
#Else
    Set NewEnum = CallByName(dict_pDictionary, "_NewEnum", vbMethod)
#End If
End Function

I wasn't sure if the IUnknown interface would be supported by the Mac version of VBA, so I changed the return value to Variant. (This has been confirmed to work in my environment.)

from vba-dictionary.

Nick-vanGemeren avatar Nick-vanGemeren commented on June 16, 2024

' Run-time error '438': Object doesn't support this property or method
For Each z In x
While it doesn't replicate the Scripting functionality, this 'feature' does force you to consider whether you want to iterate on Keys or Items.

For Each z In x.Keys
... works fine and is clearer code.

from vba-dictionary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.