What is a good place, in OO, to store a string that is used many places?











up vote
1
down vote

favorite












I have a string that is used in a few places.



string portalLoginPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";


I was thinking of creating a static class with a string const to store it.



public static class StringConsts{
public const string portalPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";
}


Is there a cleaner way to do this?



I'm building in ASP.NET Core MVC










share|improve this question
























  • One thing to keep in mind when making this decision is how difficult you're making it to internationalize the application. If the German market opens up to you how much rewriting do you want to do just to use the translated versions of your strings? Is YAGNI good enough here or is it worth at least thinking about it?
    – candied_orange
    22 mins ago

















up vote
1
down vote

favorite












I have a string that is used in a few places.



string portalLoginPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";


I was thinking of creating a static class with a string const to store it.



public static class StringConsts{
public const string portalPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";
}


Is there a cleaner way to do this?



I'm building in ASP.NET Core MVC










share|improve this question
























  • One thing to keep in mind when making this decision is how difficult you're making it to internationalize the application. If the German market opens up to you how much rewriting do you want to do just to use the translated versions of your strings? Is YAGNI good enough here or is it worth at least thinking about it?
    – candied_orange
    22 mins ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a string that is used in a few places.



string portalLoginPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";


I was thinking of creating a static class with a string const to store it.



public static class StringConsts{
public const string portalPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";
}


Is there a cleaner way to do this?



I'm building in ASP.NET Core MVC










share|improve this question















I have a string that is used in a few places.



string portalLoginPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";


I was thinking of creating a static class with a string const to store it.



public static class StringConsts{
public const string portalPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login";
}


Is there a cleaner way to do this?



I'm building in ASP.NET Core MVC







c# object-oriented clean-code strings






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago

























asked 3 hours ago









JohnOsborne

1359




1359












  • One thing to keep in mind when making this decision is how difficult you're making it to internationalize the application. If the German market opens up to you how much rewriting do you want to do just to use the translated versions of your strings? Is YAGNI good enough here or is it worth at least thinking about it?
    – candied_orange
    22 mins ago




















  • One thing to keep in mind when making this decision is how difficult you're making it to internationalize the application. If the German market opens up to you how much rewriting do you want to do just to use the translated versions of your strings? Is YAGNI good enough here or is it worth at least thinking about it?
    – candied_orange
    22 mins ago


















One thing to keep in mind when making this decision is how difficult you're making it to internationalize the application. If the German market opens up to you how much rewriting do you want to do just to use the translated versions of your strings? Is YAGNI good enough here or is it worth at least thinking about it?
– candied_orange
22 mins ago






One thing to keep in mind when making this decision is how difficult you're making it to internationalize the application. If the German market opens up to you how much rewriting do you want to do just to use the translated versions of your strings? Is YAGNI good enough here or is it worth at least thinking about it?
– candied_orange
22 mins ago












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










In C# a string preceded by a $ is an interpolated string.



An interpolated string is a construct that both




  • declares a string literal, and,

  • asks the compiler to interpolate variables names/expressions within the string.


These two cannot be separated: I know of no way to declare the string outside of the context where the compiler will find the variables, and no way to ask for interpolation of an externally stored string in the context of a scope that has the variables of interest.



Thus, there's no way to put a $"{}" type string anywhere but within the code that uses it.





The only way to put the string elsewhere (other than embedded in the code) is to forgo interpolation.  In your case all that would be left is "/Account/Login", leaving you to use the expressions in a string.Format statement.






