473,378 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

HOWTO: Raise C# event and handle in VBScript

The event SomeEvent() is raised and handled in VB6 fine. Using this exact
same code and using VBScript with the CreateObject call causes an error in
the c# code.

When using VBScript, I get a 'Object reference not set to an instance of an
object.' on the line SomeEvent(). I am positive the m_nSomeProperty =
nValue; line executes.

How can I get this working from VBScript?
Here's the code:
using System;

using System.Runtime.InteropServices;

namespace ComInterop
{
public delegate void SomeEventHandler();

[ Guid("F8E81A5A-BB37-49CC-9681-7D4FE52936B3"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h) ]
public interface ICComInteropEvents
{
[DispId(1)] void SomeEvent();
}

[ Guid("97C8501B-35DE-48CA-BECC-B980D08226F4"),
InterfaceType(ComInterfaceType.InterfaceIsDual) ]
public interface ICComInterop
{
[DispId(2)] void SomeMethod( int nValue );
[DispId(3)] int SomeProperty{ get; set; }
}

[ Guid("10EBC493-D2FD-4723-9F8B-F593586ED9B7"),
ProgId("ComInterop.CComInterop"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ICComInteropEvents)) ]
public class CComInterop : ICComInterop
{
public CComInterop() : base()
{
}

private int m_nSomeProperty = 0;
public event SomeEventHandler SomeEvent;

public void SomeMethod( int nValue )
{
m_nSomeProperty = nValue;
///////////////////////////////////////////////////////////////////////////////////////////
//
// object reference not set to an object error occurs here
//
//////////////////////////////////////////////////////////////////////////////////////////
SomeEvent();
}

public int SomeProperty
{
get{ return m_nSomeProperty; }
set{ m_nSomeProperty = value; }
}
}
}

And the VBScript code:

Option Explicit
' ================================================== ========================
' Implementation
'
' ================================================== ========================

Dim objReceive
Dim bDone

On error Resume Next

bDone = false
WScript.Echo "CreateObject"
Set objReceive = CreateObject("ComInterop.CComInterop")
if Err.Number <> 0 then
WScript.Echo "Error: CreateObject: " & Err.Description
else
objReceive.SomeMethod(7)
if Err.Number <> 0 then
WScript.Echo "Error: Setting SomeMethod: " & Err.Description
else
WScript.Sleep 3000
end if

end if

objReceive = nothing

WScript.Echo "Program Over"
' ================================================== ========================

Sub objReceive_SomeEvent()
WScript.Echo "Message: " & objReceive.SomeProperty
bDone = true
End Sub


--
Dan Sikorsky, MSCS BSCE BAB

Nov 17 '05 #1
0 1316

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ZAky | last post by:
Why do I get a System.NullReferenceException for this script? I added the following script to a simple form with a button. <Script> public event EventHandler DoThis; static void Main() {...
12
by: Vittorio Pavesi | last post by:
Hello, is it possible to manually raise the event Elapsed of the timer object ? Vittorio
4
by: Marco Meoni | last post by:
Hi. I read the Gordon McMillan's "Socket Programming Howto". I tried to use the example in this howto but this doesn't work. The code is class mysocket: '''classe solamente dimostrativa -...
3
by: Sako | last post by:
I am trying to teach myself perl by writing a program I've been meaning to implement, so I am pretty green in perl. I'm having problems sharing filehandles opened by a thread - been RTFM for a few...
2
by: Tina | last post by:
(my last post was incomplete so this issue is being reposted) I have an ASP.Net C# app with a Default.aspx page with an ascx user control on the page named EditGrid.ascx given the ID myEG on the...
3
by: =?Utf-8?B?Ulc=?= | last post by:
I constructed a new Class with some private members. I would like an event to be raised on the moment the value of one of those private members is changed. How do I define an event for that...
2
by: Sin Jeong-hun | last post by:
Suppose class Engine do something in another thread and raise events. class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new...
2
by: C. Herzog | last post by:
Hi! I want to create a huge amount of classes. All classes have in common that the change of a property should fire an Change-Event and maybe do other things. I don't want to write thousand...
2
by: Steven | last post by:
Hello, I want to raise the ValueChanged event ONLY when i move the track bar manually, but not the value changed, how to do in the event below? thank you Private Sub TrackBar1_ValueChanged(ByVal...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.