Solved

Event and delegate from C# to VB.NET

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

8 Experts available now in Live!

Get 1:1 Help Now