0

I have been told that there is no such thing as optional parameters in C#. But you can use overload functionality and input varibles with default values such as:

void Person(string name, int age)
void Person(string name)

and

void Person(string name, int age = 30)

My problem is that when calling C# components from VB6 overloaded methods tends to change name to for example "Person(string name), Person_1(string name, int age)" etc, and variables with default values can't be used when beeing "out" variables.

BUT how about

void Person(string name, [Optional] int age)

??

Can someone explain to me how that work and if i can use it to simulate optional variables in VB6?

4
  • 1
    nit-pickers corner, but it's not override functionality, it's overload in this case. Aug 22, 2011 at 12:21
  • Ahh, ofcourse its overload! I think I have read too much today and is abit dizzy.
    – user885982
    Aug 22, 2011 at 12:54
  • [Optional] works but the argument type must be object so that the caller can pass a variant of type vtEmpty. Clearly that's not what you want. Aug 23, 2011 at 4:41
  • I think i´ll maybe have to think "outside the box" on this one. If I find a working solution I will post it here. Thanks for your answers!
    – user885982
    Aug 23, 2011 at 8:17

4 Answers 4

5

As of C# 4, there is optional parameter support:

public void MyMethod(bool arg = false)
{

}

Not sure how this would get called from VB6 though.

Another possible way is to wrap the arguments in another class:

public class PersonSettings
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public Person(PersonSettings settings)
{

}

You then have one argument and can default values in the PersonSettings class as needed.

Of course, your proposed use of the OptionalAttribute should also work for you. Though I think you need to get rid of the overloaded method, or use an interface to only expose one of those methods to COM:

http://social.msdn.microsoft.com/Forums/en/clr/thread/048c0104-20ed-49af-a221-ddadb081989e

5
  • I posted something nigh identical, but upon re-reading it seems the OP knows of this and is actually not asking about implementing optional parameters as expected, but how to get around the problems of overloaded methods imported into VB6. Aug 22, 2011 at 12:14
  • @Mr. Disappointment yeah I pegged the line where the OP mentions variables with default values, but thought they were referring to something else when they mentioned ""out" variables". I'll leave the answer up as I don't believe the OP was aware of optional parameters in C# based on the first line. Aug 22, 2011 at 12:15
  • I dont think I was specifik enough when asking the question (my bad) but there is yet another aggravating factor in this case. when using optional out variables i cant assign default values to my optional attributes. For example: public void CreatePerson(string name, out string insertError = "no error") is not in working C#.
    – user885982
    Aug 22, 2011 at 13:12
  • It seems like [OptionalAttribute] would be the best solution as I´m working with COM and dont have that much access to parts of the system.
    – user885982
    Aug 22, 2011 at 13:18
  • @user885982 that isn't valid C# syntax. out parameters don't really have a concept of default value, usually they are passed in and it's assumed that the method itself is going to write to them and return them. I've put an example of C# 4 optional parameter syntax in my answer. The problem you seem to be having is that COM doesn't support overloading, so if you are trying to support COM you need to avoid overloading completely. Aug 22, 2011 at 13:50
3

C# 4.0 does indeed have optional parameters:

public void MyMethod(string optionalParameter = "optional")
{

}
2
  • The problem with this solution in my case would be when using out parameters as optional. using default value with "out", for example public void CreatePerson(string name, out string insertError = "no error") does not work in C#.
    – user885982
    Aug 22, 2011 at 13:05
  • Note also that the default values are used/fixed at compile time of the caller so won;t play nice with COM interop.
    – Deanna
    Aug 22, 2011 at 21:57
0

I sorted this out by using OptionalAttribute. I think this was the best solution because all the other solutions would have been to timeconsuming and comprehensive. To check if an optional attribute was passed, use: if(attr_optional != Type.Missing) (requires that the optional attribute is of type object)

-2

You can do this:

void Person(string name, int? age)
{
    if (!age.hasValue)
    {
        Console.WriteLine("The nullable integer parameter age wasn't provided with a value ..");
    }
}

You'll have to send "null" instead of an int, though. Empties won't work.

1
  • Im working with a very big system, so rewriting all the method calls would not be an option. Otherwise it would work.
    – user885982
    Aug 22, 2011 at 13:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.