在接口的可选参数中使用 Double.NaN

发布于 2024-10-11 19:23:54 字数 1662 浏览 5 评论 0原文

在 VB 应用程序中定义接口时,我在 Visual Studio 2010 中遇到了一些令人困惑或可能存在错误的情况:当使用 Double 类型的默认参数定义接口方法时,使用 Double.NaN 常量作为默认值会导致代码编辑器出现错误/intellisense/precompiler 一些问题。

以下代码强调“INaNTest”和“INaNTest.DoSomething”,声称“DoSomething”无法实现“DoSomething”,因为接口“INaNTest”上没有匹配的子项:

Public Class NaNTest
    Implements INaNTest
    Public Sub DoSomething(ByVal x As Double,
                           Optional ByVal a As Double = Double.NaN)
                           Implements INaNTest.DoSomething
    End Sub
End Class

Public Interface INaNTest
    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double = Double.NaN)
End Interface

删除实现并从以下位置开始:

Public Class NaNTest
    Implements INaNTest
End Class

Public Interface INaNTest
    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double = Double.NaN)
End Interface

现在“NaNTest”加下划线的位置(类“NaNTest”必须...),在“Implements INaNTest”行末尾按回车键(即自动插入实现)添加实现:

    Public Sub DoSomething(ByVal x As Double,
                           Optional ByVal a As Double = -1.#IND)
                           Implements INaNTest.DoSomething

    End Sub

然后代码编辑器在其中添加下划线“#”(需要标识符。)。因此代码自动添加了不正确的代码。

或者现在,从上面的原始代码开始,使用带下划线的“INaNTest.DoSomething”上的“错误更正选项”按钮,并选择“为“INaNTest”中的“DoSomething”生成方法存根,添加的方法存根是:

    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double = NaN)

现在“NaN”已经脱离了“双标”。前缀和下划线(“NaN”未声明。由于其保护级别,它可能无法访问。)代码编辑器再次插入了无效代码。

在 VB.net 中,是否有使用 Double.NaN 作为接口上定义的方法的默认值的正确解决方案,或者是否有根本原因导致这是不可能的?

非常感谢, 柯林斯

I have come across something confusing or potentially a bug in Visual Studio 2010 when defining an interface in my VB application: When defining an interface method with a default parameter of type Double, using the Double.NaN constant as the default value causes the code editor/intellisense/precompiler some issues.

The following code underlines "INaNTest" and "INaNTest.DoSomething" claiming that 'DoSomething' cannot implement 'DoSomething' because there is no matching sub on interface 'INaNTest':

Public Class NaNTest
    Implements INaNTest
    Public Sub DoSomething(ByVal x As Double,
                           Optional ByVal a As Double = Double.NaN)
                           Implements INaNTest.DoSomething
    End Sub
End Class

Public Interface INaNTest
    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double = Double.NaN)
End Interface

Removing the implementation and starting from:

Public Class NaNTest
    Implements INaNTest
End Class

Public Interface INaNTest
    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double = Double.NaN)
End Interface

where now "NaNTest" is underlined (Class 'NaNTest' must ...), hitting the return key at the end of the line "Implements INaNTest" (i.e. automatically insert implementation) adds the implementation:

    Public Sub DoSomething(ByVal x As Double,
                           Optional ByVal a As Double = -1.#IND)
                           Implements INaNTest.DoSomething

    End Sub

in which the code editor then underlines '#' (Identifier expected.). Thus the code automatically added code that is not right.

Alternatively now, starting with the original code above, using the Error Correction Options button on the underlined "INaNTest.DoSomething" and selecting 'Generate method stub for 'DoSomething' in 'INaNTest', the added method stub is:

    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double = NaN)

where now "NaN" has been divorced from the "Double." prefix and underlined ('NaN' is not declared. It may be inaccessible due to its protection level.) The code editor has inserted invalid code again.

Is there a correct solution to using Double.NaN as the default value for a method as defined on an interface, in VB.net, or is there a fundamental reason why this is impossible?

Many thanks,
JCollins

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-10-18 19:23:54

呃,那太丑了。很难将其定性为错误以外的其他任何东西。当您让 IDE 生成方法签名时,NaN 的默认格式显示了 VB.NET 团队使用的语言,这就是 C++ 运行时库格式化 NaN 的方式。试图让它相信你知道自己在做什么确实是徒劳的。

您可以在 connect.microsoft.com 上报告此问题。当您等待“在 Visual Studio 的下一版本中修复”这一问题时,您可能会考虑使用可空类型作为解决方法:

Public Class NaNTest
    Implements INaNTest
    Public Sub DoSomething(ByVal x As Double, Optional ByVal a As Double? = Nothing) Implements INaNTest.DoSomething
        If a.HasValue Then
            '' etc..
        End If
    End Sub
End Class

Public Interface INaNTest
    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double? = Nothing)
End Interface

Fwiw,当您使用 Double.Epsilon 时,它确实可以工作作为默认值。有点愚蠢,但也不是完全不合理的解决方法。只是不要让 IDE 生成实现,否则就会变得愚蠢。

Ugh, that's fugly. Hard to characterize this as anything other than a bug. The default formatting for NaN when you let the IDE generate the method signature shows what language the VB.NET team uses, that's the way the C++ runtime library formats NaN. Attempts to convince it you know what you are doing are indeed futile, afaict.

You can report this at connect.microsoft.com. While you wait for the 'fixed in the next version of Visual Studio' to see the light of day, you might consider using nullable types as the workaround:

Public Class NaNTest
    Implements INaNTest
    Public Sub DoSomething(ByVal x As Double, Optional ByVal a As Double? = Nothing) Implements INaNTest.DoSomething
        If a.HasValue Then
            '' etc..
        End If
    End Sub
End Class

Public Interface INaNTest
    Sub DoSomething(ByVal x As Double,
                    Optional ByVal a As Double? = Nothing)
End Interface

Fwiw, it does work when you use Double.Epsilon as the default value. Kinda silly but not an entirely unreasonable workaround either. Just don't let the IDE generate the implementation, then it gets silly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文