-3

I'm looking to compare a value against a list and then set another variable based on this comparison.

My list is

1 = Red
4 = Blue
13 = Green

I could have a series of if statements but with a large list this looks messy

if (Type == 1)
{
    Name = "Red";
}
else if (Type == 4)
{
    Name = "Blue";
}
else if (Type == 13)
{
    Name = "Green";
}

What's an efficient and tidy way to achieve the same?

Aristos
  • 66,005
  • 16
  • 114
  • 150
Josh Cooper
  • 191
  • 1
  • 2
  • 14
  • 3
    Use [`switch`](https://msdn.microsoft.com/library/06tc147t(v=vs.120)) instead. – MakePeaceGreatAgain Jun 19 '18 at 10:19
  • 5
    Do you need to have Type to be an integer? I would recommend an enum. This would also allow to assign the name from the enum value directly. – ViRuSTriNiTy Jun 19 '18 at 10:19
  • Also, suggesting you to create an enum type for these types. it will be more elegant. – Venkataraman R Jun 19 '18 at 10:19
  • 2
    Organize correspondent values in a *dictionary* – Dmitry Bychenko Jun 19 '18 at 10:19
  • You could also investigate the [EnumConverter](https://msdn.microsoft.com/en-us/library/system.componentmodel.enumconverter(v=vs.110).aspx) or TypeConverter : https://stackoverflow.com/questions/1117961/c-how-to-use-a-type-converter-to-localize-enums – PaulF Jun 19 '18 at 10:57
  • 2
    @ViRuSTriNiTy I think you're taking it too personal. The question's title says 'More elegant', don't you think that the answer you suggested (and which works) is more 'quick and dirty' than elegant (although elegant is really a matter of personal choice) – vc 74 Jun 19 '18 at 11:00
  • @vc74 The irony being, that I like enums and I'd consider using them in conjunction with the answer from Pranay. – DavidG Jun 19 '18 at 11:15
  • @DavidG Same here. As I commented in Pranay's answer, enum to declare the colors you handle which definitely does not mean the enum has to be in charge of the serialization of such values. I find a dic easier to maintain though, especially if colors are changed regularly. – vc 74 Jun 19 '18 at 11:20

4 Answers4

4

Make use of dictionary store your values, it will make clean code with less line of code and readable too

Dictionary<int, string> data = new Dictionary<int, string>
{
    { 1, "Red" }, 
    { 4, "Blue" },
    { 13, "Green" }
};

string test;
if (data.TryGetValue(1, out test)) // Returns true.
{
   Console.WriteLine(test); 
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • But this way you always need to get the string representation out of the dictionary first. Using an enum would remove this need. – ViRuSTriNiTy Jun 19 '18 at 10:37
  • 3
    @ViRuSTriNiTy How is "getting the string out of the dictionary" any worse then "ToString on an enum value"? – DavidG Jun 19 '18 at 10:39
  • 3
    @ViRuSTriNiTy Yes but with this implementation, you can make the map dynamic and store it in a config file, db... If you want to add a new color with an enum, you have to rebuild / redeploy the app – vc 74 Jun 19 '18 at 10:39
  • @DavidG @vc74 Sure one can nitpick on any solution. I was just pointing out that you should or better need to call `TryGetValue()` to avoid dangers like adding new undefined items to the dictionary for example. With an enum alone you don't have that danger but surely other disadvantages appear. – ViRuSTriNiTy Jun 19 '18 at 10:44
  • 1
    @ViRuSTriNiTy I'm not saying your solution is better/worse, as you said each solution has its pros+cons. But to me I seems cleaner to separate the collection of colors you support (possibly in an enum) from the way you serialize/deserialize these colors. – vc 74 Jun 19 '18 at 10:50
  • @Pranay, not a big fan of declaration expressions yet? ;) – vc 74 Jun 19 '18 at 10:56
2

I think you need to use switch here:

switch (Type)
  {
      case 1:
          Name = "Red";
          break;
      case 4:
          Name = "Blue";
          break;
      case 13:
          Name = "Green";
          break;
  }
Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
  • This isn't more elegant. – ViRuSTriNiTy Jun 19 '18 at 10:21
  • 4
    @ViRuSTriNiTy I'd say it's far better than a bunch of `if` statements. – DavidG Jun 19 '18 at 10:23
  • @DavidG Although i accept it i will never understand this reasoning. In my eyes using switch is uglifing the code. – ViRuSTriNiTy Jun 19 '18 at 10:29
  • 1
    @ViRuSTriNiTy It's probably because of all those `break`s, having a "breakless" switch construct in C# would be quite handy (I don't think I ever needed break in C#) – vc 74 Jun 19 '18 at 10:31
  • 2
    @ViRuSTriNiTy I agree that it can get ugly, especially with the `break` statements. However, put this in a utility function and `return` the values instead. That makes for much cleaner code imo. – DavidG Jun 19 '18 at 10:36
1

Use a switch statement insted

  switch (Type)
  {
      case 1:
          Name = "Red";
          break;
      case 4:
          Name = "Blue";
          break;
      case 13:
          Name = "Green";
          break;
  }
0
    class Program
    {
        public enum EnumDisplayStatus
        {
            Red = 1,
            Blue = 2,
            Yellow = 3,
            Orange = 4
        }
        static void Main()
        {
            int value = 3;
            EnumDisplayStatus enumDisplayStatus = (EnumDisplayStatus)value;
            string color = enumDisplayStatus.ToString();
            Console.Write(color);
        }
    }

Use enum and get string with this way that I showed.

4b0
  • 21,981
  • 30
  • 95
  • 142
Eyup Can ARSLAN
  • 692
  • 9
  • 25
  • The problem with relying on the enum ToString() method has just been discussed - there are problems if localization is needed & also problems if formatting (eg spaces in colors like "Light Blue"). These can be avoided with providing a [TypeConverter](https://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter(v=vs.110).aspx) or [EnumConverter](https://msdn.microsoft.com/en-us/library/system.componentmodel.enumconverter(v=vs.110).aspx) or simply overriding the ToString method. – PaulF Jun 19 '18 at 11:07