简体   繁体   中英

Passing array from VB6 to C#.net

How to pass a VB6 array to C# through COM Interop?

I would like to call a method in VB6 that has an array as a parameter. Unfortunately VB complains about the inappropriate type. My C# code:

public void CreateMultipleNewEsnBelegung(ref QpEnrPos_COM[] Input);

public void CreateMultipleNewEsnBelegung(ref QpEnrPos_COM[] Input)
{
    List<Domain.Entities.QpEnrPos> qpEnrPos = new List<Domain.Entities.QpEnrPos>();
    foreach (var item in Input)
    {
        qpEnrPos.Add(ComConverter.ConvertQpEnrPosComToQpEnrPos(item));
    }
   Methods.CreateMultipleNewESNPos(qpEnrPos);
}

My VB code:

Dim qpenrPos(1) As CrossCutting_Application_ESN.QpEnrPos_COM

Set qpenrPos(0) = secondimportModel
Set qpenrPos(1) = firstimportModel

obj.CreateMultipleNewEsnBelegung (qpenrPos())

I know I need to do something with MarshalAs . However, I can't find the right way to do it.

I have managed to get it to work. The trick was to specify the array as an object array. But the content of the array can be filled with any data type.

public void CreateMultipleNewEsnBelegung(ref object[] Input);

public void CreateMultipleNewEsnBelegung(ref object[] Input)
{
    List<Domain.Entities.QpEnrPos> qpEnrPos = new List<Domain.Entities.QpEnrPos>();
    foreach (var item in Input)
    {
        qpEnrPos.Add(ComConverter.ConvertQpEnrPosComToQpEnrPos(item));
    }
   Methods.CreateMultipleNewESNPos(qpEnrPos);
}

Vb Code:

Dim qpenrPos(1) As Variant

Set qpenrPos(0) = secondimportModel
Set qpenrPos(1) = firstimportModel

obj.CreateMultipleNewEsnBelegung (qpenrPos)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM