Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you convert this to VB?

Tags:

c#

vb.net

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.

like image 762
Barga Avatar asked Dec 13 '25 07:12

Barga


1 Answers

Use this

AddHandler Post.Saved, AddressOf Post_Saved

instead of

Post.Saved += New EventHandler(Of SavedEventArgs)(AddressOf Post_Saved)
like image 164
shahkalpesh Avatar answered Dec 14 '25 19:12

shahkalpesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!