JMK
JMK

Reputation: 28059

How can I subscribe to an instance object's events in VB6?

I have written a class library in C# that I am trying to use in VBA (VB6).

All is good creating my type library, using regasm to register the type library on a computer, creating an instance of the .Net object in VB6, accessing properties and calling methods.

But I am trying to subscribe to some events that an instance level object in my type library raises.

Below is an example of one of my event handlers (they are all generic event handlers) as declared in my C# class:

public event EventHandler<Events.NewCall> NewCall;

Events.NewCall is a class inhereting from EventArgs, and looks like this:

using System;

namespace MySolution.Events
{
    /// <summary>
    /// A call has been created ready for use
    /// </summary>
    public class NewCall : EventArgs
    {
        /// <summary>
        /// An automatic property
        /// </summary>
        public string AutoProperty { get; private set; }

        public NewCall(string rawData)
        {
            //Some logic here
            autoProperty = rawData;
        }
    }
}

I raise this event inside my class like so:

var checkNewCall = NewCall;
if (checkNewCall != null) { checkNewCall(this, new Events.NewCall("my raw data")); }

In C#, I can instantiate my phone object and subscribe to the event like so:

MySolution.Phone _phone = new MySolution.Phone();
_phone.NewCall += AnEventHandler;

The event handler looks like this:

void AnEventHandler(object sender, Splicecom.Events.NewCall e)
{
    //Do stuff with e
}

In VB6, I can instantiate my Phone object like so:

Dim thisPhone As New MySolution.Phone

Typing thisPhone. doesn't give me any intellisense but I am used to this, I don't get intellisense for the other methods inside my class, but I can still call them without compile/runtime errors.

As shown below, I can see the event I want to work with:

Event

But this is the intellisense for the library that is MySolution as opposed to the instance of the Phone class that is thisPhone.

In VB6, how do I subscribe to the NewCall event on the thisPhone instance of the MySolution.Phone object.

I have tried this:

Option Compare Database

Dim WithEvents thisPhone As MySolution.Phone

Private Sub Command0_Click()

    thisPhone = New MySolution.Phone
    thisPhone.NewCall = MyFunction()

End Sub

Private Sub MyFunction()

    'Do stuff

End Sub

When I click on Command0, I get this error:

Error

Upvotes: 2

Views: 992

Answers (2)

MarkJ
MarkJ

Reputation: 30408

VB6 event handlers are bound to the events at compile-time based entirely on the name of the routine. Try this:

Option Compare Database

Dim WithEvents thisPhone As MySolution.Phone

Private Sub Command0_Click()

    thisPhone = New MySolution.Phone

End Sub

' Name of this routine is <withEventsVariableName>_<eventName> '
Private Sub thisPhone_NewCall()

    ' Do stuff '

End Sub

Upvotes: 2

CodeTherapist
CodeTherapist

Reputation: 2806

A tip: If it is possible avoid interop. Often it is easier to write the VB6 code completely in C#.

Your C# must have a comvisible attribute (set to true) and a GUID. Build up an interface IPhone, VB6 needs this.

You VB6 code should look like this:

Dim thisPhone As MySolution.IPhone
Set thisPhone = new MySolution.Phone

Then you get access to your event handler if the interface exposes it.

Upvotes: 1

Related Questions