7

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
Community
  • 1
  • 1
ryrich
  • 2,164
  • 16
  • 24
  • What error do you get in the VBA? You might try a property with a 'Set' as well, that's not ReadOnly. – Govert Jun 12 '14 at 21:20
  • @Govert error 450: wrong number of arguments or invalid property assignment. If I do `MyScalars.Item(Range("E10").Value)` it works fine, I'm just curious if defaulting the property is possible. – ryrich Jun 12 '14 at 21:22
  • Also specifying a `Set` as well gives same result. – ryrich Jun 12 '14 at 21:30
  • 1
    This seems relevant: http://stackoverflow.com/questions/6452391/exposing-indexer-like-properties-to-com . Mark your property as – Govert Jun 12 '14 at 21:34
  • @Govert You should post a quick summary/example of using DispIdAttribute as an answer, as I believe that is the answer. – Joshua Honig Jun 13 '14 at 00:28
  • Isn't [**THIS**](http://msdn.microsoft.com/en-us/library/se6z814t.aspx) what you are looking for? How about [**THIS ONE**](http://stackoverflow.com/questions/293215/default-properties-in-vb-net)? –  Jun 13 '14 at 07:57
  • Did the DispId(0) work? – Govert Jun 13 '14 at 11:31
  • @Govert sorry for delay. No, still the same error. – ryrich Jun 13 '14 at 14:41
  • That's really weird... I swear that the way you write it is correct. Can you post the full VB.NET class and the way you actually call it from Excel maybe? – yu_ominae Jul 07 '14 at 08:04

1 Answers1

2

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 
talegna
  • 2,407
  • 2
  • 19
  • 22
laertes
  • 70
  • 7