VB.NET等价于VB6 Attribute Item.VB_UserMemId = 0 [英] VB.NET equivalent of VB6 Attribute Item.VB_UserMemId = 0
问题描述
在VB6中,我有一个类<$>中定义的属性c $ c> MyScalars 如下:
公共属性获取项目(vntIndexKey As Variant)As MyScalar
属性Item.VB_UserMemId = 0
设置项目= mCol(vntIndexKey)
...
结束属性
这似乎使得在Excel VBA中,我可以访问此属性而不指定它(如同默认属性):
Dim oOut As Object
Set oOut = MyScalars(Range(E10)。Value)
VB.NET中是否有等效的属性?我已经尝试了以下内容,但它在VBA中出现错误:
默认公共只读属性项(ByVal vntIndexKey As String)作为MyScalar
获取
如果mCol.ContainsKey(vntIndexKey)然后
项目= mCol.Item(vntIndexKey)
结束如果
...
结束属性
Govert的回复是正确的。
COM IDispatch
界面的所有成员都会自动标记一个 DISPID
值,
当从.NET转换为COM时。此自动进程的唯一例外是值0,该值为该类的默认成员保留。如果您没有设置 dispid
,那么移植到COM的.NET类的默认成员是 :: ToString
方法。
在你的例子中:
DispId(0)> _
公共ReadOnly属性项(ByVal vntIndexKey As String)As MyScalar
结束属性
I've upgraded a class from VB6 to VB.NET to be used in Excel via COM.
In VB6, I have a property defined in class MyScalars
as this:
Public Property Get Item(vntIndexKey As Variant) As MyScalar
Attribute Item.VB_UserMemId = 0
Set Item = mCol(vntIndexKey)
...
End Property
This seems to make it so that in Excel VBA, I can access this property without specifying it (so like a default property):
Dim oOut As Object
Set oOut = MyScalars(Range("E10").Value)
Is there an equivalent attribute in VB.NET that does this? I've tried the following but it gives an error in the VBA:
Default Public ReadOnly Property Item(ByVal vntIndexKey As String) As MyScalar
Get
If mCol.ContainsKey(vntIndexKey) Then
Item = mCol.Item(vntIndexKey)
End If
...
End Property
The reply of Govert is the right one.
All the members of a COM IDispatch
interface are automatically marked with a DISPID
value,
when converted from .NET to COM. The only exception for this automatic process is the value 0, which is reserved for the default member of the class. if you do not set the dispid
, the default member of .NET classes when is ported to COM is the ::ToString
method.
In your example:
< DispId(0) > _
Public ReadOnly Property Item(ByVal vntIndexKey As String) As MyScalar
End Property
这篇关于VB.NET等价于VB6 Attribute Item.VB_UserMemId = 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!