3
votes

I've researched this question quite a bit, and while I've found a lot about C# and parameterized properties (using an indexer is the only way), I haven't found an actual answer to my question.

First, what I'm trying to do:

I have an existing COM DLL written in VB6 and I'm trying to create a C# DLL that uses a similar interface. I say similar because the VB6 DLL is only used with late binding, so it doesn't have to have the same GUIDs for the calls (that is, it doesn't have to be "binary compatible"). This VB6 COM DLL uses parameterized properties in a few places, which I know aren't supported by C#.

When using a VB6 COM DLL with parameterized properties, the reference in C# will access them as methods in the form "get_PropName" and "set_PropName". However, I'm going in the opposite direction: I'm not trying to access the VB6 DLL in C#, I'm trying to make a C# COM DLL compatible with a VB6 DLL.

So, the question is: How do I make getter and setter methods in a C# COM DLL that appear as a single parameterized property when used by VB6?

For example, say the VB6 property is defined as follows:

Public Property Get MyProperty(Param1 As String, Param2 as String) As String
End Property

Public Property Let MyProperty(Param1 As String, Param2 As String, NewValue As String)
End Property

The equivalent in C# would be something like this:

public string get_MyProperty(string Param1, string Param2)
{
}

public void set_MyProperty(string Param1, string Param2, ref string NewValue)
{
}

So, how would I make those C# methods look like (and function like) a single parameterized property when used by VB6?

I tried creating two methods, one called "set_PropName" and the other "get_PropName", hoping it would figure out that they're supposed to be a single parameterized property when used by VB6, but that didn't work; they appeared as two different method calls from VB6.

I thought maybe some attributes needed to be applied to them in C# so that they'd be seen as a single parameterized property in COM and VB6, but I couldn't find any that seemed appropriate.

I also tried overloading the methods, removing "get_" and "set_", hoping it would see them as a single property, but that didn't work either. That one generated this error in VB6: "Property let procedure not defined and property get procedure did not return an object".

I'm almost positive that there should be a way of doing this, but I just can't seem to find it. Does anyone know how to do this?

Update:

I took Ben's advice and added an accessor class to see if this could solve my problem. However, now I'm running into another issue...

First, here's the COM interface I'm using:

[ComVisible(true),
 Guid("94EC4909-5C60-4DF8-99AD-FEBC9208CE76"),
 InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISystem
{
    object get_RefInfo(string PropertyName, int index = 0, int subindex = 0);
    void set_RefInfo(string PropertyName, int index = 0, int subindex = 0, object theValue);

    RefInfoAccessor RefInfo { get; }

}

Here's the accessor class:

public class RefInfoAccessor
{
    readonly ISystem mySys;
    public RefInfoAccessor(ISystem sys)
    {
        this.mySys = sys;
    }

    public object this[string PropertyName, int index = 0, int subindex = 0]
    {
        get
        {
            return mySys.get_RefInfo(PropertyName, index, subindex);
        }
        set
        {
            mySys.set_RefInfo(PropertyName, index, subindex, value);
        }
    }
}

Here's the implementation:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid(MySystem.ClassId)]
[ProgId("MyApp.System")]
public class MySystem : ISystem
{
    internal const string ClassId = "60A84737-8E96-4DF3-A052-7CEB855EBEC8";

    public MySystem()
    {
        _RefInfo = new RefInfoAccessor(this);
    }


    public object get_RefInfo(string PropertyName, int index = 0, int subindex = 0)
    {
        // External code does the actual work
        return "Test";
    }
    public void set_RefInfo(string PropertyName, int index = 0, int subindex = 0, object theValue)
    {
        // External code does the actual work
    }

    private RefInfoAccessor _RefInfo;
    public RefInfoAccessor RefInfo
    {
        get
        {
            return _RefInfo;
        }
    }

}

Here's what I'm doing to test this in VB6, but I get an error:

Set sys = CreateObject("MyApp.System")

' The following statement gets this error:
' "Wrong number of arguments or invalid property assignment"
s = sys.RefInfo("MyTestProperty", 0, 0)

However, this works:

Set sys = CreateObject("MyApp.System")

Set obj = sys.RefInfo
s = obj("MyTestProperty", 0, 0)

It appears that it's trying to use the parameters on the property itself and getting an error because the property has no parameters. If I reference the RefInfo property in its own object variable, then it applies the indexer properties correctly.

Any ideas on how to arrange this so that it knows to apply the parameters to the accessor's indexer, rather than attempting to apply it to the property?

Also, how do I do a +1? This is my first question on StackOverflow :-)

Update #2:

Just to see how it would work, I also tried the default value approach. Here's how the accessor looks now:

public class RefInfoAccessor
{
    readonly ISystem mySys;
    private int _index;
    private int _subindex;
    private string _propertyName;
    public RefInfoAccessor(ISystem sys, string propertyName, int index, int subindex)
    {
        this.mySys = sys;
        this._index = index;
        this._subindex = subindex;
        this._propertyName = propertyName;
    }
    [DispId(0)]
    public object Value
    {
        get
        {
            return mySys.get_RefInfo(_propertyName, _index, _subindex);
        }
        set
        {
            mySys.set_RefInfo(_propertyName, _index, _subindex, value);
        }
    }
}

This works great for a "get". However, when I try setting the value, .NET flips out with the following error:

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'blahblah.exe'.

Additional information: The runtime has encountered a fatal error. The address of the error was at 0x734a60f4, on thread 0x1694. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

I'm assuming the problem is that .NET tried setting the value to the method, rather to the default property of the returned object, or something similar. If I add ".Value" to the set line, it works fine.

Update #3: Success!

I finally got this working. There's a few things to look for, however.

First, the default value of the accessor must return a scaler, not an object, like so:

public class RefInfoAccessor
{
    readonly ISystem mySys;
    private int _index;
    private int _subindex;
    private string _propertyName;
    public RefInfoAccessor(ISystem sys, string propertyName, int index, int subindex)
    {
        this.mySys = sys;
        this._index = index;
        this._subindex = subindex;
        this._propertyName = propertyName;
    }
    [DispId(0)]
    public string Value  // <== Can't be "object"
    {
        get
        {
            return mySys.get_RefInfo(_propertyName, _index, _subindex).ToString();
        }
        set
        {
            mySys.set_RefInfo(_propertyName, _index, _subindex, value);
        }
    }
}

Second, when using the accessor, you need to make the return type an object:

    public object RefInfo(string PropertyName, int index = 0, int subindex = 0)
    {
        return new RefInfoAccessor(this,PropertyName,index,subindex);
    }

This will make C# happy, since the default value is a COM thing (dispid 0) and not a C# thing, so C# expects a RefInfoAccessor to be returned, not a string. Since RefInfoAccessor can be coerced into an object, no compiler error.

When used in VB6, the following will now all work:

s = sys.RefInfo("MyProperty", 0, 0)
Debug.Print s

sys.RefInfo("MyProperty", 0, 0) = "Test"  ' This now works!
s = sys.RefInfo("MyProperty", 0)
Debug.Print s

Many thanks to Ben for his help on this!

2

2 Answers

2
votes

C# can do indexed properties, but these must be implemented using a helper class which has an indexer. This method will work with early-bound VB but not with late-bound VB:

using System;


class MyClass {
    protected string get_MyProperty(string Param1, string Param2)
    {
        return "foo: " + Param1 + "; bar: " + Param2;
    }

    protected void set_MyProperty(string Param1, string Param2, string NewValue)
    {
        // nop
    }
    // Helper class
    public class MyPropertyAccessor {
        readonly MyClass myclass;
        internal MyPropertyAccessor(MyClass m){
            myclass = m;
        }
        public string this [string param1, string param2]{
             get {
                 return myclass.get_MyProperty(param1, param2);
             }
             set {
                 myclass.set_MyProperty(param1, param2, value);
             }
        }
    }
    public readonly MyPropertyAccessor MyProperty;
    public MyClass(){
        MyProperty = new MyPropertyAccessor(this);
    }
}


public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");

        var mc = new MyClass();
        Console.WriteLine(mc.MyProperty["a", "b"]);
    }

}

