Solved

Event and delegate from C# to VB.NET

Posted on 2007-11-28
4
1,596 Views
Last Modified: 2013-12-16
Hi experts

i have this short code in C#

    public event EventHandler<LogAddedEventArgs> LogAdded;

   private void OnLogAdded(LogAddedEventArgs info)
        {
            if (LogAdded != null)
            {
                // Ce code permet l’exécution asynchrone des méthodes abonnées à  l’évènement
                Delegate[] DTab = LogAdded.GetInvocationList();
                foreach (Delegate tmp in DTab)
                {
                    EventHandler<LogAddedEventArgs> Dtmp = (EventHandler<LogAddedEventArgs>)tmp;
                    Dtmp.BeginInvoke(this, info, null, null);
                }
            }
        }


and when we translate in vb.NET

  Public Event LogAdded As EventHandler(Of LogAddedEventArgs)

 Private Sub OnLogAdded(ByVal info As LogAddedEventArgs)
            If LogAdded IsNot Nothing Then
                ' Ce code permet l’exécution asynchrone des méthodes abonnées à  l’évènement
                Dim DTab As [Delegate]() = LogAdded.GetInvocationList()
                For Each tmp As [Delegate] In DTab
                    Dim Dtmp As EventHandler(Of LogAddedEventArgs) = DirectCast(tmp, EventHandler(Of LogAddedEventArgs))
                    Dtmp.BeginInvoke(Me, info, Nothing, Nothing)
                Next
            End If
        End Sub


but i have error

seems that in C# they can "test" the delegate?!
can u help me to figure it out from c# to vb.net?

thank you

regards
0
Comment
Question by:Dnx_7
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20367117
Instead of this:
Dim DTab As [Delegate]() = LogAdded.GetInvocationList()

try this:
Dim DTab() As [Delegate] = LogAdded.GetInvocationList()
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 20367612
the error is that the "logadded" declared as event in vb.net cannot be tested in a "IF" condition and in C#, you can do that... (seems...)

regards
0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 20368052
I've done some reading which leads me to believe that VB.Net's event subscription model is indeed less... complete (for lack of a better word) than C#'s.  That said, you might find something useful in this article:
http://www.dotnetjohn.com/articles.aspx?articleid=62

Finally, I'm looking more closely at this C# line:

        if (LogAdded != null)

I'm more of a VB guy than a C# guy, so I may be missing something subtle in the C#, but it seems like this is supposed to determine if the LogAdded delegate has any handlers subscribing to it. If that's the case, the code may not be needed at all.  Since it's used inside code that handles the LogAdded event it won't run unless LogAdded has at least this method attached to it, and will therefore not be null.  However, I don't know that  the method won't be called explicitly as well, and again I may be missing something subtle.  


0
 
LVL 2

Author Comment

by:Dnx_7
ID: 20537169
thnak you :)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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