tags:

views:

411

answers:

2

Is it true that you cannot use COM Interop to expose COM properties? Everything has to be a method?

If this is not true, how do you do it?

+4  A: 

Not true.

I understand your question to be asking about COM calling or using a .NET class.

According to this page, you can expose methods, properties and fields of managed classes to COM consumers.

All managed types, methods, properties, fields, and events that you want to expose to COM must be public. Types must have a public default constructor, which is the only constructor that can be invoked through COM.

Does this work for you?:

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("A1209192-723B-4325-8599-FB39D9F202D9")]
public interface ITicklee
{
    [DispId(1)]
    void TickleMe();

    [DispId(2)]
    int Count{get;}
}


[Guid("45715A3B-CA95-49f7-9889-A0022B31EF8A")]
public class Elmo : ITicklee
{
    // default ctor
    public Elmo () {}

    private int _tickleCount;

    public int Count
    {
        get
        {
            return _tickleCount;
        }
    }

    public void TickleMe()
    {
        _tickleCount++;
    }
}

A VBScript test client might look like this:

Sub TestTickle()  

    WScript.echo("")

    WScript.echo("Instantiating an Elmo ...")
    dim elmo
    set elmo = WScript.CreateObject("Ionic.Tests.Com.Elmo")

    WScript.echo("Tickling...")

    For i = 1 to 5
      elmo.TickleMe()
    Next  

    WScript.echo("")
    c = elmo.Count

    WScript.echo("Tickle Count = " & c)

    ' one for the road'
    elmo.TickleMe()

End Sub

call TestTickle()     ' ahem '
Cheeso
A: 

If you mean that you cannot expose com object properties to a .NET application using interop, and the COM object was created in VB, then you are correct. VB uses property LET for native types and property SET for object types. If you try to set a property of your COM object from your .NET application, it tries to use SET. If the underlying property is a native VB type, this fails.

As far as I know, the only solution aside from using methods rather than properties is to manually manipulate the IDL.

Scott Ewers