Rexxowski
10/12/2007 3:05:00 PM
martin.wegger.jentoft@gmail.com napsal(a):
> Hi,
>
> I have a .net-dll that I'm using from VB6. And when I do a for each on
> my Collection class (Reports), i'm getting a "Type
>
> mismatch" error.
>
> If I change the InterfaceType attribute to InterfaceIsDual or
> InterfaceIsIDispatch, it works fine.
>
> Can anyone tell what I shoud do to get this working, without changing
> the InterfaceType attribute?
>
> Thanks in advance.
>
> Martin
>
>
> Code example:
>
> <C# .net>
> using System.Collections;
> using System.Collections.Generic;
> using System.Runtime.InteropServices;
>
> [ComVisible(true)]
> [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
> public interface IReports
> {
> IEnumerator GetEnumerator();
> }
>
> [ComVisible(true)]
> [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
> public interface IReport
> {
> string Name { get; }
> }
>
> [ComVisible(true)]
> [ComDefaultInterface(typeof(IReports))]
> public class Reports : IReports
> {
> private List<Report> _reports = new List<Report>();
>
> public Reports()
> {
> _reports.Add(new Report("Report01"));
> _reports.Add(new Report("Report02"));
> }
>
> IEnumerator IReports.GetEnumerator()
> {
> return _reports.GetEnumerator();
> }
>
> }
>
> [ComVisible(true)]
> [ComDefaultInterface(typeof(IReport))]
> public class Report : IReport
> {
> private string _name;
> public Report(string name)
> {
> _name = name;
> }
> string IReport.Name
> {
> get { return _name; }
> }
> }
> </C# .net>
>
>
>
> <VB6>
> Dim rpts As Reports
> Set rpts = New Reports
>
> Dim r As Report
> For Each r In rpts
> Debug.Print r.Name
> Next r
> </VB6>
>
Is the problem already in the For Each loop or in reading r.Name
property? The For Each loop needs IEnumVARIANT, there shouldn't be
the problem. But for reading r.Name you need IDispatch interface,
VB always needs an interface derived from IDispatch.