Solved

Events in .NET Between VB and C#

Posted on 2011-09-08
4
328 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now