VB6 to VB.Net COM Interface Help
-
Why do i get the error - "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic", when I try to pass a String array from vb6 to a class:Property string array in vb.net? This is the vb6 code:
Option Explicit
Private Sub btnTest_Click()
Dim strTest(4) As String
strTest(1) = "abc"
strTest(2) = "def"
strTest(3) = "ghi"
strTest(4) = "jkl"On Error GoTo err1 Dim clsTest As ComTest.clsValues Set clsTest = New ComTest.clsValues clsTest.SingleString = "this is my string" clsTest.stringArray = strTest ' <<
This is the vb.net class
Imports System.Runtime.InteropServices
_
Public Class clsValues#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "095dc64d-141f-46f9-9af2-1c5e633b459b"
Public Const InterfaceId As String = "86ec1f14-1ef4-446d-9172-b92cf260cd16"
Public Const EventsId As String = "1605e412-8b11-4ee0-a34c-9ec04464737f"
#End Region' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Private mSingleString As String Public Property SingleString() As String Get Return mSingleString End Get Set(ByVal value As String) mSingleString = value End Set End Property Private mStringArray() As String Public Property StringArray() As String() Get Return mStringArray End Get Set(
-
Why do i get the error - "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic", when I try to pass a String array from vb6 to a class:Property string array in vb.net? This is the vb6 code:
Option Explicit
Private Sub btnTest_Click()
Dim strTest(4) As String
strTest(1) = "abc"
strTest(2) = "def"
strTest(3) = "ghi"
strTest(4) = "jkl"On Error GoTo err1 Dim clsTest As ComTest.clsValues Set clsTest = New ComTest.clsValues clsTest.SingleString = "this is my string" clsTest.stringArray = strTest ' <<
This is the vb.net class
Imports System.Runtime.InteropServices
_
Public Class clsValues#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "095dc64d-141f-46f9-9af2-1c5e633b459b"
Public Const InterfaceId As String = "86ec1f14-1ef4-446d-9172-b92cf260cd16"
Public Const EventsId As String = "1605e412-8b11-4ee0-a34c-9ec04464737f"
#End Region' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Private mSingleString As String Public Property SingleString() As String Get Return mSingleString End Get Set(ByVal value As String) mSingleString = value End Set End Property Private mStringArray() As String Public Property StringArray() As String() Get Return mStringArray End Get Set(
You don't seem to be setting the
StringArray
property anywhere. You also have a line which doesn't look like it should compile:clsTest strTest
Is this a typo in your question?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You don't seem to be setting the
StringArray
property anywhere. You also have a line which doesn't look like it should compile:clsTest strTest
Is this a typo in your question?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Why do i get the error - "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic", when I try to pass a String array from vb6 to a class:Property string array in vb.net? This is the vb6 code:
Option Explicit
Private Sub btnTest_Click()
Dim strTest(4) As String
strTest(1) = "abc"
strTest(2) = "def"
strTest(3) = "ghi"
strTest(4) = "jkl"On Error GoTo err1 Dim clsTest As ComTest.clsValues Set clsTest = New ComTest.clsValues clsTest.SingleString = "this is my string" clsTest.stringArray = strTest ' <<
This is the vb.net class
Imports System.Runtime.InteropServices
_
Public Class clsValues#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "095dc64d-141f-46f9-9af2-1c5e633b459b"
Public Const InterfaceId As String = "86ec1f14-1ef4-446d-9172-b92cf260cd16"
Public Const EventsId As String = "1605e412-8b11-4ee0-a34c-9ec04464737f"
#End Region' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Private mSingleString As String Public Property SingleString() As String Get Return mSingleString End Get Set(ByVal value As String) mSingleString = value End Set End Property Private mStringArray() As String Public Property StringArray() As String() Get Return mStringArray End Get Set(
I suspect the problem is that the array needs to be passed
ByRef
, which isn't supported in property setters. Try replacing the property setter with a method:Public Property StringArray() As String()
Get
Return mStringArray
End Get
End PropertyPublic Sub SetStringArray( ByRef value As String())
mStringArray = value
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I suspect the problem is that the array needs to be passed
ByRef
, which isn't supported in property setters. Try replacing the property setter with a method:Public Property StringArray() As String()
Get
Return mStringArray
End Get
End PropertyPublic Sub SetStringArray( ByRef value As String())
mStringArray = value
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I read setting the parameter ByRef and not ByVal would work but wasn't sure how to set it up. A little tweaking and the vb.net code looks like this:
_
Public Class clsValues#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "095dc64d-141f-46f9-9af2-1c5e633b459b"
Public Const InterfaceId As String = "86ec1f14-1ef4-446d-9172-b92cf260cd16"
Public Const EventsId As String = "1605e412-8b11-4ee0-a34c-9ec04464737f"
#End Region' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Private mSingleString As String Public Property SingleString() As String Get Return mSingleString End Get Set(ByVal value As String) mSingleString = value End Set End Property Private mStringArray As String() Public Function StringArray() As String() Return mStringArray.ToArray End Function Public Function StringArray(i As Short) As String Return mStringArray(i) End Function Public Sub SetStringArray( ByRef value As String()) mStringArray = value End Sub
End Class
The final vb6 code looks like this:
Option Explicit
Private Sub btnTest_Click()
Dim x As Integer
Dim strTest(4) As String
strTest(1) = "abc"
strTest(2) = "def"
strTest(3) = "ghi"
strTest(4) = "jkl"On Error GoTo err1 Dim clsTest As comtest.clsValues Set clsTest = New comtest.clsValues clsTest.SingleString = "this is my string" clsTest.SetStringArray strTest MsgBox "Single string = " & clsTest.SingleString, vbInformation, "Test" For x = 1 To UBound(clsTest.StringArray) 'clsTest.UboundStringArray MsgBox "Multi string " & x & " = " & clsTest.StringArray\_2(x), vbInformation, "Test" Next x Exit Sub
err1:
MsgBox "Error: " & Err.Number & vbCrLf & Err.Descrip