Click here to Skip to main content
15,557,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'am having a static method named myStaticMethod. Which is defined as follows:

C#
public static void myStaticMethod(string strInputVal="Default")
        {       
            if ("Is accessing Default Value") // how can i define this condition
            {
                //Do something
            }
            else
            {
                //Do some other task
            }
        }


Now i am able to call the method in different ways as follows:

C#
myStaticMethod(); // will access default value
myStaticMethod("Some Value");// will use the passed value
myStaticMethod("Default"); // Here passing value and default value are same


Here my question is How can i identify whether the method is accessing the default value or the value passed through the method call.



Please note:If i define the condition like;

C#
if (strInputVal == "Default")
    {
       // do operation here
    }



Which is meaning full for all function call expect myStaticMethod("Default"); because in this case the method actually accessing the passed value, but my condition will say it is accessing the default value

What I have tried:

C#
myStaticMethod(); // will access default value
myStaticMethod("Some Value");// will use the passed value
myStaticMethod("Default"); // Here passing value and default value are same
Posted
Updated 18-Feb-16 22:05pm
v2

1 solution

MSDN: (while the quote and link are to VB documentation, the content applies equally to C#)
Determining Whether an Optional Argument Is Present
A procedure cannot detect at run time whether a given argument has been omitted or the calling code has explicitly supplied the default value. If you need to make this distinction, you can set an unlikely value as the default. The following procedure defines the optional parameter office, and tests for its default value, QJZ, to see if it has been omitted in the call:
[^]

What you can do is to make your optional parameters nullable:
C#
public static void SomeStaticMethod(int? intParam1 = null, bool? boolParam1 = null, string stringParam1 = null)
{
    int defaultInt = -1;
    bool defaultBool = false;
    string defaultString = "Default";

    if (intParam1 == null)
    {
        // intParam1 not specified
        // set to default ?
        // throw error ?

    } else if (intParam1 == defaultInt)
    {
        // called with default int value
        // ?
    }

    if (boolParam1 == null)
    {
        // boolParam1 not specified
        // set to default ?
        // throw error ?

    } else if (boolParam1 == defaultBool)
    {
        // called with default bool value
        // ?
    }

    if (stringParam1 == null)
    {
        // stringParam1 not specified
        // set to default ?
        // throw error ?

    } else if (stringParam1 == defaultString)
    {
        // called with default string value
        // ?
    }
}
You'll note that Type 'String (a strange "flavor" of a RefernceType[1]) is Nullable; for the ValueTypes you use, you add the '?' to the Type Name as shown here.

I consider it a reasonable expectation that users of code be aware of the requirements for parameters (calling conventions).

Another strategy is method-overloading where you define a series of methods with the same names and return Types, but with different (required, not optional) parameters. Then, you can be absolutely sure which method overload was called. But, consider the case you had three parameters and you wanted to write a separate method overload for every possible case: none : a b c : ab ac bc : abc ... a lot of work !

[1] Look at Eric Lippert's blog entries, and comments on StackOverflow about the complexity of how Type 'String is managed in .NET
 
Share this answer
 
v3

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