1

I would like to clean my code and make it more simple. Now I've got a method which returns and int value.

private int ReturnMyValue(string line)
{
    switch (line)
    {
        case "ST":
            return 1;
        case "KG":
            return 2;
        case "M":
            return 3;
        case "M2":
            return 4;
        (...)
    }
}

Before that, I read an array of strings from a file, so it looks like this:

var myString = "";
var splittedString = myString.Split('|');
var iWantThatValue = ReturnMyValue(splitted[0]); 

Is there a way to have an enum like this:

private enum Value
{
    ST = 1,
    KG = 2
}

and get rid of the method somehow ?

I want my iWantThatValue to be 1 when splitted[0] equals "ST".

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Alice
  • 173
  • 1
  • 15

6 Answers6

3

You can parse a string to an enum e.g.

public enum Value
{
    ST = 1, // start at 1
    KG,
    M,
    M2
}

var value = (Value)Enum.Parse(typeof(Value), "KG");

And to get the integer value...

var intValue = (int)val;
phuzi
  • 12,078
  • 3
  • 26
  • 50
1

Use Enum.TryParse https://learn.microsoft.com/en-us/dotnet/api/system.enum.tryparse?redirectedfrom=MSDN&view=netframework-4.7.2#System_Enum_TryParse__1_System_String___0__

if(Enum.TryParse<Value>(splitted[0], out var iWantThatValue)){...}
wertzui
  • 5,148
  • 3
  • 31
  • 51
0

I am unsure if Enums are the right tool here. Enumerators are compile time constants. If you want something that does not change at runtime, readonly is another option. I mean what if you need to do localisation for those (user input) strings? What if you want the possible values to change at each loadup? Enumerators can not support that.

Normally I prefer to replace switch/case statements with Collections. For your specific case, a string[] could be enough: You use the line as value and the return value as Index. But you can go more fancier with a Dictionary or something like that.

Your case is a toss up as we do not know the surrounding requirements.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

You can convert from enum to Int with Convert.ToInt32

Example: https://rextester.com/ETB25701

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine(Convert.ToInt32(Value.ST));
        }

    }

    public enum Value
    {
        ST = 1,
        KG = 2
    }
}
osiris85
  • 196
  • 7
0

Your method can be boiled down to:

private int ReturnMyValue(string line)
{
    return intValue.ContainsKey(line)
        ? intValue[line]
        : -1; //Or any other default value
}

by using a Dictionary/HashMap.

Translate your switch-cases to entries in your intValue map:

{
    {"ST", 1},
    {"KG", 2},
    .
    .
}
displayName
  • 13,888
  • 8
  • 60
  • 75
0

If you can change your design a bit, Enum.Parse can be a great solution.

  1. Change your enum to a [Flags] enum, with the values as powers of two (0x01, 0x02, 0x04, etc.).
  2. Change your string from using a pipe character (|) to using a comma (,)

The code ends up being:

[Flags]
public enum Value
{
    ST = 0x01,
    KG = 0x02,
    M = 0x04,
    M2 = 0x08,
    //etc
}

and then:

    const string valueChoices = "ST,M2";
    Value values = (Value) Enum.Parse(typeof(Value), valueChoices);
    Debug.WriteLine(values);

If you look at the value of values above it will be ST | M2, or, as an int: 9. If you look in the Output window, you will see: ST, M2

Flydog57
  • 6,851
  • 2
  • 17
  • 18