0

I'm trying to use App.config and Configuration Manager for the first time. I wanted to use some values in App.config as default parameters for some methods, but this results in an error (not a compile time constant):

public void ThisDoesntWork(string parameter = ConfigurationManager.AppSettings["SettingName"])
{
    // Error: not a compile time constant.
}

I kind of understand why this is the case, so I found this workaround:

public void ThisWorks(string parameter = "Use App.config")
{
    if(parameter == "Use App.config")
    {
        parameter = ConfigurationManager.AppSettings["SettingName"]
    }
    // Rest of method.
}

It should be noted that this could also be used to make the return value of a static method (or anything that isn't a compile time constant) a default parameter's value.

This feels a little odd to me and I was wondering if this was a code smell. Has anyone used a workaround like this before and run into any issues? What is the best practice in this situation?


EDIT: I don't think my question is a duplicate of any of the 5 questions linked. Additionally, if my question is a duplicate of those 5 questions, are those 5 questions not duplicates of eachother? Anyway, heres why it is different from the 5 linked questions:

Different from "Is it wrong to use any type of parameter to determine behavior?" because this example/question is not looking for the best way to pass data down through multiple functions via parameters, nor is it using it to modify functionality.

Different from "Is it wrong to use any type of parameter to determine behavior?" because the method in my example does not want different functionality for different parameter values

Different from "Is it wrong to use a boolean parameter to determine values?" because the parameter in my example does not create 2 branches of very different code. It appends logic to the beginning of a pice of code and the rest is identical in the 2 branches.

Different from "How to eliminate a if-else block which is used to determine behavior according to a boolean input?" ibid

Different from "Is it better to have one method that takes a bool as a parameter or two methods with different names? [duplicate]" because it is a duplicate of "Is it wrong to use any type of parameter to determine behavior?"


As a result of reading those threads, I do think I found the answer in the Martin Fowler article linked a few times throughout those 5 threads. I don't think any of the answers pointed out this particular section though:

...retain the method with the flag argument, but keep it hidden.

    class Order...
      public Booking regularBook(Customer aCustomer) {
        return hiddenBookImpl(aCustomer, false);
      }
      public Booking premiumBook(Customer aCustomer) {
        return hiddenBookImpl(aCustomer, true);
      }
      private Booking hiddenBookImpl(Customer aCustomer,  boolean isPremium) {...}
}

Applying this idea forces me to use a boolean flag in the ThisWorks method I originally posted:

public void DoThis(string parameter)
{
    OriginalDoThisMethod(parameter, false);
}

public void DoThisWithDefault()
{
    OriginalDoThisMethod(null, true);
}

private void ThisWorks(string parameter, bool useDefaultParameter)
{
    if(useDefaultParameter)
    {
        parameter = ConfigurationManager.AppSettings["SettingName"];
    }
    // Rest of method
}

Again, I don't think any of answers in those threads pointed out this particular implementation, but I only read the top 1 or 2 answers in each thread, and it's pretty a terrible UX if I have to comb through 5 threads of potentially duplicate information to prove I am allowed to ask a question on a website.

marked as duplicate by Robert Harvey Jul 13 at 19:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Voting to re-open this as it is completely unrelated to boolean parameters and thus to the five wrongly claimed duplicates. – David Arno Jul 15 at 9:59