tags:

views:

99

answers:

3
static RelatedPosts()
{
 Post.Saved += new EventHandler<SavedEventArgs>(Post_Saved);
}

static void Post_Saved(object sender, SavedEventArgs e)
{
 if (e.Action == SaveAction.Update)
 {
  Post post = (Post)sender;
  if (_Cache.ContainsKey(post.Id))
   _Cache.Remove(post.Id);
 }
}

I converted to:

Shared Sub New()
    Post.Saved += New EventHandler(Of SavedEventArgs)(AddressOf Post_Saved)
End Sub


Private Shared Sub Post_Saved(ByVal sender As Object, ByVal e As SavedEventArgs)
    If e.Action = SaveAction.Update Then
        Dim post As Post = DirectCast(sender, Post)
        If _Cache.ContainsKey(post.Id) Then
            _Cache.Remove(post.Id)
        End If
    End If
End Sub

But it give me an error:

Public Shared event Saved() is an event and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

A: 

There are tools available to provide the conversion for you. There is a very well known tool which allows the inspection of assemblies (I won't name it explicitly). I think in this instance when a free tool is available to save time and effort there is little point unless - talking about the specifics of event handling syntax differences - in doing this here! I hope I dont offend anyone with this view.

brumScouse
+2  A: 

From what I can gather you have an event called Save which you are trying to invoke somewhere and you are probably doing something like:

Save(myObject, mySavedEventArgs)

The above is ok in C#, however, in VB.NET you need to use the RaiseEvent statement:

RaiseEvent Save(myObject, mySavedEventArgs)
James
+3  A: 

Use this

AddHandler Post.Saved, AddressOf Post_Saved

instead of

Post.Saved += New EventHandler(Of SavedEventArgs)(AddressOf Post_Saved)
shahkalpesh
That is cool thanks
Barga