Translate Delegates from c# to VB.NET

302 Views Asked by At

I am using the TreeListView control from BrightIdeas software. Looks great but I'm not familiar with delegates, and the example is in c#. Can someone lend me a hand translating this to VB.NET?

This is the example:

    this.treeListView.CanExpandGetter = delegate(object x) {
        Return ((MyFileSystemInfo) x).IsDirectory;
    };

And this is my best guess as to the intent (obviously wrong)

    Dim expander As TreeListView.CanExpandGetterDelegate
    expander = AddressOf IsReportPopulated
    '// CanExpandGetter Is called very often! It must be very fast.
    Me.treeListView.CanExpandGetter = expander(x As Object) ' no idea where we are getting the object from

And the function is defined as this

Private Function IsReportPopulated(x As Object) As Boolean
    Dim myreport As ADCLReport = CType(x, ADCLReport)
    If myreport.Chambers.Count > 0 Or myreport.Electrometers.Count > 0 Then Return True
    Return False
End Function

Per advice, ran through a translator. Doesn't look quite right. Output:

thisPublic Delegate Sub ((ByVal Unknown As x)
{Return(CType(x,MyFileSystemInfo)).IsDirectory
UnknownUnknown
1

There are 1 best solutions below

1
Dave Doknjas On BEST ANSWER

Your C# code is the way you had to attempt to write a lambda before C# had lambdas - it's usually written as:

this.treeListView.CanExpandGetter = (object x) =>
{
    return ((MyFileSystemInfo)x).IsDirectory;
};

The VB equivalent is:

Me.treeListView.CanExpandGetter = Function(x As Object)
    Return DirectCast(x, MyFileSystemInfo).IsDirectory
End Function