share|improve this answer






























    up vote
    4
    down vote













    A static class is a good idea. One thing to be careful of is that class getting cluttered with a bunch of strings, then you might have little context of what the string is actually used for.



    One way to solve this is have the context in the name of the class. Instead of StringConsts, maybe call this PortalConsts (Or whatever makes the most sense). This way you can organize your strings by class so it doesn't get disorganized.






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "131"
      };
      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',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fsoftwareengineering.stackexchange.com%2fquestions%2f383059%2fwhat-is-a-good-place-in-oo-to-store-a-string-that-is-used-many-places%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








      up vote
      3
      down vote



      accepted










      In C# a string preceded by a $ is an interpolated string.



      An interpolated string is a construct that both




      • declares a string literal, and,

      • asks the compiler to interpolate variables names/expressions within the string.


      These two cannot be separated: I know of no way to declare the string outside of the context where the compiler will find the variables, and no way to ask for interpolation of an externally stored string in the context of a scope that has the variables of interest.



      Thus, there's no way to put a $"{}" type string anywhere but within the code that uses it.





      The only way to put the string elsewhere (other than embedded in the code) is to forgo interpolation.  In your case all that would be left is "/Account/Login", leaving you to use the expressions in a string.Format statement.






      share|improve this answer



























        up vote
        3
        down vote



        accepted










        In C# a string preceded by a $ is an interpolated string.



        An interpolated string is a construct that both




        • declares a string literal, and,

        • asks the compiler to interpolate variables names/expressions within the string.


        These two cannot be separated: I know of no way to declare the string outside of the context where the compiler will find the variables, and no way to ask for interpolation of an externally stored string in the context of a scope that has the variables of interest.



        Thus, there's no way to put a $"{}" type string anywhere but within the code that uses it.





        The only way to put the string elsewhere (other than embedded in the code) is to forgo interpolation.  In your case all that would be left is "/Account/Login", leaving you to use the expressions in a string.Format statement.






        share|improve this answer

























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          In C# a string preceded by a $ is an interpolated string.



          An interpolated string is a construct that both




          • declares a string literal, and,

          • asks the compiler to interpolate variables names/expressions within the string.


          These two cannot be separated: I know of no way to declare the string outside of the context where the compiler will find the variables, and no way to ask for interpolation of an externally stored string in the context of a scope that has the variables of interest.



          Thus, there's no way to put a $"{}" type string anywhere but within the code that uses it.





          The only way to put the string elsewhere (other than embedded in the code) is to forgo interpolation.  In your case all that would be left is "/Account/Login", leaving you to use the expressions in a string.Format statement.






          share|improve this answer














          In C# a string preceded by a $ is an interpolated string.



          An interpolated string is a construct that both




          • declares a string literal, and,

          • asks the compiler to interpolate variables names/expressions within the string.


          These two cannot be separated: I know of no way to declare the string outside of the context where the compiler will find the variables, and no way to ask for interpolation of an externally stored string in the context of a scope that has the variables of interest.



          Thus, there's no way to put a $"{}" type string anywhere but within the code that uses it.





          The only way to put the string elsewhere (other than embedded in the code) is to forgo interpolation.  In your case all that would be left is "/Account/Login", leaving you to use the expressions in a string.Format statement.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          Erik Eidt

          22.1k43156




          22.1k43156
























              up vote
              4
              down vote













              A static class is a good idea. One thing to be careful of is that class getting cluttered with a bunch of strings, then you might have little context of what the string is actually used for.



              One way to solve this is have the context in the name of the class. Instead of StringConsts, maybe call this PortalConsts (Or whatever makes the most sense). This way you can organize your strings by class so it doesn't get disorganized.






              share|improve this answer

























                up vote
                4
                down vote













                A static class is a good idea. One thing to be careful of is that class getting cluttered with a bunch of strings, then you might have little context of what the string is actually used for.



                One way to solve this is have the context in the name of the class. Instead of StringConsts, maybe call this PortalConsts (Or whatever makes the most sense). This way you can organize your strings by class so it doesn't get disorganized.






                share|improve this answer























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  A static class is a good idea. One thing to be careful of is that class getting cluttered with a bunch of strings, then you might have little context of what the string is actually used for.



                  One way to solve this is have the context in the name of the class. Instead of StringConsts, maybe call this PortalConsts (Or whatever makes the most sense). This way you can organize your strings by class so it doesn't get disorganized.






                  share|improve this answer












                  A static class is a good idea. One thing to be careful of is that class getting cluttered with a bunch of strings, then you might have little context of what the string is actually used for.



                  One way to solve this is have the context in the name of the class. Instead of StringConsts, maybe call this PortalConsts (Or whatever makes the most sense). This way you can organize your strings by class so it doesn't get disorganized.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 3 hours ago









                  JSarwer

                  787




                  787






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Software Engineering Stack Exchange!


                      • 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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2fsoftwareengineering.stackexchange.com%2fquestions%2f383059%2fwhat-is-a-good-place-in-oo-to-store-a-string-that-is-used-many-places%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

                      Willebadessen

                      Ida-Boy-Ed-Garten

                      Residenzschloss Arolsen