I've developed a VB.NET library (partially developed on C# as well) that heavily depends on inheriting from an abstract generic base class, and I'm trying to figure out the best practice for this. I Sadly have to do it using framework 3.5.
Public MustInherit Class MyBaseClass(Of T)
Public Whatever As T
End Class
Public Class MyDerivedClass
Inherits MyBaseClass(Of String)
Private _myProperty As String
Public Property MyProperty As String
Get
Return _myProperty
End Get
Set(value As String)
_myProperty = value
End Set
End Property
End Class
I attach the .tlb file as a reference in VBA (using Excel), and I run the following code:
Dim m As New VBtoVBA.MyDerivedClass
m.MyProperty = "foo"
And I get the error "Run-time error 430: Class does not support Automation or does not support expected interface".
On the other hand, I change the first lines to:
Public MustInherit Class MyBaseClass
Public Whatever As String
End Class
Public Class MyDerivedClass
Inherits MyBaseClass
The VBA script works. Hence I assume the issue is with generics (as documented in other sources as well). Dropping the generic feature of my library is not possible, though. The "best" workaround I can think of is to write a third class that includes MyDerivedClass as a field, and works as a non-generic interface to it:
Public Class MyDerivedClassString
Private _innerObj As New MyDerivedClass
Public Property MyProperty As String
Get
Return _innerObj.MyProperty
End Get
Set(value As String)
_innerObj.MyProperty = value
End Set
End Property
Public Property Whatever As String
Get
Return _innerObj.Whatever
End Get
Set(value As String)
_innerObj.Whatever = value
End Set
End Property
End Class
This way I can work with it pretty much as I'd like to in VBA:
m.Whatever = "wha"
MsgBox (m.Whatever)
By the way I think that there might be another (better) way to achieve the same result, and i really hope so since m library is made up of dozens of classes.
Many thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…