-1

I'm not sure if this is possible at all or if I should use something else then const.

I have about 50 const strings, with all a unique value. The program will read a string which is one of the const names.

what I want:

private const string AllPlace1 = "1355,-203,-4,0.002551732,0.705572185,0.708626711,0.003092848,-1,0,0,0";
private const string MoveDown1 = "1355,-203,-24,0.002551735,0.705572183,0.708626713,0.00309285,-1,0,0,0";
private const string Free1 = "1355,-108,-24,0.002551719,0.705572218,0.708626678,0.003092837,-1,0,0,0";

When the string "AllPlace1" is given the system should print out the value of const AllPlace1.

of course, I can write something like this for all possibilities, but that is not what I want to do for 50 possible values.

if (args[3] == "AllPlace1")
    WriteLine(AllPlace1);
else if (args[3] == "MoveDown1")
    WriteLine(MoveDown1);
etc
wes
  • 379
  • 3
  • 15
  • 3
    Try `nameof(AllPlace1)`. Although a better design would be to create a dictionary with names and values – Panagiotis Kanavos May 23 '19 at 12:40
  • 9
    Maybe you want a `Dictionary` instead of a bunch of constants? – David May 23 '19 at 12:41
  • You can use reflection with `type.GetFields` within a function to get what you need. – Trevor May 23 '19 at 12:42
  • Maybe this works `Console.WriteLine(this.GetType().GetField("yourstring").GetValue(this)) ` or depending on your classes. –  May 23 '19 at 12:46
  • per @David comment, I think this link can be of use to you: https://www.tutorialsteacher.com/csharp/csharp-dictionary – Drewskis May 23 '19 at 12:48
  • There is also [ReadOnlyDictionary](https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.readonlydictionary-2?view=netframework-4.8) if you are concerned with values changing. You haven't quite explained all the context, but I would caution against getting too caught up in trying to force the compiler to prevent developer mistakes instead of following "reasonable" practices (whatever that means). – BurnsBA May 23 '19 at 12:50

1 Answers1

8

You could use a dictionary instead:

static readonly Dictionary<string, string> NameValueMapper = new Dictionary<string, string>{
    { "AllPlace1", "1355,-203,-4,0.002551732,0.705572185,0.708626711,0.003092848,-1,0,0,0"},
    { "MoveDown1", "1355,-203,-24,0.002551735,0.705572183,0.708626713,0.00309285,-1,0,0,0"},
    { "Free1"    , "1355,-108,-24,0.002551719,0.705572218,0.708626678,0.003092837,-1,0,0,0"},
};

...

if (NameValueMapper.TryGetValue(args[3], out string value))
{
    WriteLine(value);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Now why would someone use a `Dictionary` to hold their constants, doing this they no longer *are* constants... Besides, now anywhere these are used would all have to be redone... – Trevor May 23 '19 at 13:00
  • `I would never go the reflection way here to find out if there is a private constant with a given name` and why? How else would you go about getting a constants value (given the name) without reflection? – Trevor May 23 '19 at 13:04
  • @Çöđěxěŕ: Btw, you could still add the constants to this dictionary. So for example: `...new Dictionary{{"AllPlace1", AllPlace1}, ...}` – Tim Schmelter May 23 '19 at 13:23
  • yes you can still add the constants to the dictionary, but anywhere these constants *were* used would no longer be available and code would have to be refactored, pick your poison I guess. Also I do agree this approach wasn't correct for the OP, but current implementation the OP was doing, reflection would be minimal without changing any constants etc... – Trevor May 23 '19 at 13:29
  • 1
    @Çöđěxěŕ: Why? The constants would still be available and every code that existed and used them will still work. Only the code(which is new anyway) where he determines the value from the name in the args-parameter would use this dictionary. But the more code would use these constants directly, the more poison he would take. – Tim Schmelter May 23 '19 at 13:31
  • Yes, I am thinking the old way, throwing the values into a dictionary; not adding the constants to the dictionary. You are correct if the constants were added to the dictionary. – Trevor May 23 '19 at 13:33