Using Double.NaN in optional parameter on interface
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 i开发者_如何学编程nterface 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
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.
精彩评论