papadi

Hi. I'm experienced in VB but new in C#.
What's the ecquievelant of the following in C#



Public Event MyEvent(arg1 as string, arg2 as int)

Public Sub RaiseMyEvent()
    RaiseEvent MyEvent("bla bla",15)
End Sub

 




Re: Visual C# Language event declaration

Daniel Rieck



public delegate void MyEventHandler(string arg1, int arg2);

public event MyEventHandler MyEvent;

protected virtual void OnMyEvent(string arg1, int arg2)
{
    MyEventHandler temp = MyEvent;
    if (temp != null)
    {
        temp(arg1, arg2);
    }
}

 






Re: Visual C# Language event declaration

papadi

So... it's not possible to declare a custom event without declaring a delegate. Right





Re: Visual C# Language event declaration

David Anton

Right - you need the delegate.

The actual translation is:

public
delegate void MyEventEventHandler(string arg1, int arg2);
public event MyEventEventHandler MyEvent;

public
void RaiseMyEvent()
{
   if (MyEvent != null)
      MyEvent("bla bla",15);
}

(Note that you do not need a temporary variable as one post showed)

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code






Re: Visual C# Language event declaration

Daniel Rieck

You need the temp variable to be thread-safe. In fact, this is the code from the "invoke" code snippet in VS2005.

I also made the changes to the RaiseMyEvent method (protected virtual, and called "On<event name>") because that's the convention.


Update: Of course the string and int should be wrapped in a custom EventArgs class and the signature of the delegate should be something like (object sender, MyEventArgs e)




Re: Visual C# Language event declaration

David Anton

Good point.

I focused on the literal translation - i.e., the C# equivalent to the original possibly un-thread-safe VB code.  Is the original VB code thread-safe - or does VB make this thread-safe behind the scenes






Re: Visual C# Language event declaration

papadi

...not sure about vb! Can you give me an example of a possible thread-unsafe scenario






Re: Visual C# Language event declaration

dkocur2

Imagine what would happen if there is one and only one "subscriber" to the event and that subscriber (running on a separate thread) "unsubscribes" after the "if" statement, but before the invokation. 




Re: Visual C# Language event declaration

papadi

I'm gonna have to create a sample to see the output IDL and compare it with the C# ecquivelant. I will get back to you for that.
But how would the temporary variable of the sample above guarantees thread-safety If the one and only user unsubscribes after if the result would be the same!




Re: Visual C# Language event declaration

Jon Skeet

Using the temporary variable guarantees that you won't get a NullReferenceException, which code such as:



if (someEvent != null)
{
    someEvent(foo);
}

 

doesn't - the value of someEvent can change between the check and the invocation.

However, using a temporary variable on its own doesn't guarantee that you'll see the most recent value of the delegate list.

See this part of my threading article for my preferred way of dealing with events, including making the event subscription/unsubscription threadsafe.





Re: Visual C# Language event declaration

GoldPantherSPQR

I tried these techniques, and all I got were tons of errors saying that the events can only reside on the left side of a += or -=. My version was explicitly setup how your article specified except that I had to add void on the OnSomeEvent virtual protected function. I even tried if(!someEvent.Equals(null)). It still brought up the same error.






Re: Visual C# Language event declaration

Thomas Danecker

It sounds like you want to race the event outside of the class you've declared it. This is not possible. You can only add or remove events (+=/-=) from the outside (public domain). The event can only be raised from within the class where you've declared the event (private domain).