Associate two sets of values

Posted by PJW on Stack Overflow See other posts from Stack Overflow or by PJW
Published on 2013-10-29T09:43:49Z Indexed on 2013/10/29 9:53 UTC
Read the original article Hit count: 117

Filed under:

I have the following code -

public static int GetViewLevel(string viewLevelDesc)
{
    try
    {
        switch (viewLevelDesc)
        {
            case "All":
                return 0;

            case "Office":
                return 10;

            case "Manager":
                return 50;

            default:
                throw new Exception("Invalid View Level Description");
        }
    }
    catch (Exception eX)
    {
        throw new Exception("Action: GetViewLevel()" + Environment.NewLine + eX.Message);
    }
}

public static string GetViewLevelDescription(int viewLevel)
{
    try
    {
        switch (viewLevel)
        {
            case 0:
                return "All";

            case 10:
                return "Office";

            case 50:
                return "Manager";

            default:
                throw new Exception("Invalid View Level Description");
        }
    }
    catch (Exception eX)
    {
        throw new Exception("Action: GetViewLevelDescription()" + Environment.NewLine + eX.Message);
    }
}

The two static Methods enable me to either get an int ViewLevel from a string ViewLevelDesc or vice versa. I'm sure the way I have done this is far more cumbersome than it needs to be, and I'm looking for some advice how to achieve the same objective but more concisely. The list of int / string pairs will increase significantly. The ones in the above code are just the first three I intend to use.

© Stack Overflow or respective owner

Related posts about c#