Solved

Events in .NET Between VB and C#

Posted on 2011-09-08
4
332 Views
Last Modified: 2013-11-08
I have a codebase that has mixed VB.NET and C#: it is slowly being rewritten into purely C#, but for now it has to be mixed.  Unfortunately, this has left me with a situation where a C# library that has events needs to communicate those events to a form application written in C#.

I have plenty of programming experience, both with VB.net and C#, but I haven't really gotten into the intricacies of events, especially between the two languages.  I've managed to wire-up events strictly in VB, and the same with C# (once or twice each)... but not across the two.

I'm looking for examples.  The basics are:
 * Form application in VB, displays "what's going on" to the user
 * C# library, firing events that indicate what's going on
 * need the VB app to handle the C# events

I don't know if this is possible.  Pointers are welcome, but please don't waste time focusing on the mixed environment unless doing so will result in real progress.
0
Comment
Question by:crazedsanity
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36503991
When coding in one .Net language treat all class library calls as if it is the same language. For example, if you have an event "OnStart" in you C# class library, then to wire it up in VB.Net:

AddHandler myClassObject.OnStart, AddressOf MyEventHandlerFunction
0
 
LVL 9

Author Comment

by:crazedsanity
ID: 36504148
Thanks for the info.  I have the VB code listening to the events, but the text isn't getting sent... I'm not really sure where to debug.

Is there an example of creating an event and event handler in C# that will allow a string to be sent?  It seems my C# is sending events that something has happened, but I'm not the text.

The C# code was originally for a console application that initializes a database.  I've adapted it somewhat, and I have events firing (I think), but the text doesn't get sent.  On the console, it writes things like:
Initializing content for language en-GB
 -- created 5432 records in 12.3 seconds
initializing content for language fr
 -- created 5432 records in 12.3 seconds

Open in new window


That info doesn't seem to get sent.  I've tried to put the last message into a variable to check... but that seems wrong (not to mention it doesn't actually work).
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36504305
Open both project in the same solution, set some break points where you fire the events and in the event handlers and see if C# is raising the event and if VB is handling the event.
0
 
LVL 9

Author Closing Comment

by:crazedsanity
ID: 36504684
The part that I was missing was that the event in C# was using the standard "EventArgs" class, while the method (in VB) that was updating the screen used a custom "UpdateStatusEventArgs" class.  By implementing that class in C# (taking the code and converting to C# using http://www.developerfusion.com/tools/convert/vb-to-csharp/), I was able to pass a string in the notification.  Sometimes it's difficult to see the forest through all them trees...

Thanks for the help, strickdd!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question