Search code examples
c#.netvb.netdelegatescode-translation

Convert C# dictionary with delegate values to its VB.NET equivalent


I require some help to understand how can I translate this kind of dictionary entries that uses a delegate as value, from C# to VB.NET:

using LookupT = 
    System.Collections.Generic.KeyValuePair<string, System.Func<BBCodeNode, bool, string>>;

public delegate string HtmlRendererCallback(BBCodeNode Node, 
                                            bool ThrowOnError, 
                                            object LookupTable);

static readonly LookupT[] convertLookup = 
    new Dictionary<string, HtmlRendererCallback>
    {
        { "b", DirectConvert }
    }.ToArray();

public static string DirectConvert(BBCodeNode Node, 
                                   bool ThrowOnError, 
                                   object LookupTable)
{
    //...
} 

What I tried is this translation below which will not work, the debugger marks the function name as error in the dictionary entry ( {"b", DirectConvert()} ) asking me to pass the proper arguments to the DirectConvert function:

Imports LookupT = 
    System.Collections.Generic.KeyValuePair(Of String, System.Func(Of BBCodeNode, Boolean, String))

Public Delegate Function HtmlRendererCallback(node As BBCodeNode, 
                                              throwOnError As Boolean, 
                                              lookupTable As Object) As String

Shared ReadOnly convertLookup As LookupT() = 
    New Dictionary(Of String, HtmlRendererCallback)() From 
        {
            {"b", DirectConvert()}
        }.ToArray()

Public Shared Function DirectConvert(node As BBCodeNode, 
                                     throwOnError As Boolean,
                                     lookupTable As Object) As String
' ...
End sub

...however, in the C# code if I'm not wrong it seems like it is not specifying at all any argument to the function, so... it is confusing me.


Solution

  • Could you please try this:

        Public Delegate Function HtmlRendererCallback(ByVal node As BBCodeNode, ByVal throwOnError As Boolean, ByVal lookupTable As Object) As String
        Shared convertMeth As HtmlRendererCallback = AddressOf DirectConvert
        Shared ReadOnly convertLookup As ILookup(Of String, HtmlRendererCallback) = New Dictionary(Of String, HtmlRendererCallback)() From {
            {"b", convertMeth},
            {"c", convertMeth},
            {"d", convertMeth}
        }
    
        Public Shared Function DirectConvert(ByVal node As BBCodeNode, ByVal throwOnError As Boolean, ByVal lookupTable As Object) As String
            DirectConvert = ""
        End Function