Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am trying to understand how optional and named parameters are work, so far I have managed to implement these options with my function, but now i have realised that this is not what I want.

Basically I want a function that provides selective values, for instance let say I have function with to optional values:

C#
func(string DatabaseName1="db1",string  DatabaseName2="db2")


So now in my code I can choose one of these option when I am calling the method (I don’t want to override or change their value, the value stay as it is (db1,db2)I would like to choose between these two and select one).

I would really appreciate if someone helps me on this.

Thanks,
Posted
Updated 28-May-12 5:17am
v2

You could probably implement something along the lines of this;

C#
private void doSomething(String db1, String db2)
{
    if (!(String.IsNullOrEmpty(db1)))
    {
        //Do this
    }
    else
    {
        if (!(String.IsNullOrEmpty(db2)))
        {
            //Do this
        }
        else
        {
            throw new InvalidOperationException();
        }
    }
}
 
Share this answer
 
Comments
fjdiewornncalwe 28-May-12 11:58am    
+5. That's how I handle optional args in C#
If these values are fixed, and you don't want to pass other value as argument, why even use them? Why not a single bool?
This is not what optional and named parameters are made for.

First of all read this: http://msdn.microsoft.com/en-us/library/dd264739.aspx[^]

The theory:
If you create a routine (function, procedure, method or that like), you define a formal parameter list in the routine header. These are the names you use in the routine body. In strongly typed languages you also define their desired type. When you call the routine, you pass the actual parameters. The compiler/interpreter has to match/bind them. There are following types of binding:
- order binding: this is common, the first actual parameter is bound to the first formal parameter and so on. If the language supports optional parameters, you can skip them, and the default value will be used. In some languages if you skip one, you have to skip all to the right of it too.
- name binding: the system tries to bind the actual parameter to the formal parameter based on it's name. Thus, in the actual parameter list, you have to specify the name of the parameter you want to feed with the actual parameter
- type binding: the system tries to match and/or set the actual type of the parameters. Automatic type cast might occur.

In c# 4.0 you have all.
 
Share this answer
 
How about employing null and the ??-operator? E.g.
C#
// take one or the other argument.
// If a is null, take b; if both are null, take "...".
public void Func(string a = null, string b = null)
{
    string c = a ?? b ?? "...";
    // do something with c...
}


Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900