【问题标题】:Translate Delegates from c# to VB.NET将代表从 c# 转换为 VB.NET
【发布时间】:2017-07-14 02:36:33
【问题描述】:

我正在使用来自 BrightIdeas 软件的 TreeListView 控件。看起来不错,但我对代表不熟悉,示例是用 c# 编写的。有人可以帮我把它翻译成 VB.NET 吗?

这是一个例子:

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

这是我对意图的最佳猜测(显然是错误的)

    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

而函数定义为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

根据建议,通过翻译进行。看起来不太对劲。输出:

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

【问题讨论】:

  • 您是否尝试过先通过翻译器运行此程序?
  • 译者的准确率远不及 100%,但我同意这是一个很好的起点。
  • 查看修订版,你!

标签: vb.net objectlistview treelistview


【解决方案1】:

您的 C# 代码是在 C# 有 lambda 之前您必须尝试编写 lambda 的方式 - 它通常写成:

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

VB 等价物是:

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

【讨论】:

  • 非常感谢,现在这很有意义。
猜你喜欢
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
相关资源
最近更新 更多