473,473 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning a Collection object from .NET to VB6 (using COM Interop)

As I mentioned in my other post, I'm attempting to, using COM Interop so I
can update existing VB6 code to (for several specific functions) return a
Hashtable from a .NET library.
I've had very little luck processing the Hashtable itself in VB6 (I can add
a reference to the project so it knows what a Hashtable is, but I'm not
having much luck looping through all objects in the Hashtable), so I decided
to try a different idea.
I thought I'd write a quick function (in .NET) that would convert the
Hashtable to a more-vb6-friendly Collection object instead. That way VB6
could call the function to return the Hashtable, then use that return value
to call my new ConvertToCollection function, returning a Collection.
Problem: for some reason, every time I call a .NET function that returns a
Collection object from VB, if I try to put the return value in a (VB6)
Collection, I geta "type mismatch" error.
I can store the (.NET) Collection in a (VB6) Variant, but when I try to
assign THAT value to my (VB6) Collection, I get the "type mismatch" again...
It's making me wonder if the .NET Collection object is incompatible with the
VB6 Collection object.
Can anyone confirm this? Or least offer suggestions how I can get past this,
so I can work with the data from my Hashtable/Collection from VB6?
Thanks!
-Scott
Dec 6 '05 #1
6 13365
Let me give you a specific example of what I'm seeing:
In my .NET library I added the following function:

Public Function ReturnCollection() As Collection
Dim aColl As New Collection
aColl.Add("test 1", "1")
aColl.Add("test 2", "2")
aColl.Add("test 3", "3")
aColl.Add("test 4", "4")

Return aColl

End Function
In VB6 (using COM Interop), I have a reference to that .NET library, let's
call it MyLibrary.

I'm trying to call it with this code:

Dim oMyLibrary As New MyLibrary
Dim oCollection As Collection

Set oCollection = oMyLibrary.ReturnCollection
And it fails on that Set statement with the "Type Mismatch".
So I changed the VB6 code to the following:

Dim oMyLibrary As New MyLibrary
Dim oObject As Object
Dim oCollection As Collection

Set oObject oMyLibrary.ReturnCollection
Set oCollection = oObject

And this one fails on the last Set statement. But when I add a watch to
oObject and oCollection, it tells me that oCollection is of type Collection
(as expected), and that oObject is of type Object/Collection (which also
seems reasonable).
So why is it failing with a "type mismatch"? Won't it let me assign an
Object/Collection to a Collection?

Thanks!

-Scott
"Scott M. Lyon" <sc******************@rapistan.BLUE.com> wrote in message
news:u%******************@TK2MSFTNGP15.phx.gbl...
As I mentioned in my other post, I'm attempting to, using COM Interop so I
can update existing VB6 code to (for several specific functions) return a
Hashtable from a .NET library.
I've had very little luck processing the Hashtable itself in VB6 (I can
add a reference to the project so it knows what a Hashtable is, but I'm
not having much luck looping through all objects in the Hashtable), so I
decided to try a different idea.
I thought I'd write a quick function (in .NET) that would convert the
Hashtable to a more-vb6-friendly Collection object instead. That way VB6
could call the function to return the Hashtable, then use that return
value to call my new ConvertToCollection function, returning a Collection.
Problem: for some reason, every time I call a .NET function that returns a
Collection object from VB, if I try to put the return value in a (VB6)
Collection, I geta "type mismatch" error.
I can store the (.NET) Collection in a (VB6) Variant, but when I try to
assign THAT value to my (VB6) Collection, I get the "type mismatch"
again...
It's making me wonder if the .NET Collection object is incompatible with
the VB6 Collection object.
Can anyone confirm this? Or least offer suggestions how I can get past
this, so I can work with the data from my Hashtable/Collection from VB6?
Thanks!
-Scott

Dec 6 '05 #2

Scott M. Lyon wrote:
[snip]
It's making me wonder if the .NET Collection object is incompatible with the
VB6 Collection object.
Your wonderings are correct. They are completely different types of
thing! Do not let the name fool you! (as evidence they are completely
different types of thing, I offer their starkly different public
interfaces).
Can anyone confirm this? Or least offer suggestions how I can get past this,
so I can work with the data from my Hashtable/Collection from VB6?


COM Interop is a messy business at best. I am no expert, but my advice
would be to stick to native COM data types.

I will look for your other posts to see if there is anything more
specific I can offer

--
Larry Lard
Replies to group please

Dec 6 '05 #3

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Your wonderings are correct. They are completely different types of
thing! Do not let the name fool you! (as evidence they are completely
different types of thing, I offer their starkly different public
interfaces).

Ahhh, I was afraid of that... In that case, guess I need to find another
solution to my problem.
In a nutshell, I have various .NET public functions that return Hashtable
objects (containing custom defined objects). I need to find a way that VB6
can work with this data.

My original thought with Collections is that I could add another function to
..NET that would convert the Hashtable to a Collection that VB6 could deal
with, and then the VB6 code would have to make the initial call (to get the
Hashtable), then a second call to convert that hashtable to a collection (or
whatever).
Any ideas how I'd be able to pull this off, without significant coding on
the VB6 side every time it needs to work with a Hashtable?
(You see, the problem is that while I support the .NET side of this, the VB6
side is a few other developers, and I really don't want to complicate
matters too terribly much on their end, for fear it would introduce too much
possibility for error)
Thanks!
-Scott
Dec 6 '05 #4

