Setting the default value of a C# Optional Parameter

27,204

Solution 1

No, you will not be able to make the resource work directly in the default. What you need to do is set the default value to something like null and then do the resource lookup when the parameter has the default value in the body of the method.

Solution 2

One option is to make the default value null and then populate that appropriately:

public void ValidationError(string fieldName, string message = null)
{
    string realMessage = message ?? ValidationMessages.ContactNotFound;
    ...
}

Of course, this only works if you don't want to allow null as a genuine value.

Another potential option would be to have a pre-build step which created a file full of const strings based on the resources; you could then reference those consts. It would be fairly awkward though.

Solution 3

Another option is to split your method into two, and have the one overload call the other, like so:

public void ValidationError(string fieldName)
{ 
    ValidationError(fieldName, ValidationMessages.ContactNotFound);
}

public void ValidationError(string fieldName, string message)
{
    ...
}

This way also enables you to pass null as a value for message in case that is also a valid value for that parameter.

Share:
27,204

Related videos on Youtube

Jaxidian
Author by

Jaxidian

Official Title: Cloud Solutions Architect While never claiming to be an expert, my primary focus is as an architect and developer working with enterprise line-of-business applications running in-part or entirely in Azure. I mentor senior developers and coach teams struggling with cloud/web technologies and agile techniques.

Updated on May 17, 2020

Comments

  • Jaxidian
    Jaxidian almost 4 years

    Whenever I attempt to set the default value of an optional parameter to something in a resource file, I get a compile-time error of

    Default parameter value for 'message' must be a compile-time constant.

    Is there any way that I can change how the resource files work to make this possible?

    public void ValidationError(string fieldName, 
                                string message = ValidationMessages.ContactNotFound)
    

    In this, ValidationMessages is a resource file.

  • Jaxidian
    Jaxidian about 14 years
    That's what I was afraid of. I really like the development-time convenience of it telling the devs what the string will be: jaxidian.org/optparam.jpg I realize that what I'm asking for (compiling the resource file) kills part of the reason it exists, but I'm using it not for localization and dynamic changes but just as a common store for reused strings.
  • Jaxidian
    Jaxidian about 14 years
    That's what I was afraid of. I really like the development-time convenience of it telling the devs what the string will be: jaxidian.org/optparam.jpg I realize that what I'm asking for (compiling the resource file) kills part of the reason it exists, but I'm using it not for localization and dynamic changes but just as a common store for reused strings.