CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003
    Posts
    23

    Angry 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?

  2. #2
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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 .

  3. #3
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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.

  4. #4
    Join Date
    Oct 2003
    Posts
    23

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured