This project has moved. For the latest updates, please go here.

vbs events

Aug 29, 2014 at 7:55 PM
hi,
how can i use events in vbs?
Coordinator
Aug 29, 2014 at 9:07 PM
Hi furesoft,

Are you asking about handling .NET events in VBScript code running in ClearScript?

Thanks!
Aug 30, 2014 at 9:08 AM
yes
Coordinator
Aug 30, 2014 at 2:22 PM
Hi again,

Suppose you have a .NET class with an event:
public class Foo {
    public event EventHandler SomethingHappened;
    public void MakeSomethingHappen() {
        if (SomethingHappened != null)
            SomethingHappened(this, new EventArgs());
    }
}
Here's how you might handle the event in VBScript code:
engine.AddHostObject("foo", new Foo());
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    sub OnSomethingHappened(sender, args)
        Console.WriteLine(""Something just happened!"")
    end sub
    set connection = foo.SomethingHappened.connect(GetRef(""OnSomethingHappened""))
    foo.MakeSomethingHappen()
    connection.disconnect()
");
Good luck!