Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Folks,

How to have a method that has a string[] array value with default optional values.

In gridviews most database have the fields ID and DateModified setup for GridView.DataKeyNames.
I have 1 application that have ID and date_modified als columns for GridView.DataKeyNames

In my method I want to do
void MyMethod(string[] datakeyNames = new string[] { "Id", "DateModified"})

How can I set-up default values for a string array in a method?

What I have tried:

All I get from C# compiler is the message:
Default parameter value for 'datakeyNames' must be a compile-time constant
Posted
Updated 2-Jul-19 16:41pm

You cannot define a default value for an array as a parameter, because this way the default value has to be a compile-time constant.
You can however test for a null value, and assign the default value accordingly:
C#
void MyMethod(string[] datakeyNames = null)
{
   datakeyNames = datakeyNames ?? new string[] { "Id", "DateModified" };
   // ...
}
 
Share this answer
 
v2
Comments
TheRealSteveJudge 2-Jul-19 7:43am    
Elegant workaround! 5*
phil.o 2-Jul-19 8:11am    
Thank you :)
Maciej Los 2-Jul-19 8:27am    
5ed!
You can't do it with default parameters (for an array), but you can do it with an overload:

C#
public bool MyMethod()
{
    string[] value = new string[] { "Id", "DataModified" };
    return MyMethod(value);
}

public bool MyMethod(string[] value)
{
    // do something with the string array
    return false;
}


This way, you can call either overload and be okay. If you combine the two answers, you could do something like this:

C#
protected string[] DefaultValues()
{
    return new string[] { "Id", "DataModified" };
}

public bool MyMethod()
{
    return MyMethod(this.DefaultValues());
}

public bool MyMethod(string[] value = null)
{
    value = value ?? this.DefaultValues();
    // do something with the string array
    return false;
}


That way, you cover all of your bases, and still only have to change the default values in one location.
 
Share this answer
 
v4
Comments
TheRealSteveJudge 2-Jul-19 8:14am    
Also good! 5*
Maciej Los 2-Jul-19 8:28am    
5ed!
This is an alternative pattern I sometimes use for handling a mix of optional and default parameters:
private string[] defaultData = new []{"Id", "DataModified"};

// requires C# >= 4.00
// requires Linq
public bool myMethod(bool ensureDefaultDataIsUsed = true, params string[] data)
{
    if (ensureDefaultDataIsUsed)
    {
        data = data.Concat(defaultData).Distinct().ToArray();
    }

    return true;
}
Consider these usage examples:
// no parameters: 'defaultData is used
bool m0 = myMethod();

// single parameter: 'defaultData combines with parameter
bool m1 = myMethod(data: "hello");

// multiple parameters: 'defaultData combines with parameters
bool m2 = myMethod(true, "hello", "goodbye");

// multiple parameters: 'defaultData is not used
bool m3 = myMethod(false, "hello", "goodbye");

// will not compile
bool m4 = myMethod("hello");
What I like about this:

1 keeping default parameters in a variable makes it easy to change what the defaults are

2 all the options are in one method

3 control over whether defaults are used, ignored, or combined with user supplied data

What some might consider the downside: example #m4 illustrates that using an explicitly named parameter for 'ensureDefaultDataIsUsed is required to avoid a clash between a positional and an optional value.
 
Share this answer
 

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