471,570 Members | 902 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Handling a C# event from VB.NET

I have the following code. I am trying to handle the event in the C#
code in VB.net. However whenever the event tries to get called, the
MyEvent method is always null..... any ideas?

VB.NET
dim xml as new MyClass
AddHandler xml.MyEvent, New EventHandler(AddressOf xmlEvent)

C#
public class XMLHandler
{
public event EventHandler MyEvent;

protected virtual void OnRaiseMe(object o, EventArgs e)
{
if (MyEvent!=null)
MyEvent(this, e); //Doesn't happen as MyEvent is null
}
}

Nov 29 '05 #1
3 3042

Dave wrote:
I have the following code.
Really?
I am trying to handle the event in the C#
code in VB.net. However whenever the event tries to get called, the
MyEvent method is always null..... any ideas?

VB.NET
dim xml as new MyClass
AddHandler xml.MyEvent, New EventHandler(AddressOf xmlEvent)


You haven't defined MyClass (you call your C# class 'XMLHandler', and
in fact 'MyClass' is a VB.NET keyword, so this line can't even
compile. You also don't show us xmlEvent.

This code works for me in VS2003:

In a C# class library (this is your code with the addition of a public
method that raises the event):

using System;

namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class XMLHandler
{
public event EventHandler MyEvent;

public void RaiseE()
{
OnRaiseMe(null, null);
}

protected virtual void OnRaiseMe(object o, EventArgs e)
{
if (MyEvent!=null)
MyEvent(this, e); //Doesn't happen as MyEvent is null
}
}
}

In a VB.NET Windows Forms application with a reference to the above:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim x As ClassLibrary1.XMLHandler

x = New ClassLibrary1.XMLHandler

AddHandler x.MyEvent, AddressOf Me.MyEventHandler

x.RaiseE()
End Sub

Public Sub MyEventHandler(ByVal o As Object, ByVal e As EventArgs)
MsgBox("pop")
End Sub

Click the button, up comes the message box.

--
Larry Lard
Replies to group please

Nov 29 '05 #2
Thank you for your help Larry, your a true gent!

Nov 29 '05 #3
Dave,

The problem is not in the code you've posted. Even though I was sure I've
tested it and it works.

The only reason of this to happen that I can think of from the top of my
head is if you re-create the XMLHandler object after you attach the event
handler.
--

Stoitcho Goutsev (100) [C# MVP]

"Dave" <da*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have the following code. I am trying to handle the event in the C#
code in VB.net. However whenever the event tries to get called, the
MyEvent method is always null..... any ideas?

VB.NET
dim xml as new MyClass
AddHandler xml.MyEvent, New EventHandler(AddressOf xmlEvent)

C#
public class XMLHandler
{
public event EventHandler MyEvent;

protected virtual void OnRaiseMe(object o, EventArgs e)
{
if (MyEvent!=null)
MyEvent(this, e); //Doesn't happen as MyEvent is null
}
}

Nov 29 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by hillcountry74 | last post: by
9 posts views Thread by Sridhar | last post: by
2 posts views Thread by Paul E. Orman | last post: by
4 posts views Thread by reggiestyles | last post: by
1 post views Thread by stmfc | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by

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.