There is a tutorial here:

Late-bound VB Workaround

This is a workaround which takes advantage of two facts about VB. One is that in the array index operator is the same as the function call operator - round brackets (parens). The other is that VB will allow us to omit the name of the default property.

Read-only Properties

If the property is get-only, you don't need to bother with this. Just use a function, and this will behave the same as array access for late-bound code.

Read-Write properties

Using the two facts above, we can see that these are equivalent in VB

// VB Syntax: PropName could either be an indexed property or a function
varName = obj.PropName(index1).Value
obj.PropName(index1).Value = varName

// But if Value is the default property of obj.PropName(index1) 
// this is equivalent:
varName = obj.PropName(index1)
obj.PropName(index1) = varName

This means that instead of doing the this:

//Property => Object with Indexer
// C# syntax
obj.PropName[index1];

We can do this:

// C# syntax
obj.PropName(index1).Value

So here is the example code, with a single parameter.

class HasIndexedProperty {
    protected string get_PropertyName(int index1){
        // replace with your own implementation
        return string.Format("PropertyName: {0}", index1);
    }
    protected void set_PropertyName(int index1, string v){
        // this is an example - put your implementation here
    }
    // This line provides the indexed property name as a function.
    public string PropertyName(int index1){
        return new HasIndexedProperty_PropertyName(this, index1);
    }
    public class HasIndexedProperty_PropertyName{
        protected HasIndexedProperty _owner;
        protected int _index1;
        internal HasIndexedProperty_PropertyName(
            HasIndexedProperty owner, int index1){
            _owner = owner; _index1 = index1;
        }
        // This line makes the property Value the default
        [DispId(0)]
        public string Value{
            get {
                return _owner.get_PropertyName(_index1);
            }
            set {
                _owner.set_PropertyName(_index1, value);
            }
        }
    }
}

