Solved

Event and delegate from C# to VB.NET

Posted on 2007-11-28
4
1,600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

635 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