Results 1 to 3 of 3

Thread: problems with VB.NET equivalent in C#

  1. #1

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    32

    Unhappy problems with VB.NET equivalent in C#

    Hi
    I could not solve this problem.
    could someone help me?
    Please tell vb.net equivalent of following:
    Code:
    #region using
    
    using System;
    using System.Web;
    using BlogEngine.Core.Web.Controls;
    using BlogEngine.Core;
    using System.Net.Mail;
    using System.Threading;
    
    #endregion
    
    /// <summary>
    /// Pings all the ping services specified on the 
    /// PingServices admin page and send track- and pingbacks
    /// </summary>
    [Extension("Pings all the ping services specified on the PingServices admin page and send track- and pingbacks", "1.3", "BlogEngine.NET")]
    public class SendPings
    {
    
      /// <summary>
      /// Hooks up an event handler to the Post.Saved event.
      /// </summary>
      static SendPings()
      {
            Post.Saved += new EventHandler<SavedEventArgs>(Post_Saved);
    		Page.Saved += new EventHandler<SavedEventArgs>(Post_Saved);
      }
    
      /// <summary>
      /// Sends the pings in a new thread.
      /// <remarks>
      /// It opens a new thread and executes the pings from there,
      /// because it takes some time to complete.
      /// </remarks>
      /// </summary>
      private static void Post_Saved(object sender, SavedEventArgs e)
      {
    		if (e.Action == SaveAction.None || e.Action == SaveAction.Delete)
    			return;
    
            IPublishable item = (IPublishable)sender;
    
    		if (item.IsVisibleToPublic)
    		{
    			Uri url = item.AbsoluteLink;
    			ThreadPool.QueueUserWorkItem(delegate { Ping(item, url); });
    		}
      }
    
      /// <summary>
      /// Executes the pings from the new thread.
      /// </summary>
      private static void Ping(IPublishable item, Uri itemUrl)
      {
    		try
    		{
    			System.Threading.Thread.Sleep(500);
    
    			// Ping the specified ping services.
    			BlogEngine.Core.Ping.PingService.Send(itemUrl);
    
    			// Send trackbacks and pingbacks.
    			if (!BlogSettings.Instance.EnableTrackBackSend && !BlogSettings.Instance.EnablePingBackSend)
    				return;
    
    			if (item.Content.ToUpperInvariant().Contains("\"HTTP"))
    				BlogEngine.Core.Ping.Manager.Send(item, itemUrl);
    		}
    		catch (Exception)
    		{
    		  // We need to catch this exception so the application doesn't get killed.
    		}
      }
    
    }

    I tried using C# to VB.NET code converter which gave me this:

    Code:
    Imports System
    Imports System.Web
    Imports BlogEngine.Core.Web.Controls
    Imports BlogEngine.Core
    Imports System.Net.Mail
    Imports System.Threading
    
    <Extension("Pings all the ping services specified on the PingServices admin page and send track- and pingbacks", "1.3", "BlogEngine.NET")> _
    Public Class SendPings
        Shared Sub New()
            AddHandler Post.Saved, AddressOf Post_Saved
            AddHandler Page.Saved, AddressOf Post_Saved
        End Sub
    
        Private Shared Sub Post_Saved(ByVal sender As Object, ByVal e As SavedEventArgs)
            If e.Action = SaveAction.None OrElse e.Action = SaveAction.Delete Then
                Exit Sub
            End If
    
            Dim item As IPublishable = DirectCast(sender, IPublishable)
            If item.IsVisibleToPublic Then
                Dim url As Uri = item.AbsoluteLink
                 ThreadPool.QueueUserWorkItem(AddressOf Ping, item)
            End If
       End Sub
    
        Private Shared Sub Ping(ByVal item As IPublishable, ByVal itemUrl As Uri)
            Try
                System.Threading.Thread.Sleep(500)
    
                ' Ping the specified ping services.
                BlogEngine.Core.Ping.PingService.Send(itemUrl)
    
                ' Send trackbacks and pingbacks.
                If Not BlogSettings.Instance.EnableTrackBackSend AndAlso Not BlogSettings.Instance.EnablePingBackSend Then
                    Exit Sub
                End If
    
                If item.Content.ToUpperInvariant().Contains("""HTTP") Then
                    BlogEngine.Core.Ping.Manager.Send(item, itemUrl)
                End If
            Catch generatedExceptionName As Exception
                ' We need to catch this exception so the application doesn't get killed.
            End Try
        End Sub
    
    End Class
    the error is
    Method 'Private Shared Sub Ping(item As BlogEngine.Core.IPublishable, itemUrl As System.Uri)' does not have a signature compatible with delegate 'Delegate Sub WaitCallback(state As Object)'.

    Which on build in visual studio 2008 gives an error.

    Please help

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: problems with VB.NET equivalent in C#

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: problems with VB.NET equivalent in C#

    It's a little awkward in 2008:

    Code:
    #Region "using"
    
    Imports System
    Imports System.Web
    Imports BlogEngine.Core.Web.Controls
    Imports BlogEngine.Core
    Imports System.Net.Mail
    Imports System.Threading
    
    #End Region
    
    ''' <summary>
    ''' Pings all the ping services specified on the 
    ''' PingServices admin page and send track- and pingbacks
    ''' </summary>
    <Extension("Pings all the ping services specified on the PingServices admin page and send track- and pingbacks", "1.3", "BlogEngine.NET")> _
    Public Class SendPings
    
      ''' <summary>
      ''' Hooks up an event handler to the Post.Saved event.
      ''' </summary>
      Shared Sub New()
    		AddHandler Post.Saved, AddressOf Post_Saved
    		AddHandler Page.Saved, AddressOf Post_Saved
      End Sub
    
      ''' <summary>
      ''' Sends the pings in a new thread.
      ''' <remarks>
      ''' It opens a new thread and executes the pings from there,
      ''' because it takes some time to complete.
      ''' </remarks>
      ''' </summary>
      Private Shared Sub Post_Saved(ByVal sender As Object, ByVal e As SavedEventArgs)
    		If e.Action = SaveAction.None OrElse e.Action = SaveAction.Delete Then
    			Return
    		End If
    
    		Dim item As IPublishable = CType(sender, IPublishable)
    
    		If item.IsVisibleToPublic Then
    			Dim url As Uri = item.AbsoluteLink
    			ThreadPool.QueueUserWorkItem(Function() AnonymousMethod1(item, url))
    		End If
      End Sub
      
      Private Shared Function AnonymousMethod1(ByVal item As IPublishable, ByVal url As Uri) As Object
    	  Ping(item, url)
    	  Return Nothing
      End Function
    
      ''' <summary>
      ''' Executes the pings from the new thread.
      ''' </summary>
      Private Shared Sub Ping(ByVal item As IPublishable, ByVal itemUrl As Uri)
    		Try
    			System.Threading.Thread.Sleep(500)
    
    			' Ping the specified ping services.
    			BlogEngine.Core.Ping.PingService.Send(itemUrl)
    
    			' Send trackbacks and pingbacks.
    			If (Not BlogSettings.Instance.EnableTrackBackSend) AndAlso (Not BlogSettings.Instance.EnablePingBackSend) Then
    				Return
    			End If
    
    			If item.Content.ToUpperInvariant().Contains("""HTTP") Then
    				BlogEngine.Core.Ping.Manager.Send(item, itemUrl)
    			End If
    		Catch e1 As Exception
    		  ' We need to catch this exception so the application doesn't get killed.
    		End Try
      End Sub
    
    End Class
    In 2010 (VB10), you can just use a "Sub" lambda and avoid the wrapper 'anonymous method':
    Code:
    ThreadPool.QueueUserWorkItem(Sub() Ping(item, url))
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width