• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 837
  • Last Modified:

How to implement _NewEnum with VB

Hi there.

Is it possible to create a class that implementes its own NewEnum-Interface(for...each), WITHOUT using a collection?

If so, how?
I deposit 50 points for a "no", and will raise to 250 if there is a solution in sight.

tx, holli
0
holli
Asked:
holli
  • 5
  • 3
  • 2
  • +2
1 Solution
 
bob_onlineCommented:
I don't think so.  The For Each construction exist for iterating through a members of a collection.  If the members are not in a collection, what are they in?  If they're in an array, you can for use For lowerbound to upperbound.
0
 
Richie_SimonettiIT OperationsCommented:
I am with bob, excellent explanation by the way :)
0
 
NazdorCommented:
You can use For..Each for variables other than Collections, eg:

dim a(20) as string
dim v as variant
for each v in a
   ...
next

But the 'v' must be a variant.

I don't know how you could use this with regard to a NewEnum entry, because you can't do:

   a.[_NewEnum]

but the above might help find an alternative solution, eg, expose the array directly (read-only), as in:

   for each v in myclass.a
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
holliAuthor Commented:
hi nazdor,

this is a nice one.
it does not answer the question, but i will deposit 50 points for you in this ta.

I believe, to do what i want i have to implement the original collections interface (or parts of it).
this is somewhat hardcore so i`ll pay 300 points for the way to do.


holli
0
 
NazdorCommented:
I suspect you might have to look elsewhere than in VB (might be wrong though).  

For example, you can't "Implements Collection" as the collection has _NewEnum method which can't be implemented.


When you say "without using a collection" - do you mean without using a collection of sub-objects?  As in:

clsQuestions
clsQuestion

Where clsQuestions is a collection wrapper for clsQuestion.


If this is what you're trying to get away from, then you could alway just use a generic "collection" within the main class and add whatever you want to it - eg strings, numbers.

Depends on what you're trying to do...
0
 
holliAuthor Commented:
"For example, you can't "Implements Collection" as the collection has _NewEnum method which can't be implemented."

Sure you can. If you would know the collections` interface (or better the interface of the collection-class) i, you could create a compatible class using "Implements". Any routines using a collection would then also support the new class, including "for...each".

What i want to do:
I created an associative-array class (like a perl hash). This class uses to parallel collections, one for the keys, one for the values.
i now want to replace the keys-collection by an other (faster) data-structure (e.g. an array or binary tree), but i donĀ“t want to loose the "for...each"-capability.

tx,

holli


0
 
holliAuthor Commented:
nazdor, would you be so kind and get you 50 points at
so the question can be closed (and this one also?)

tx, holli
0
 
Richie_SimonettiIT OperationsCommented:
Holli, why did you post another question? just accepting some of nazdor's comment here would be enought.
Cheers
0
 
NazdorCommented:
Sorry for the delay.

Given Richie's comment, I've (now) not added anything to the other question as I'm lead to believe it's easier to get it deleted if there's no comments.

If you still want me to have the points, just let me know.
0
 
holliAuthor Commented:
So shall it be.
0
 
iainshepherdCommented:
For the benefit of anyone (like me) who was looking for this,
Bruce McKinney has the answer.
http://www.mvps.org/vb/hardcore/html/enumeratingvariants.htm

And it's messy. Very messy...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now