[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

ComInterfaceType.InterfaceIsIUnknown and For Each and the COM side

martin.wegger.jentoft

10/12/2007 2:08:00 PM

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>

2 Answers

Rexxowski

10/12/2007 3:05:00 PM

0

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.

martin.wegger.jentoft

10/16/2007 9:10:00 AM

0

Hi Rexxowski!

Thanks for the response.

Yes, the problem is already in the for loop. I get the error on the
line "For Each r In rpts".

I have no problem using the Name property when i use
InterfaceIsIUnknown, it's only the for each.

The reason I use
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)], is that I only
want to enable early binding.

Martin