locked
Convert C# to VB

    Question

  • I am having some trouble "translating" some C# code to VB from an excellent class I found at http://bloggingabout.net/blogs/jschreuder/archive/2006/05/31/12272.aspx

    Here is the section I need some help with

    /// <summary>
    /// Definition for the FileEvent.
    /// </summary>

    public delegate void FileEventHandler(object sender, FileEventArgs e);

    /// <summary>
    /// Event is raised for each file in a directory.
    /// </summary>

    public event FileEventHandler FileEvent;

    /// <summary>
    /// Raises the file event.
    /// </summary>
    /// <param name="fileInfo"><see cref="FileInfo"/> object for the current file.</param>

    private bool RaiseFileEvent(FileInfo fileInfo)

    {
    bool continueScan = true;

    // Create a new argument object for the file event.

    FileEventArgs args = new FileEventArgs(fileInfo);

    // Now raise the event.

    FileEvent(this, args);
    continueScan = !args.Cancel;
    return continueScan;

    }

    /// <summary>
    /// Definition for the DirectoryEvent.
    /// </summary>

    public delegate void DirectoryEventHandler(object sender, DirectoryEventArgs e);

    /// <summary>

    /// Event is raised for each directory.
    /// </summary>

    public event DirectoryEventHandler DirectoryEvent;

    /// <summary>
    /// Raises the directory event.
    /// </summary>

    /// <param name="directory"><see cref="DirectoryInfo"/> object for the current path.</param>
    /// <param name="action">The <see cref="ScanDirectoryAction"/> action value.</param>
    /// <returns><see langword="true"/> when the scan is allowed to continue. <see langword="false"/> if otherwise;</returns>

    private bool RaiseDirectoryEvent(DirectoryInfo directory, ScanDirectoryAction action)

    {
    bool continueScan = true;

    // Only do something when the event has been declared.

    if (FileEvent != null)  //How do I translate this line to VB?

    {

    // Create a new argument object for the file event.

    DirectoryEventArgs args = new DirectoryEventArgs(directory, action);

    // Now raise the event.

    DirectoryEvent(this, args);

    continueScan = !args.Cancel;

    }

    return continueScan;

    }

    Thanks,

    Luis

     

    Wednesday, July 26, 2006 2:58 PM

Answers

  • The on-line converters, including the one mentioned in the other post, do not convert this correctly.

    Our Instant VB C# to VB converter produces the following - note the use of the hidden event variables required in VB (e.g., FileEventEvent):

    ''' <summary>
    ''' Definition for the FileEvent.
    ''' </summary>

    Public Delegate Sub FileEventHandler(ByVal sender As Object, ByVal e As FileEventArgs)

    ''' <summary>
    ''' Event is raised for each file in a directory.
    ''' </summary>

    Public Event FileEvent As FileEventHandler

    ''' <summary>
    ''' Raises the file event.
    ''' </summary>
    ''' <param name="fileInfo"><see cref="FileInfo"/> object for the current file.</param>

    Private Function RaiseFileEvent(ByVal fileInfo As FileInfo) As Boolean

    Dim continueScan As Boolean = True

    ' Create a new argument object for the file event.

    Dim args As FileEventArgs = New FileEventArgs(fileInfo)

    ' Now raise the event.

    RaiseEvent FileEvent(Me, args)
    continueScan = Not args.Cancel
    Return continueScan

    End Function

    ''' <summary>
    ''' Definition for the DirectoryEvent.
    ''' </summary>

    Public Delegate Sub DirectoryEventHandler(ByVal sender As Object, ByVal e As DirectoryEventArgs)

    ''' <summary>

    ''' Event is raised for each directory.
    ''' </summary>

    Public Event DirectoryEvent As DirectoryEventHandler

    ''' <summary>
    ''' Raises the directory event.
    ''' </summary>

    ''' <param name="directory"><see cref="DirectoryInfo"/> object for the current path.</param>
    ''' <param name="action">The <see cref="ScanDirectoryAction"/> action value.</param>
    ''' <returns><see langword="true"/> when the scan is allowed to continue. <see langword="false"/> if otherwise;</returns>

    Private Function RaiseDirectoryEvent(ByVal directory As DirectoryInfo, ByVal action As ScanDirectoryAction) As Boolean

    Dim continueScan As Boolean = True

    ' Only do something when the event has been declared.

    If Not FileEventEvent Is Nothing Then 'How do I translate this line to VB?


    ' Create a new argument object for the file event.

    Dim args As DirectoryEventArgs = New DirectoryEventArgs(directory, action)

    ' Now raise the event.

    RaiseEvent DirectoryEvent(Me, args)

    continueScan = Not args.Cancel

    End If

    Return continueScan

    End Function

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Clear VB: Cleans up VB code
    C# Code Metrics: Quick metrics for C#

     

    Thursday, July 27, 2006 12:39 AM

