Click to See Complete Forum and Search --> : Why event doesnt fire in .NET object from VBS?


Gatwick
November 26th, 2004, 11:09 AM
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?

Sahir
November 27th, 2004, 02:55 AM
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 .

Sahir
November 27th, 2004, 02:59 AM
This article

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconhandlingobjectsevents.asp

discusses handling object events from Visual Basic.
The same concept can be applied with minor modifications to VBScript.

Gatwick
November 27th, 2004, 04:01 AM
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.