-1

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 this workaround before and run into any issues? What is the best practice in this situation?

How can I use configuration values as a defaults of parameters of my methods without using hacks and creating code smell?

eriyg
  • 99
  • 1
  • 12
  • 4
    Seems like this question is too broad and/or is primarily opinion-based – Andreas Jul 13 '19 at 17:49
  • @Andreas Well then give me your opinion – eriyg Jul 13 '19 at 17:59
  • 1
    Opinion-based question is off-topic in here – Andreas Jul 13 '19 at 18:00
  • @Andreas where ought I ask this question? – eriyg Jul 13 '19 at 18:00
  • Definitely not here. Unless if you have a more specific programming problem that follows the site guidelines – Andreas Jul 13 '19 at 18:01
  • @Andreas can you suggest a website/forum that would be equipped to answer this question? – eriyg Jul 13 '19 at 18:03
  • 2
    Asking for off-site resources is also off-topic in here. You can find those using your favorite search engine – Andreas Jul 13 '19 at 18:03
  • Personally I would be using `null` instead of that string. However, this question is unfortunately asking for peoples opinion, and that's off-topic here on Stack Overflow. The answer will differ based on who is giving it, which means it's not really an answer. You may want to hit the chat forums (Stack Overflow toolbar, rightmost icon, pick the Chat) and then ask in the C# chat room, but again, the answer will depend on who is giving it, so it may or may not help you. – Lasse V. Karlsen Jul 13 '19 at 18:38

1 Answers1

0

Rather than using hacks via magic string/null - you should simply create two overloads of your method.

First one that is parameter-less and uses value from app.config to call the second one (which has required parameter without default value).

Nice and clean and eliminates confusion/ambiguity.

Dusan
  • 5,000
  • 6
  • 41
  • 58
  • [How to Answer](https://stackoverflow.com/help/how-to-answer) strongly recommends only answering well-asked questions. – Andreas Jul 13 '19 at 19:49
  • The question is very well understandable, but sometimes people don't bother even reading it carefully. But, let me translate essence of this question to you: `How can I use configuration values as a defaults of parameters of my methods without using hacks and creating code smell?` – Dusan Jul 13 '19 at 20:01
  • Well if you feel that is the intention, you're welcome to edit the question. However, as it is now, multiple people agreed that it is a primarily opinion-based question (even OP does not deny that) – Andreas Jul 13 '19 at 20:03
  • I agree that this is better. Polymorphism is good. It does seem odd to not use a default value to provide default behavior though. Maybe I'm being too nitpicky. – eriyg Jul 13 '19 at 20:41