C# Conversion - AddressOf Delegate

I am looking for help converting the following block of C# code to VB.Net:

public static void CloseSplash()
{
frmSplash formSplash = SplashInstance();

if (formSplash.InvokeRequired)
formSplash.Invoke(new CloseDelegate(CloseSplash), null);
else
formSplash.Close();
}

I have used various C# to VB Converters... They return the following:

Public Shared Sub CloseSplash()
Dim FormSplash As frmSplash = SplashInstance
If FormSplash.InvokeRequired Then
FormSplash.Invoke(AddressOf CloseSplash, Nothing)
Else
FormSplash.Close()
End If
End Sub

However this code generates a " 'AddressOf' expression cannot be converted to 'System.Delegate' because type 'System.Delegate' is declared 'MustInherit' and cannot be created." error.

MSDN says I need to "Assign the AddressOf expression to a specific delegate type", but I guess I don't understand this Any pointers would be appreciated.



Answer this question

C# Conversion - AddressOf Delegate

  • johnconstantine

    you need to declear a delegate

    Public Class Form1

    Private Delegate Sub CloseDelegate()

    Public Shared Sub CloseSplash()

    Dim FormSplash As frmSplash = SplashInstance

    If FormSplash.InvokeRequired Then

    FormSplash.Invoke(New CloseDelegate(AddressOf CloseSplash), Nothing)

    Else

    FormSplash.Close()

    End If

    End Sub

    End Class



  • Skyline58687

    Already had the delegate declaration, however I did not have "New CloseDelegate" part in my FormSplash.Invoke statement. That took care of it. Thanks for your help!


  • JayH

    Use a better C# to VB converter.

    Adding the C# delegate declaration included in the original sample, we get the following from Instant VB:

    Private Delegate Sub CloseDelegate()
    Public Shared Sub CloseSplash()
    Dim formSplash As frmSplash = SplashInstance()

    If formSplash.InvokeRequired Then
    formSplash.Invoke(New CloseDelegate(AddressOf CloseSplash), Nothing)
    Else
    formSplash.Close()
    End If
    End Sub

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter and VB to C++ converter
    Instant J#: VB to J# converter
    Clear VB: Cleans up VB.NET code
    Clear C#: Cleans up C# code



  • C# Conversion - AddressOf Delegate