1
votes

I'm writing a C# COM object that will be used by a VB 6 program. That shouldn't be much of a problem, however, the VB6 call to the COM object passes in a VB Control (in this case a TextBox). The program expects the COM object to alter the Text property of the control. Unfortunately, I have to stick to this interface as I'm dealing with someone elses legacy code.

How can I set the property for the passed in TextBox? Do I simply create an interface with a Text property and cast input to that interface? Is this even possible?

Please let me know if I need to clarify. Note: I intended to leave out the COM declarations in the following code.

// C# COM Object Interface
public interface IObj
{
    // This function must receive the argument of type object.
    void Test(object input);
}

// C# COM Object Implementation
public class Obj : IOjb
{
    // A VB6 TextBox is passed into here,
    // expecting a change to the Text property.
    public void Test(object input)
    {
        // INSERT NECESSARY CODE HERE

        input.Text = "arbitrary string";
    }
}


// VB 6
comObject.Test (txtBox)
2
How does the current COM object do it? You need to duplicate the signature of all of its methods.John Saunders
The current COM object is a VB6 COM object that takes in the argument of type object. So this should be a duplicate of the signature.Perishable Dave

2 Answers

3
votes

You should put code for updating the text box and any other UI specific code in to a VB6 COM object, and hand off any non-UI code to a C# class, then you will not have to deal with a VB6 UI control in C#.

2
votes

Are you using .Net 4.0?

You could try using the new dynamic language features:

public void Test(object input)
{
    dynamic textBox = input;
    //Assuming there is a property named Text at runtime
    textBox.Text = "arbitrary string";
}

This should do the equivalent of late binding in COM.

You might also try using dynamic in your method declaration.