Solved

Event and delegate from C# to VB.NET

Posted on 2007-11-28
4
1,592 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

16 Experts available now in Live!

Get 1:1 Help Now