empi
empi

Reputation: 15901

How to call method written in VB.NET from C# with optional arguments

I have a method written in VB.NET. It looks like this:

Shared Sub SomeMethod(ByVal Id As Guid, 
                      Optional ByVal str1 As String = "foo", 
                      Optional ByVal str2 As String = "")

I want to call this method from C# 3.0 and I want it to use its default arguments. I tried passing System.Reflection.Missing.Value, but I cannot cast it as String.

Is there any way to do that?

Thanks in advance for help.

Upvotes: 3

Views: 949

Answers (3)

Andrey
Andrey

Reputation: 60095

if you want to retrieve optional parameters' values you can use reflection, this information is stored in custom attribute at corresponding parameter

Upvotes: 0

STW
STW

Reputation: 46394

Without using C# 4.0 (which adds support for optional parameters) you can't use them; if you run your code through FxCop you will see Optional parameters specifically flagged for their inability to be consumed by C#.

Upvotes: 2

Henk Holterman
Henk Holterman

Reputation: 273711

No, in C#3 you simply have to pass all parameters. C#4 will have optional and named parameters.

You could off course create a few overloaded variations, but that is only an approximation.

Upvotes: 9

Related Questions