All replies

  • Have you tried online converters like Convert C# to VB.NET on Developer Fusion?
    Wednesday, July 26, 2006 3:19 PM
  • The on-line converters, including the one mentioned in the other post, do not convert this correctly.

    Our Instant VB C# to VB converter produces the following - note the use of the hidden event variables required in VB (e.g., FileEventEvent):

    ''' <summary>
    ''' Definition for the FileEvent.
    ''' </summary>

    Public Delegate Sub FileEventHandler(ByVal sender As Object, ByVal e As FileEventArgs)

    ''' <summary>
    ''' Event is raised for each file in a directory.
    ''' </summary>

    Public Event FileEvent As FileEventHandler

    ''' <summary>
    ''' Raises the file event.
    ''' </summary>
    ''' <param name="fileInfo"><see cref="FileInfo"/> object for the current file.</param>

    Private Function RaiseFileEvent(ByVal fileInfo As FileInfo) As Boolean

    Dim continueScan As Boolean = True

    ' Create a new argument object for the file event.

    Dim args As FileEventArgs = New FileEventArgs(fileInfo)

    ' Now raise the event.

    RaiseEvent FileEvent(Me, args)
    continueScan = Not args.Cancel
    Return continueScan

    End Function

    ''' <summary>
    ''' Definition for the DirectoryEvent.
    ''' </summary>

    Public Delegate Sub DirectoryEventHandler(ByVal sender As Object, ByVal e As DirectoryEventArgs)

    ''' <summary>

    ''' Event is raised for each directory.
    ''' </summary>

    Public Event DirectoryEvent As DirectoryEventHandler

    ''' <summary>
    ''' Raises the directory event.
    ''' </summary>

    ''' <param name="directory"><see cref="DirectoryInfo"/> object for the current path.</param>
    ''' <param name="action">The <see cref="ScanDirectoryAction"/> action value.</param>
    ''' <returns><see langword="true"/> when the scan is allowed to continue. <see langword="false"/> if otherwise;</returns>

    Private Function RaiseDirectoryEvent(ByVal directory As DirectoryInfo, ByVal action As ScanDirectoryAction) As Boolean

    Dim continueScan As Boolean = True

    ' Only do something when the event has been declared.

    If Not FileEventEvent Is Nothing Then 'How do I translate this line to VB?


    ' Create a new argument object for the file event.

    Dim args As DirectoryEventArgs = New DirectoryEventArgs(directory, action)

    ' Now raise the event.

    RaiseEvent DirectoryEvent(Me, args)

    continueScan = Not args.Cancel

    End If

    Return continueScan

    End Function

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Clear VB: Cleans up VB code
    C# Code Metrics: Quick metrics for C#

     

    Thursday, July 27, 2006 12:39 AM
  • Thanks for the information. All of the online converters that I tried did not handle the FileEventEvent line correctly.

    BTW, I installed the demo of your software and it would not run on my computer.

    Luis 

    Thursday, July 27, 2006 12:45 PM
  • Please contact us via our support or info emails about this.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Clear VB: Cleans up VB.NET code
    C# Code Metrics: Quick metrics for C#

    Thursday, July 27, 2006 2:36 PM