【问题标题】:How to convert this line of code from C# to vb.net如何将这行代码从 C# 转换为 vb.net
【发布时间】:2012-02-03 09:29:59
【问题描述】:

我正在使用 http://www.developerfusion.com/tools/convert/csharp-to-vb/ 将代码块从 C# 转换为 VB.NET

除了一行我不知道如何转换之外,一切都很顺利:

C# 源代码

result.DrawPolyline(Array.ConvertAll<PointF, Point>(pts, Point.Round), true, new Bgr(Color.Red), 5);

使用转换器可以做到这一点

result.DrawPolyline(Array.ConvertAll(Of PointF, Point)(pts, Point.Round), True, New Bgr(Color.Red), 5)

上面一行的错误是:

未为“公共共享”的参数“值”指定参数 函数 Round(value As System.Drawing.PointF) As System.Drawing.Point'。

【问题讨论】:

  • 也许 VB.Net 认为 Point.Round 是一个函数而不是枚举的值,因此会询问您一个参数。当您在 (pts, 之后键入 Point. 时,您的智能感知(或相同)会发生什么?

标签: c# vb.net c#-to-vb.net


【解决方案1】:

这应该将一个 PointF 数组转换为 Point:

result.DrawPolyline(Array.ConvertAll(Of PointF, Point)(pts, Function(p) Point.Round(p)), True, New Bgr(Color.Red), 5)

您需要将PointF 传递给Point.Round

测试:

Dim pts As PointF() = {New PointF(123.23, 12345.23)}
Dim r = Array.ConvertAll(Of PointF, Point)(
            pts,
            Function(p) Point.Round(p))

如果您将委托传递给@Jon 提到的Point.Round,它也会起作用:

Dim pts As PointF() = {New PointF(123.23, 12345.23)}
Dim r = Array.ConvertAll(Of PointF, Point)(
            pts,
            AddressOf Point.Round)

【讨论】:

    【解决方案2】:

    Array.ConvertAll 的第二个参数应该是用于转换的方法(参见here

    有可能是在Point.Round调用之前该行缺少AddressOf,所以方法作为委托传递,而不是被执行,如下:

    result.DrawPolyline(Array.ConvertAll(Of PointF, Point)(pts, AddressOf Point.Round), True, New Bgr(Color.Red), 5)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多