Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Keeping constant strings

Keeping constant strings

Scheduled Pinned Locked Moved C#
csharphtmlcomsysadmintutorial
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #1

    I am looking for a good method to keep my constants. Currently I have created a static class like this

    public static class ConstantMessages
    {
    public static string ServerCommunicationFailure = "Message";
    public static string AuthenticationFailure = "Message";
    }

    I have many fields like this. Is this a good approach ? Or is there any alternative better approach for this ? I will be using the above class like the following

    try
    {
    // Doing something that communicates with remote server
    }
    catch(Exception ex)
    {
    throw new CommunicationException(ConstantMessages.ServerCommunicationFailure);
    }

    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

    C K P 3 Replies Last reply
    0
    • N N a v a n e e t h

      I am looking for a good method to keep my constants. Currently I have created a static class like this

      public static class ConstantMessages
      {
      public static string ServerCommunicationFailure = "Message";
      public static string AuthenticationFailure = "Message";
      }

      I have many fields like this. Is this a good approach ? Or is there any alternative better approach for this ? I will be using the above class like the following

      try
      {
      // Doing something that communicates with remote server
      }
      catch(Exception ex)
      {
      throw new CommunicationException(ConstantMessages.ServerCommunicationFailure);
      }

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I'd just use a settings class, that's what they are for. But, this does work just fine.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      N 1 Reply Last reply
      0
      • C Christian Graus

        I'd just use a settings class, that's what they are for. But, this does work just fine.

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Christian Graus wrote:

        But, this does work just fine.

        Thanks. Yes this works fine, but it tough to maintain. Each time a variable and it's value has to be added. Settings class looks good, I will give a try. There is going to be many settings which will be kept in this settings file, do you think it will slow down the accessing ?

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        1 Reply Last reply
        0
        • N N a v a n e e t h

          I am looking for a good method to keep my constants. Currently I have created a static class like this

          public static class ConstantMessages
          {
          public static string ServerCommunicationFailure = "Message";
          public static string AuthenticationFailure = "Message";
          }

          I have many fields like this. Is this a good approach ? Or is there any alternative better approach for this ? I will be using the above class like the following

          try
          {
          // Doing something that communicates with remote server
          }
          catch(Exception ex)
          {
          throw new CommunicationException(ConstantMessages.ServerCommunicationFailure);
          }

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          K Offline
          K Offline
          Kalvin Work
          wrote on last edited by
          #4

          Looking at this example it looks like there is a contradiction of ideas here. The class is called ConstantMessages, but the strings inside the class are being set up so that the application can modify them. If you want constant messages why not use: public const string ServerCommunicationFailure = "Message"; This should give a performance benefit. If I am wrong, someone please correct me so I can continue to learn. Kalvin

          N 1 Reply Last reply
          0
          • K Kalvin Work

            Looking at this example it looks like there is a contradiction of ideas here. The class is called ConstantMessages, but the strings inside the class are being set up so that the application can modify them. If you want constant messages why not use: public const string ServerCommunicationFailure = "Message"; This should give a performance benefit. If I am wrong, someone please correct me so I can continue to learn. Kalvin

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Yes you are correct. I wrote that to explain the problem. This is not used exactly as it is.

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            R 1 Reply Last reply
            0
            • N N a v a n e e t h

              I am looking for a good method to keep my constants. Currently I have created a static class like this

              public static class ConstantMessages
              {
              public static string ServerCommunicationFailure = "Message";
              public static string AuthenticationFailure = "Message";
              }

              I have many fields like this. Is this a good approach ? Or is there any alternative better approach for this ? I will be using the above class like the following

              try
              {
              // Doing something that communicates with remote server
              }
              catch(Exception ex)
              {
              throw new CommunicationException(ConstantMessages.ServerCommunicationFailure);
              }

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Use an enum with Description attributes?

              enum Messages
              {
              [Description("Message")]
              ServerCommunicationFailure
              ,
              [Description("Message")]
              AuthenticationFailure
              }

              N 1 Reply Last reply
              0
              • N N a v a n e e t h

                Yes you are correct. I wrote that to explain the problem. This is not used exactly as it is.

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                R Offline
                R Offline
                Roger Alsing 0
                wrote on last edited by
                #7

                However, the recomended way by MS is to use static readonly instead of const. because of versioning issues. if you use a const, every consumer that use your const will have the const value compiled down into the consumer code. while if you access a readonly field, the consumer code will always get the values from the owner class. in short, if you use consts and then ship a new version of the dll containing your consts, those changes will not affect already compiled code. while if you use readonly fields, it will affect already compiled consumers..

                My Blog

                N P 2 Replies Last reply
                0
                • R Roger Alsing 0

                  However, the recomended way by MS is to use static readonly instead of const. because of versioning issues. if you use a const, every consumer that use your const will have the const value compiled down into the consumer code. while if you access a readonly field, the consumer code will always get the values from the owner class. in short, if you use consts and then ship a new version of the dll containing your consts, those changes will not affect already compiled code. while if you use readonly fields, it will affect already compiled consumers..

                  My Blog

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  Very valid points. Thanks for that Roger

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Use an enum with Description attributes?

                    enum Messages
                    {
                    [Description("Message")]
                    ServerCommunicationFailure
                    ,
                    [Description("Message")]
                    AuthenticationFailure
                    }

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    Thanks. I will take a look into that.

                    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                    1 Reply Last reply
                    0
                    • R Roger Alsing 0

                      However, the recomended way by MS is to use static readonly instead of const. because of versioning issues. if you use a const, every consumer that use your const will have the const value compiled down into the consumer code. while if you access a readonly field, the consumer code will always get the values from the owner class. in short, if you use consts and then ship a new version of the dll containing your consts, those changes will not affect already compiled code. while if you use readonly fields, it will affect already compiled consumers..

                      My Blog

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      What?! :omg: I've never seen that documented anywhere. But I just tried it and it seems to be true. That doesn't make sense. Edit: It's true of enum members as well. I just looked through the specs and saw no mention of this functionality, but I see now that it does make (some) sense. Among other things, const values are valid in case labels, and case labels must be unique. Changing a const value (or enum) could make a previously-compiled switch invalid. So, yeah, I think a best practice is; unless the value is an actual constant value like Pi, don't use public const, but use public readonly instead. Using private const, may not be as bad, but the next developer to come along may make your privates public. So use const sparingly. Thanks for the tip.

                      modified on Monday, May 12, 2008 11:46 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups