tags:

views:

136

answers:

3

Hi,

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.

+9  A: 

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.

Henk Holterman
+1. This is exactly what the VB.Net compiler does anyway - it simply automatically inserts the default values for any missing values when it compiles the caller.
MarkJ
@MarkJ, yes, but in Fx4 the optional values are also in the metadata, I just verified with VS2010. C# uses the opt values from VB just fine.
Henk Holterman
+2  A: 

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#.

STW
A: 

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

Andrey
I think you misunderstood the question. He wants to do `SomeMethod(someGUID, "Some String")` from C# where the VB Method Signature is as he indicated. Something that's not possible on C# < 4.0
jamone