Why event doesnt fire in .NET object from VBS?
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace DEMONS
{
[ComVisible(false)]
public delegate void TestEventHandler();
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ControlEvents
{
//[DispIdAttribute(0x60020000)]
void OnBeforeStartTest();
}
[
ClassInterface(ClassInterfaceType.AutoDispatch),
ComSourceInterfaces(typeof(ControlEvents))
]
public class Demo
{
public event TestEventHandler OnBeforeStartTest;
public void TEST()
{
Console.WriteLine("TEST()");
if (OnBeforeStartTest != null)
OnBeforeStartTest();
}
}
}
VBS:
sub OnTest2()
msgbox "VBSCRIPT OnTest"
end sub
sub obj_OnBeforeStartMigration()
msgbox "VBSCRIPT OnTest"
end sub
set obj = createobject("DEMONS.Demo")
set obj.OnBeforeStartTest = GetRef("OnTest2")
obj.TEST
--------------------------------
obj.TEST works fine (if to comment upper string). But when I leave it uncommented i receive
Microsoft VBScript runtime error: Object doesn't support this property or method: 'obj.OnBeforeStartTest'
Whats wrong to my code - VBS and .NET?
Re: Why event doesnt fire in .NET object from VBS?
It's the VB Script.
You need to declare the object like this
Dim WithEvents myObj As SomeLib.SomeObj
and then add an event handler for this. The convention is object name underscore event name, in your case it could take the form
Private Sub obj_OnBeforeStartTest()
' do something
End Sub
Pls note : The variable declared WithEvents cannot be a generic object . The type must be known before hand .
Re: Why event doesnt fire in .NET object from VBS?
This article
http://msdn.microsoft.com/library/de...ectsevents.asp
discusses handling object events from Visual Basic.
The same concept can be applied with minor modifications to VBScript.
Re: Why event doesnt fire in .NET object from VBS?
Quote:
Originally Posted by Sahir
It's the VB Script.
You need to declare the object like this
Dim WithEvents myObj As SomeLib.SomeObj
and then add an event handler for this. The convention is object name underscore event name, in your case it could take the form
Private Sub obj_OnBeforeStartTest()
' do something
End Sub
Pls note : The variable declared WithEvents cannot be a generic object . The type must be known before hand .
Sorry but VBScript doesn understand
Dim WithEvents myObj As DEMONS.Demo
But I need VBScript solution. I need working solution for later binding.