C#
C#

help

Root Question Message

talha
talha12/6/2022
❔ Method call based on config file

Based on a value in a config file, I want my program to call one of 3 methods who have different parameters but overall do the same thing. What's the best way to handle this? Overloading the method and adding an additional parameter?
Angius
Angius12/6/2022
Honestly, probably just a switch or if-else
talha
talha12/6/2022
So I would just rewrite this method for the other config values regularly and call them depending on which value is used?
Angius
Angius12/6/2022
Based on some value in the config file, you said?
talha
talha12/6/2022
Yes
Angius
Angius12/6/2022
So
switch (cfg.Whatever) {
    case "one":
        MethodOne(1, 2, "false");
        break;
    case "two":
        MethodTwo("hello", true, 9.19);
        break;
    case "three":
        MethodThree(new Person(), 67, 'a');
        break;
    default:
        throw new OutOfRangeException();
}
talha
talha12/6/2022
Seems like a good approach I suppose, thanks
Atakan / Cracker
Atakan / Cracker12/6/2022
And you can use IOptionsMonitor to manage changes in config file during runtime
Atakan / Cracker
Atakan / Cracker12/6/2022
In case you need that, I was excited when first see this and use in my app https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy