I have an array in C# which has to be initialized in VB. While initializing I am getting an error "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic."
C#.Net Code:
public class InventoryMasterSearchSettings
{
public string[] PrintLabelsIDetail { get; set; }
}
VB.Net Code:
Public Property PrintLabelsIDetail() As String()
Get
PrintLabelsIDetail = mInventoryMasterSearchSettings.PrintLabelsIDetail
End Get
Set(value As String())
mInventoryMasterSearchSettings.PrintLabelsIDetail = value
End Set
End Property
VB6
Public Sub ShowPrintLabels(invmast() As String, bShowAvailableInventoryOnlyPar As Boolean, Optional fCalledFromScreen As Form, Optional sVendorIDPar As String, Optional sUPCCodePar As String, Optional sInventoryDescriptionPar As String)
Dim oInventoryMasterSearchSettings As New Shared_Interop.InventoryMasterSearchSettings
oInventoryMasterSearchSettings.PrintLabelsIDetail = invmast 'error on PrintLabelsIDetail
End Sub
Are you sure the type of mInventoryMasterSearchSettings.PrintLabelsIDetail is a standard array? (not a collection, or a arraylist type)
You could try two things.
First - use a compiler directive to force the array as a COM "safe" array.
Public Property PrintLabelsIDetail() As <MarshalAs(UnmanagedType.SafeArray)> String()
Get
PrintLabelsIDetail = mInventoryMasterSearchSettings.PrintLabelsIDetail
End Get
Set(value As String())
mInventoryMasterSearchSettings.PrintLabelsIDetail = value
End Set
End Property
So, above may well help marshal the array() of strings to somthing more compatiable with the COM side from a consuming point of view.
The other idea? You could do a cast of the type before you return it.
So, say like this:
Public Property PrintLabelsIDetail() As <MarshalAs(UnmanagedType.SafeArray)> String()
Get
PrintLabelsIDetail = DirectCast(mInventoryMasterSearchSettings.PrintLabelsIDetail, String())
End Get
Set(value As String())
mInventoryMasterSearchSettings.PrintLabelsIDetail = value
End Set
End Property
Also, if the array of string() type is not initialized, then you might want to ensure that has occurred before you return that array.
As a general rule, you CAN pass a array from the COM side to .net, but you USEALLY MUST send that array as byref and NOT byval.
However, you are going the the other way. .net --> COM side. A plane jane array of string type usually can go to COM side, and even so without the above marshalas compiler directive.
I would try one or the other idea, or in fact as my 2nd example does, try adding both a marshal compiler directive, and a cast.
My spider sense suggests that the data type of PrintLabelsIDetail is a collection, iList, or arrarylist when it need to be a array() of string type. So, check the data type of PrintLabelsIDetail - I not tested but I don't think COM will even like a arrayList in place of Array. And you would do well to initialize the array before sending it across the COM bridge
I was having a coffee - the directcast might get you a COM/ActiveX compatible array, but I "think" it may well break the connection. (the VB6 side would be modifying a copy - it may well not persist in the COM object members) - so try the compiler directive without the directcast suggestion
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.