Read data from app.config and use it across multiple classes in c#












0















Hi I am reading the values from app.config. I have different sections as below.



<TEST>
<add key="key" value="value1" />
</TEST>

<DEV>
<add key="key" value="value1" />
</DEV>


I am accessing these values in my class files as below.



protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;


If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.










share|improve this question





























    0















    Hi I am reading the values from app.config. I have different sections as below.



    <TEST>
    <add key="key" value="value1" />
    </TEST>

    <DEV>
    <add key="key" value="value1" />
    </DEV>


    I am accessing these values in my class files as below.



    protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;


    If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.










    share|improve this question



























      0












      0








      0








      Hi I am reading the values from app.config. I have different sections as below.



      <TEST>
      <add key="key" value="value1" />
      </TEST>

      <DEV>
      <add key="key" value="value1" />
      </DEV>


      I am accessing these values in my class files as below.



      protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;


      If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.










      share|improve this question
















      Hi I am reading the values from app.config. I have different sections as below.



      <TEST>
      <add key="key" value="value1" />
      </TEST>

      <DEV>
      <add key="key" value="value1" />
      </DEV>


      I am accessing these values in my class files as below.



      protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;


      If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.







      c# .net app-config






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 13:35









      CodeConstruct

      175114




      175114










      asked Nov 23 '18 at 9:19









      NiranjanNiranjan

      20719




      20719
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Access Modifiers



          make a public static string containing the value you want.



          Then just refer to that instead of directly inserting the value.



          And whenever you want to change it, you just have to change the value of the static in that one place.



          public static string Section = "DEV";


          and then you can just insert this, so you only have to change the value of Section when you want to switch.



          protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;





          share|improve this answer


























          • Okay thanks. In which file I need to public?

            – Niranjan
            Nov 23 '18 at 9:36











          • any file as long as it's in the same assembly. more info on the link added :)

            – Dennis Vanhout
            Nov 23 '18 at 9:40











          • @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

            – Dennis Vanhout
            Nov 23 '18 at 9:47






          • 1





            Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

            – User_CO
            Nov 23 '18 at 9:50



















          0














          Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443738%2fread-data-from-app-config-and-use-it-across-multiple-classes-in-c-sharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Access Modifiers



            make a public static string containing the value you want.



            Then just refer to that instead of directly inserting the value.



            And whenever you want to change it, you just have to change the value of the static in that one place.



            public static string Section = "DEV";


            and then you can just insert this, so you only have to change the value of Section when you want to switch.



            protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;





            share|improve this answer


























            • Okay thanks. In which file I need to public?

              – Niranjan
              Nov 23 '18 at 9:36











            • any file as long as it's in the same assembly. more info on the link added :)

              – Dennis Vanhout
              Nov 23 '18 at 9:40











            • @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

              – Dennis Vanhout
              Nov 23 '18 at 9:47






            • 1





              Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

              – User_CO
              Nov 23 '18 at 9:50
















            1














            Access Modifiers



            make a public static string containing the value you want.



            Then just refer to that instead of directly inserting the value.



            And whenever you want to change it, you just have to change the value of the static in that one place.



            public static string Section = "DEV";


            and then you can just insert this, so you only have to change the value of Section when you want to switch.



            protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;





            share|improve this answer


























            • Okay thanks. In which file I need to public?

              – Niranjan
              Nov 23 '18 at 9:36











            • any file as long as it's in the same assembly. more info on the link added :)

              – Dennis Vanhout
              Nov 23 '18 at 9:40











            • @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

              – Dennis Vanhout
              Nov 23 '18 at 9:47






            • 1





              Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

              – User_CO
              Nov 23 '18 at 9:50














            1












            1








            1







            Access Modifiers



            make a public static string containing the value you want.



            Then just refer to that instead of directly inserting the value.



            And whenever you want to change it, you just have to change the value of the static in that one place.



            public static string Section = "DEV";


            and then you can just insert this, so you only have to change the value of Section when you want to switch.



            protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;





            share|improve this answer















            Access Modifiers



            make a public static string containing the value you want.



            Then just refer to that instead of directly inserting the value.



            And whenever you want to change it, you just have to change the value of the static in that one place.



            public static string Section = "DEV";


            and then you can just insert this, so you only have to change the value of Section when you want to switch.



            protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 9:38

























            answered Nov 23 '18 at 9:34









            Dennis VanhoutDennis Vanhout

            917




            917













            • Okay thanks. In which file I need to public?

              – Niranjan
              Nov 23 '18 at 9:36











            • any file as long as it's in the same assembly. more info on the link added :)

              – Dennis Vanhout
              Nov 23 '18 at 9:40











            • @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

              – Dennis Vanhout
              Nov 23 '18 at 9:47






            • 1





              Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

              – User_CO
              Nov 23 '18 at 9:50



















            • Okay thanks. In which file I need to public?

              – Niranjan
              Nov 23 '18 at 9:36











            • any file as long as it's in the same assembly. more info on the link added :)

              – Dennis Vanhout
              Nov 23 '18 at 9:40











            • @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

              – Dennis Vanhout
              Nov 23 '18 at 9:47






            • 1





              Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

              – User_CO
              Nov 23 '18 at 9:50

















            Okay thanks. In which file I need to public?

            – Niranjan
            Nov 23 '18 at 9:36





            Okay thanks. In which file I need to public?

            – Niranjan
            Nov 23 '18 at 9:36













            any file as long as it's in the same assembly. more info on the link added :)

            – Dennis Vanhout
            Nov 23 '18 at 9:40





            any file as long as it's in the same assembly. more info on the link added :)

            – Dennis Vanhout
            Nov 23 '18 at 9:40













            @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

            – Dennis Vanhout
            Nov 23 '18 at 9:47





            @Niranjan if it worked, please mark as answer, if not, feel free to ask :)

            – Dennis Vanhout
            Nov 23 '18 at 9:47




            1




            1





            Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

            – User_CO
            Nov 23 '18 at 9:50





            Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.

            – User_CO
            Nov 23 '18 at 9:50













            0














            Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode






            share|improve this answer




























              0














              Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode






              share|improve this answer


























                0












                0








                0







                Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode






                share|improve this answer













                Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 10:08









                CodeConstructCodeConstruct

                175114




                175114






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443738%2fread-data-from-app-config-and-use-it-across-multiple-classes-in-c-sharp%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?