Limitations

The limitation is that to work, this depends on the call being made in a context where the result is coerced to a non-object type. For example

varName = obj.PropName(99)

Since the Set keyword was not used, VB knows that it must get the default property for use here.

Again, when passing to a function which takes for example a string, this will work. Internally VariantChangeType will be called to convert the object to the correct type, which if coercing to a non-object will access the default property.

The problem may occur when passing directly as a parameter to a function which takes a Variant as an argument. In this case the accessor object will be passed. As soon as the object is used in a non-object context (e.g. an assignment or conversion to string) the default property will be fetched. However this will be the value at the time it is converted, not the time it was originally accessed. This may or may not be an issue.

This issue can be worked around however by having the accessor object cache the value it returns to ensure it is the value as at the time the accessor was created.

2
votes

This feature you seek is usually called "indexed properties". The flavor that VB6 uses is the flavor supported by COM interfaces.

This IDL fragment is similar to what VB6 would generate, and shows what's going on under the hood:

interface ISomething : IDispatch {
    [id(0x68030001), propget]
    HRESULT IndexedProp(
                    [in, out] BSTR* a,      // Index 1
                    [in, out] BSTR* b,      // Index 2
                    [out, retval] BSTR* );
    [id(0x68030001), propput]
    HRESULT IndexedProp(
                    [in, out] BSTR* a,      // Index 1
                    [in, out] BSTR* b,      // Index 2
                    [in, out] BSTR* );


    [id(0x68030000), propget]
    HRESULT PlainProp(
                    [out, retval] BSTR* );

    [id(0x68030000), propput]
    HRESULT PlainProp(
                    [in, out] BSTR* );
};

IndexedProp is a String property that takes two String parameters as indices. Contrast with PlainProp, which is of course a non-indexed conventional property.

Unfortunately, C# has very limited support for COM-style indexed properties.

C# 4.0 supports consuming COM objects (written elsewhere) that implement a COM interface with indexed properties. This was added to improve interoperability with COM Automation servers like Excel. However, it doesn't support declaring such an interface, or creating an object that implements such a COM interface even if legally declared elsewhere.

Ben's answer tells you how to create indexed properties in C# - or at least something that results in an equivalent syntax in C# code. If you just want the syntax flavor while writing C# code, that works great. But of course it's not a COM-style indexed property.

This is a limitation of the C# language, not the .NET platform. VB.NET does support COM indexed properties, because they had the mandate to replace VB6 and therefore needed to go the extra mile.

If you really want COM indexed properties, you could consider writing the COM version of your object in VB.NET, and have that object forward calls to your C# implementation. It sounds like a lot of work to me. Or port all your code to VB.NET. It really depends on how badly do you want it.

References

But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0.