VB6 referencing a .NET Hashtable

S

Scott M. Lyon

I'm working on using the COM Interop wrapper on an existing .NET library, to
allow existing VB6 applications to call the .NET code.


And I've been able to get it working in a basic sense (I can return basic
types like strings and booleans, and I've added a reference to MSCORLIB.tlb
so I can even return Hashtables, which the .NET library uses quite a bit).


However, I cannot figure out how to work with the Hashtable objects from
VB6.


Here's an example. I have a .NET function called GetProjects() that returns
a Hashtable of Project objects. The Project object is one that we defined,
and has a variety of methods and properties. All I need for the moment is
the .Name property (which returns a string).


I'd like to get the Hashtable, and simply loop through the objects contained
therein, and display the Project.Name property for each.


I was thinking something along the lines of:


Dim aLibrary As New NETLibrary ' This is my .Net library that I'm trying
to call from VB6
Dim aHash As New Hashtable
Dim aProject As Project

Set aHash = aLibrary.GetProjects

For Each aProject in aHash
Debug.Print aProject.Name
Next


Unfortunately, VB6 isn't liking that "for each" syntax. But I'm at a loss to
figure out how to do this otherwise.


Can anyone offer suggestions?


Thanks!
 
G

Guest

Scott,

I have no idea how to work with a .Net hashtable in VB6, or even if you can
work with a .Net hashtable in VB6.

However, I do know that you cannot loop through a hashtable like you are
attempting, even in .Net.

Remember, a hashtable holds a collection of DictionaryEntry objects. So in
..Net you would do something like this:

Dim aProject As Project
For each de As DictionaryEntry In aHash
aProject = de.Value
Msgbox aProject.Name
Next

Kerry Moorman
 
S

Scott M. Lyon

Kerry Moorman said:
Scott,

I have no idea how to work with a .Net hashtable in VB6, or even if you
can
work with a .Net hashtable in VB6.

However, I do know that you cannot loop through a hashtable like you are
attempting, even in .Net.

Remember, a hashtable holds a collection of DictionaryEntry objects. So in
.Net you would do something like this:

Dim aProject As Project
For each de As DictionaryEntry In aHash
aProject = de.Value
Msgbox aProject.Name
Next

I've actually found a few ways that I can loop through a hashtable in .NET.
The on you mentioned (using DictionaryEntry objects to do a FOR EACH)
doesn't apply to VB6 because you can't do a FOR EACH in VB6 except on
generic Variant or Object objects. And if I change it to a Variant (or
Object), then FOR EACH oVariant IN aHash fails.

The other ways I found was using either iEnumerator or iDictionaryEnumerator
(and the Hashtable's GetEnumerator function), and using the MoveNext
property of the enumerator to loop through the items.

Unfortunately, the iEnumerator (when exposed in VB6) while it allows me to
MoveNext through the enumerations, each one comes through as empty (despite
me verifying that the hashtable has data in it).

Just as unfortunately, the iDictionaryEnumerator won't even allow me to use
the .MoveNext method in VB6.


Anyone have any other ideas/suggestions?


Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top