locked
How to avoid using Magic string in C# code. RRS feed

  • Question

  • User-1042970710 posted

    Hi Friends,

    I am working with Arabic applications and utilizing Arabic text off and on.   I want to avoid using magic strings and wanted to replace them with  Enums in C#.   (to make it more readable).

    Can anyone provide me a good example?

    Thanks.

    Tuesday, March 9, 2021 1:19 PM

Answers

  • User303363814 posted

    I'm guessing that you don't really want an enum.  You have just grabbed the enum idea as a 'solution' to help you.  I suspect that a better fit for your request would be to use a Resource file.

    Right-click somewhere in your project and choose 'Add item'  then choose 'Resources File'.

    Once created, double clicking the .resx file will open a designer which lets you give a name and a value to each of your strings.  I have a Resource file called ApiMessages.  It has a resource (string) called CannotCreateEvent which has a value of "Does not have the ClubEventCreate permission"

    In your code you can refer to the string with ResourceFileName.ResourceName In my case that is ApiMessages.CannotCreateEvent

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 10, 2021 2:31 AM

All replies

  • User475983607 posted

    I am working with Arabic applications and utilizing Arabic text off and on.   I want to avoid using magic strings and wanted to replace them with  Enums in C#.   (to make it more readable).

    Can anyone provide me a good example?

    Do you have an actual coding problem?  Have you tried reading the official enumeration documentation?

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum

    Perhaps you really want string constants rather than enumerations?

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants#:~:text=Constants%20are%20declared%20with%20the,and%20arrays%2C%20cannot%20be%20const%20.

    Is there any way you can provide code so we can see where you need help?

    Tuesday, March 9, 2021 1:53 PM
  • User753101303 posted

    Hi,

    If you really have an "off" and "on" value, couldn't it be just a boolean? Maybe also https://www.codeproject.com/Articles/19953/Localizing-NET-Enums if this is a localizarion issue?

    Or give some more context so that we first better iunderstand which kind of sample you need.

    Tuesday, March 9, 2021 2:36 PM
  • User303363814 posted

    I'm guessing that you don't really want an enum.  You have just grabbed the enum idea as a 'solution' to help you.  I suspect that a better fit for your request would be to use a Resource file.

    Right-click somewhere in your project and choose 'Add item'  then choose 'Resources File'.

    Once created, double clicking the .resx file will open a designer which lets you give a name and a value to each of your strings.  I have a Resource file called ApiMessages.  It has a resource (string) called CannotCreateEvent which has a value of "Does not have the ClubEventCreate permission"

    In your code you can refer to the string with ResourceFileName.ResourceName In my case that is ApiMessages.CannotCreateEvent

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 10, 2021 2:31 AM