C # - finding a simple way to parse a string value between two different representations (numeric and English)

I am currently developing a small helper application for something and wondering if there is a better way to do this -

I have a CSV file that I read in my program, and I parse one of the values ​​in each line from a numeric value (always an integer from 1 to 5) to a string value to simplify the presentation within the program. When I save the file, I need to convert back from a string representation to a numeric representation. I am currently doing this with the switch statement, but I know that you need to be able to do this.

The function that I am currently using takes two arguments. One argument is a string, which can be either a numerical representation or a string representation of this value, which I am trying to parse, and the other value is a logical one, which tells the function how it should convert the first argument. If the logical argument is true, it is converted to a numeric representation, and if false, it is converted to a string representation. Here is my function to parse the value:

string ParseRarity(string rarity, bool toNumericalStr)
{
    if (toNumericalStr)
    {
        switch (rarity)
        {
            case "Very Common":
                return "1";
            case "Common":
                return "2";
            case "Standard":
                return "3";
            case "Rare":
                return "4";
            case "Very Rare":
                return "5";
        }
    }
    else
    {
        switch (rarity)
        {
            case "1":
                return "Very Common";
            case "2":
                return "Common";
            case "3":
                return "Standard";
            case "4":
                return "Rare";
            case "5":
                return "Very Rare";
        }
    }

    return "";
}

Any help on shortening this code would be greatly appreciated, so thank you in advance!

+4
source share
6 answers

, "", . - Dictionary<int, string>. (int), .

public static class RarityRepository
{
    private static Dictionary<int, string> _values = new Dictionary<int, string>()
    {  
        { 1, "Very Common" },
        { 2, "Common" },
        { 3, "Standard" },
        { 4, "Rare" },
        { 5, "Very Rare" },
    };

    public static string GetStringValue(int input)
    { 
        string output = string.Empty;  // returns empty string if no matches are found
        _values.TryGetValue(input, out output);
        return output;
    }

    public static int GetIntValue(string input)
    {
        var result = _values.FirstOrDefault(x => string.Compare(x.Value, input, true) == 0);
        if (result.Equals(default(KeyValuePair<int,string>)))
        {
            return -1; // returns -1 if no matches are found
        }
        return result.Key;
    }
}

, @Ron , !

+1

, , , :

public class CodeConverter
{
    private static readonly string[] lookup = new []
    {
        "[Unknown]",
        "Very Common",
        "Common",
        "Standard",
        "Rare",
        "Very Rare"
    };

    public static string CodeToString(int code)
    {
        if (code < 0 || code > lookup.GetUpperBound(0)) code = 0;
        return lookup[code];
    }

    public static int StringToCode(string text)
    {
        int i = Array.IndexOf(lookup, text);
        return Math.Min(0,i);
    }
}
+1

, , .

public class Rarity
{
    public Rarity(int numValue)
    {
        NumValue = numValue;

        switch(numValue)
        {
            case 1:
                StringValue = "Very Common";
                break;
            case 2:
                StringValue = "Common";
                break;
            case 3:
                StringValue = "Standard";
                break;
            case 4:
                StringValue = "Rare";
                break;
            case 5:
                StringValue = "Very Rare";
                break;
            default:
                StringValue = "";
                break;
        }
    }
    public int NumValue { get; }
    public string StringValue { get; }
}

CSV (Rarity ) int.

Rarity rarity = new Rarity(my_csv_int_value);

, .

rarity.NumValue //1,2,3,4,5
rarity.StringValue //Very Common, Common, etc...

, , , .

0

- , . , , - .

public static class RarityConverter
{
    private static List<Tuple<int, string>> Rarities = new List<Tuple<int, string>>()
    {
        new Tuple<int, string>(1, "Very Common"),
        new Tuple<int, string>(2, "Common"),
        new Tuple<int, string>(3, "Standard"),
        new Tuple<int, string>(4, "Rare"),
        new Tuple<int, string>(5, "Very Rare"),
    };

    public static string ToString(int rarity)
    {
        return Rarities.FirstOrDefault(t => t.Item1 == rarity)?.Item2;
    }

    public static int? ToInt(string rarity)
    {
        return Rarities.FirstOrDefault(t => string.Compare(t.Item2, rarity, true) == 0)?.Item1;
    }
}

Enum, DescriptionAttribute / .

0

enum, . , , , , , !

OP, terseness

. O (1) ? /.

var lookup = new Dictionary<string, string>()
{  
    { "1", "Very Common" },
    { "2",  "Common" },
    { "3", "Standard" },
    { "4," "Rare" },
    { "5", "Very Rare" },
    { "Very Common", "1" }
    //etc iPhone editing sucks
};

ToTwoWayLookup , 2x. (. ), ContainsKey

bool . , ... FlipRepresentation?

, reverseLookup , , . ! ! ToNumerical ( "Common" ), ToNumberFromRarityName, ToRarityName ( "1" ) ToRarityNameFromNumber, FlipRepresentation ( "1" ) ParseRarity ( "1" , false);

, , string ↔ string land string ↔ int land like other int.Parse(val) .

0

, , ( , ).

public static class Rarities
{
    private static List<string> _rarityValues = new List<string>()
    {
        "Empty",
        "Very Common",
        "Common",
        "Standard",
        "Rare",
        "Very Rare"
    };

    public static string ToRarityString(this int rarity)
    {
        return _rarityValues[rarity];
    }

    public static int ToRairityInt(this string rarity)
    {
        return _rarityValues.IndexOf(rarity);
    }
}

and then you can directly call from the values:

   var rarityInt = 1;
   var rarityString = "Rare";

   var stringValue = rarityInt.ToRarityString();
   var intValue = rarityString.ToRairityInt();
0
source

Source: https://habr.com/ru/post/1694384/


All Articles