Calling a Visual Basic function with optional parameters with a C# call
I am interacting with Visual Basic code on a different tier, using a client-side C# program. The Visual Basic function signature looks like this:
Public Sub toggleExclusion( _
ByVal mouse As Double, _
ByV开发者_如何学Goal study As Integer, _
Optional ByVal box As Integer = 0)
When I call this from C# as such:
_obj.toggleExclusion(mouse, study)
I get an error saying no overloaded method of toggleExclusion takes two arguments. Why?
That depends on the C# version. Older C# versions don’t yet support optional arguments – you always need to specify all of them. Since C# 4 optional arguments are supported, too.
A workaround would be to pass the optional argument - since it has a default, there is no loss if you just pass it.
You might be able to use System.Reflection.Missing.Value
. I am not sure if it works for a Visual Basic call.
精彩评论