Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Event and delegate from C# to VB.NET

Posted on 2007-11-28
4
Medium Priority
?
1,601 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 1500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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