2

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, _
    ByVal 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darren Young
  • 10,972
  • 36
  • 91
  • 150

3 Answers3

3

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
1

A workaround would be to pass the optional argument - since it has a default, there is no loss if you just pass it.

Adrian
  • 2,244
  • 18
  • 20
  • If it's passed in it takes the passed in value, not the optional default. This works fine if the optional default happens to be default(T), but probably won't work if the default was `42`. In that case the passed in value overrides it. – Ahmad Mageed Nov 03 '10 at 15:31
1

You might be able to use System.Reflection.Missing.Value. I am not sure if it works for a Visual Basic call.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Doggett
  • 3,364
  • 21
  • 17