Scott M. Lyon wrote:
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Your wonderings are correct. They are completely different types of
thing! Do not let the name fool you! (as evidence they are completely
different types of thing, I offer their starkly different public
interfaces).

Ahhh, I was afraid of that... In that case, guess I need to find another
solution to my problem.
In a nutshell, I have various .NET public functions that return Hashtable
objects (containing custom defined objects). I need to find a way that VB6
can work with this data.

My original thought with Collections is that I could add another function to
.NET that would convert the Hashtable to a Collection that VB6 could deal
with, and then the VB6 code would have to make the initial call (to get the
Hashtable), then a second call to convert that hashtable to a collection (or
whatever).
Any ideas how I'd be able to pull this off, without significant coding on
the VB6 side every time it needs to work with a Hashtable?


I've had a little google around, and it looks like it might be possible
(with some unpretty trickery) to get VB6-enumerable objects out of .NET
- however even if we managed this, I fear we would still have a problem
with this bit:
objects (containing custom defined objects). I need to find a way that VB6
can work with this data.
In that even if we manage to enumerate through a collection and get
stuff out of a hashtable, we will still have a .net object. Now, if we
are going to write code to present these underlying objects in a COM
friendly way, we might as well go as far as creating a complete
"translation layer" that keeps all out actual Interop calls at the
basic data type level.

What I mean is this: instead of trying to build this:

_____
vb6 side .NET side

hashtable(complex object)
<-- some magic ----------

enumerable collection

For Each complex object

<------- ? invoke methods on the complex object
to get the actual info we want
in a COM-friendly format -------->

COM-friendly basic data types (strings, ints etc)

do actual work
____

it might actual be easier to build this:

vb6 side .NET side

--- tell me about the hashtable ---->

<------- return a simple array of the keys,
or the IDs, or some other basic data type
that identifies each hashtable entry -------

--- ask *specific* *simple* questions about the complex objects
---->

<-------- return basic COM data types that answer the questions
-------

_____________

To try and restate it again (because I'm not sure how clear this is):

Suppose we had on the .NET side a hashtable of string -> string(), eg

Key: "Fruit" Value: { "Apple", "Orange", "Banana" }
Key "Animal" Value: { "Horse", "Dog", "Cat" }

Instead of trying to pass that <en masse> to vb6, instead just have
methods that return:

- the number of top-level entries
- the key of the nth entry
- the number of entries in the list of the nth entry
- the mth entry in the list of the nth entry

which all return basic data types but are still sufficient to get all
the data (eg we call the first, get 2, call the second for 1 and 2, get
Fruit and Animal, call the third for 1, get 3, call the fourth for 1,1;
1,2; 1,3; 1,4 and so on)
(You see, the problem is that while I support the .NET side of this, the VB6
side is a few other developers, and I really don't want to complicate
matters too terribly much on their end, for fear it would introduce too much
possibility for error)


So at the moment they are already operating on these complex .NET
objects, or what?

--
Larry Lard
Replies to group please

Dec 6 '05 #5

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
(You see, the problem is that while I support the .NET side of this, the
VB6
side is a few other developers, and I really don't want to complicate
matters too terribly much on their end, for fear it would introduce too
much
possibility for error)


So at the moment they are already operating on these complex .NET
objects, or what?


No, actually at the moment they're calling old VB6 components to do
essentially the same thing as the new .NET library does.

But we're trying very hard to get away from supporting two different
platforms for exactly the same processing.
Dec 6 '05 #6

Scott M. Lyon wrote:
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
(You see, the problem is that while I support the .NET side of this, the
VB6
side is a few other developers, and I really don't want to complicate
matters too terribly much on their end, for fear it would introduce too
much
possibility for error)


So at the moment they are already operating on these complex .NET
objects, or what?


No, actually at the moment they're calling old VB6 components to do
essentially the same thing as the new .NET library does.

But we're trying very hard to get away from supporting two different
platforms for exactly the same processing.


Well I would say then make a .net layer that just returns the
simple-data-type answers. Without learning the details I can't offer
much more.

--
Larry Lard
Replies to group please

Dec 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rick Clift | last post by:
I am trying to add items to a collection object in VB and keep getting the error: "You must implement the Add(System.Object) method on Microsoft.VisualBasic.Collection because it inherits from...
8
by: Ivan Debono | last post by:
Hi, How is it possible to instantiate an new collection in asp serverside?? Thanks, Ivan
1
by: Karuppasamy | last post by:
H I have a WebService calling a Method in a Remote Object. The Remote method Returns an Object. This Webservice also returns the same object. All things are working fine when this Web Service is...
1
by: vbvjain | last post by:
Hi Thanks for your suggestion, i even tried that but now the error that is generated is "There was an error generating XML". If i declare the input parameter of my web service method as object and...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
4
by: Kevin L | last post by:
I have a collection. I am looping through the collection using a FOR EACH. Is there a way to access the Key from the current item? This code obviously does not work, but I am providing it to...
0
by: bryanp10 | last post by:
I'm working on a project that uses some older VB6 COM objects. One of the classes returns a Collection object, which I am having trouble reading in C#. Can someone point me in the right direction...
5
by: Rich | last post by:
In VB6 I used to be able to add an object like a textbox and see the dropdown properties of that textbox from the collection object: Dim col as collection set col = new collection col.Add(txt1)...
1
by: ropo | last post by:
I am using .NET 2.0 I have a C#.Net App, which uses a .NET Class Library, which accesses a COM object through interop. The C#.Net App also uses a mixed mode C++.Net Class library which uses an...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.