473,785 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to Enumerate a C# class in VB6

I have a bit of a complicated mix of COM and .Net here, so bear with
me while I explain :)

We have a Type Library that defines all of the interfaces used in this
project (a rather large one at that) which was created in VB6. For a
number of unrelated reasons, we've chosen to create new .Net classes
that implement these interfaces, which are, for the most part, working
correctly. We are able to pass our new objects into our COM classes
and perform most operations on them, with one major exception:
Enumeration.

Enumeration is defined in the existing type library.

In VB6 it appears as: IObject_NewEnum () as stdole.IUnknown
In .Net, it maps to: System.Collecti ons.IEnumerator GetEnumerator()

I've created an internal Enumerator class on the .Net side to support
the enumeration, and it works correctly when enumerating from .Net,
however, when attempting to enumerate from VB6, I receive: ErrorCode
-2146827864 "Object Required". Looping through the objects by index
works correctly in both .Net and VB6.
Based on search results, I've tried adding [DispId(-4)] to the
GetEnumerator() method, which is apparently supposed to direct .Net to
output the enumerator as an EnumVariant, but I've had no success
here. My belief is that this isn't working because DispId needs to be
specified on the interface, however, I do not have access to change
this; I'm only able to work with the implementation (in either case,
I'm not certain how, or if, this could be done on a VB6 interface
anyway).
Ideally, I'm looking for a solution which does not require a change to
the type library. Is there another way to force C# to output the
enumerator in a format which VB can understand?

Or, if there is no other way than to change the interface, is there a
way to provide this directive in VB6?

Any suggestions are appreciated, but please keep in mind that getting
authority to change the VB6 type library will be extremely difficult,
and possibly not an option at all...if its not, my only recourse will
be to go back to utilizing the poorly written-hard to work with (and
equally unmodifiable) VB6 classes...somet hing I'd really like to avoid
doing.
Some basic code:

public class IBFields : TBWApplication. IFields
{
[snip]
[System.Runtime. InteropServices .DispId(-4)]
public System.Collecti ons.IEnumerator GetEnumerator()
{
return new FieldEnumerator (m_fieldsArray) ;
}
}

internal class FieldEnumerator : System.Collecti ons.IEnumerator
{

private SortedList<stri ng, IBFieldm_fields ;
int position = -1;

public FieldEnumerator (SortedList<str ing, IBFieldlist)
{
m_fields = list;
}

public object Current
{
get
{
try
{
return
(TBWApplication .IField)m_field s.Values[position];
}
catch (IndexOutOfRang eException)
{
throw new InvalidOperatio nException();
}
}
}
public bool MoveNext()
{
position++;
return (position < m_fields.Count) ;
}

public void Reset()
{
position = -1;
}
}
Dec 13 '07 #1
1 1917
Well, I've found my own answer here. For anyone else that runs into
this, make sure the class being returned by your enumerator is set to
[ComVisible(true )].

It seems rather illogical that I'm able to pass a "Com invisible"
class into a COM class and work with it without flagging it as
visible, but that was the problem. I suppose its a case of the
enumerator returning an object with a type that VB6 was unable to
identify.

Also, as it turns out, our VB6 interface was already defined with
DispId(-4); its quite possible that this would have been required as
well.
Dec 17 '07 #2

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

Similar topics

7
4548
by: localhost | last post by:
What is the best and/or fastest way to enumerate attributes of an HTML control? In my page template, I have: <body id="myBody" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout" class="Body" runat="server"> And in my code-behind I want to look for the leftMargin
16
4223
by: Serdar Kalaycý | last post by:
Hi everybody, My problem seems a bit clichè but I could not work around. Well I read lots of MSDN papers and discussions, but my problem is a bit different from them. When I tried to run the project in debug mode (by hitting F5) it gives an error message "Error while trying to run project: Unable to start debugging on the web server.
1
4292
by: Alex Stevens | last post by:
Hi I have a class which inherits the sortedlist collection. I've overloaded the add method to allow the collection to store sqlParameters, with the key being the parameter name. When I try to enumerate using this code: *****Test Code Start *****
2
2446
by: eight02645999 | last post by:
hi, i am using python 2.1. Can i use the code below to simulate the enumerate() function in 2.3? If not, how to simulate in 2.1? thanks from __future__ import generators def enumerate(sequence): index = 0 for item in sequence: yield index, item
8
1420
by: Dustan | last post by:
Can I make enumerate(myObject) act differently? class A(object): def __getitem__(self, item): if item 0: return self.sequence elif item < 0: return self.sequence elif item == 0: raise IndexError, "Index 0 is not valid."
21
2331
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility than in the current enumerate, tightens up code in many cases, and seems that it would break no
12
2094
by: Danny Colligan | last post by:
In the following code snippet, I attempt to assign 10 to every index in the list a and fail because when I try to assign number to 10, number is a deep copy of the ith index (is this statement correct?). .... number = 10 .... So, I have to resort to using enumerate to assign to the list:
8
5030
by: eight02645999 | last post by:
hi say i want to enumerate lines of a file eg for n,l in enumerate(open("file")): # print next line ie is there a way to print out the next line from current line using the above?. Or do i have to do a readlines() first to get it into a list eg d = open("file").readlines()
0
2507
by: bcanter | last post by:
I found a file on the web that will allow you to enumerate groups but it was an .hta and the top level admins won't allow this. I need to give managers access to the groups so that when a new user is created they can tell me who they want their account to be modeled after. The html app works great as a .hta file but when it is saved as a web page it won't list the members of any groups. I get an error on line 182: "ActiveX component can't create...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
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 we have to send another system
2
3656
muto222
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.