3

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.

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+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, 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.
2

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

Comments

0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.