Mediator
Mediator

Reputation: 15378

How to turn these two functions into one?

first:

public static String ConvertServerToClientAdapter(String adapterServer)
    {
        string temp = adapterServer.Substring(0, adapterServer.IndexOf("/"));
        switch (temp)
        {
            case "f":
                {
                    return "FA";
                }
            case "g":
                {
                    return "Gi";
                }
            case "s":
                {
                    return "SE";
                }
            case "a":
                {
                    return "ATM";
                }
            default:
                return null;
        }
    }

second:

public static String ConvertClientToServerAdapter(String adapterClient)
{
    switch (adapterClient)
    {
        case "FA":
            {
                return "f";
            }
        case "Gi":
            {
                return "g";
            }
        case "SE":
            {
                return "s";
            }
        case "ATM":
            {
                return "a";
            }
        default:
            return null;
    }
}

Upvotes: 2

Views: 136

Answers (4)

Keith Paul Barrow
Keith Paul Barrow

Reputation: 328

What you have is a map datastructure, but unfortunately .net does not provide one. If you are really worried about doing this sort of thing regulary, you could google for a ready-made map. You can also implement your own: Start with a generic Dictionary and override all the methods to ensure the the "value" side is unique, and add a second indexer property for the "reverse" lookup jdv-Jan de Vaan's solution is close to this!

Upvotes: 0

user180326
user180326

Reputation:

You can store the string pairs in a dictionary. That will reduce the duplication of having two places in the code saying that 'a' is paired with 'ATM'.

For the forward conversion, just index the dictionary. return _dict[adapterServer];.

Edit: maybe it should be TryGetValue if you want to return null for invalid input, rather than throwing exceptions.

For backward, use linq:

 return _dict.Elements.FirstOrDefault(e => e.Value == adapterClient).Key);

You can swap what is forward/backward depending on what is used more often, forward dictionary indexing is faster than linq.

Upvotes: 4

FlyingStreudel
FlyingStreudel

Reputation: 4464

public static String Convert(String input)
{
    if (input.Contains("/"))
        switch (input.Substring(0, adapterServer.IndexOf("/")))
        {
            case "f":
                    return "FA";
            case "g":
                    return "Gi";
            case "s":
                    return "SE";
            case "a":
                return "ATM";
            default:
                return null;
        }
    }
    else 
        switch (adapterClient)
        {
            case "FA":
                    return "f";
            case "Gi":
                    return "g";
            case "SE":
                    return "s";
            case "ATM":
                    return "a";
            default:
                return null;
        }
}

Upvotes: 1

confucius
confucius

Reputation: 13327

    public static String ConvertServerToClientAdapter(String adapterServer)
{

    switch (adapterServer.Substring(0, adapterServer.IndexOf("/")))
    {
        case "f":
            {
                return "FA";
            }
        case "g":
            {
                return "Gi";
            }
        case "s":
            {
                return "SE";
            }
        case "a":
            {
                return "ATM";
            }
        default:
           {
             switch (adapterClient)
{
    case "FA":
        {
            return "f";
        }
    case "Gi":
        {
            return "g";
        }
    case "SE":
        {
            return "s";
        }
    case "ATM":
        {
            return "a";
        }
    default:
        return null;
}
 }
      }
     }

Upvotes: -1

Related Questions