-1

Here I have got a method that will return the matched parameter with just string as return type and its working fine ....

    private static string GetSortedParameter(string modelValue)
    {
        string returnValue = null;
        if (modelValue == "UserId")
        {
            returnValue = "UsrID";

        }
        if (modelValue == "Status")
        {
            returnValue = "TransactionStatusTypeName";

        }
        if (modelValue == "ProjectCaseNumber")
        {
            returnValue = "PROJCASE";            
        }

        if (modelValue == "CP")
        {
            returnValue = "CPNumber";
        }
        if (modelValue == "ItemID")
        {
            returnValue = "ItemID";
        }
        if (modelValue == "TypeOfChange")
        {
            returnValue = "TransactionTypeName";
        }
        if (modelValue == "ChangeDescription")
        {
            returnValue = "TransactionTypeDescription";
        }
        if (modelValue == "CreatedOnEnd")
        {
            returnValue = "CreatedDateTime";
        }
        if (modelValue == "UpdatedOnEnd")
        {
            returnValue = "UpdatedDateTime";
        }
        if (modelValue == "Comment")
        {
            returnValue = "Comments";
        }

        return returnValue;

    }

and here I am calling this method

       if (request.Sorts != null && request.Sorts.Count > 0)
        {
            sortField = request.Sorts[0].Member;
            sortDirection = request.Sorts[0].SortDirection.ToString();
        }
         string SortFieldParameter = GetSortedParameter(sortField);

But I want to use enum for this type how can i use enum for this type of matching parameters that will take one value as input parameter and gives matched value .....

would you pls give any idea and any solutions for this one....

I am also looking for any generic solution for this .....

Glory Raj
  • 17,397
  • 27
  • 100
  • 203

2 Answers2

1

If you are not using spaces in your descriptions you can sort of cheat with two Enums and cross casting.

enum ModelValue
{
    UserId = 1,
    Status = 2,
    ProjectCaseNumber = 3,
    CP = 4,
    ItemId = 5,
    TypeOfChange = 6,
    ChangeDescription = 7,
    CreatedOnEnd = 8,
    UpdatedOnEnd = 9,
    Comment = 10
}

 enum SortedParameters {
        CUsrID = 1,
        TransactionStatusTypeName = 2,
        PROJCASE = 3,            
        CPNumber = 4,
        ItemID = 5,
        TransactionTypeName = 6,
        TransactionTypeDescription = 7,
        CreatedDateTime = 8,
        UpdatedDateTime = 9,
        Comments = 10
 }

Then all you need is

string GetSortedParameter(string value) {
  ModelValue modelValue;
  if (Enum.TryParse(value, out modelValue)) {
    return ((SortedParameters)(int)modelValue).ToString();
  }
  throw new ArgumentOutOfRangeException("Not a valid value");
}

Why you want to use Enums is not really clear, or did you want GetSortedParameter to return an Enum?

SortedParameters GetSortedParameter(string value) {
  ModelValue modelValue;
  if (Enum.TryParse(value, out modelValue)) {
    return (SortedParameters)(int)modelValue;
  }
  throw new ArgumentOutOfRangeException("Not a valid value");
}

However, is you just want string to string translation Dictionary<string,string> would be far better.

 static Dictionary<string,string> ModelToSortedParameter = new Dictionary<string,string> {
    { "UserId", "CUsrID" },
    { "Status", "TransactionStatusTypeName" },
    # <and so on...>
 }

Then you just do

    SortFieldParameter = ModelToSortedParameter[sortField]; 

You could implement this in a function with a key check if you want to gracefully handle missing values.

This solution also works just as well for string to Enum

static Dictionary<string,SortedParameters> ModelToSortedParameter = new Dictionary<string,SortedParameters> {
    { "UserId", SortedParameters.CUsrID },
    { "Status", SortedParameters.TransactionStatusTypeName },
    # <and so on...>
 }
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
0

First do this

enum ModelValue
{
    UserId,
    Status,
    ProjectCaseNumber,
    CP,
    ItemId,
    TypeOfChange,
    ChangeDescription,
    CreatedOnEnd,
    UpdatedOnEnd,
    Comment
}

then this

private static string GetSortedParameter(ModelValue value)
{
    switch(value)
    {
        case ModelValue.UserId:
            return "CUsrID";

        case ModelValue.Status:
            return "TransactionStatusTypeName";

        case ModelValue.ProjectCaseNumber:
            return "PROJCASE";            

        case ModelValue.CP:
            return "CPNumber";

        case ModelValue.ItemId:
            return "ItemID";

        case ModelValue.TypeOfChange:
            return "TransactionTypeName";

        case ModelValue.ChangeDescription:
            return  "TransactionTypeDescription";

        case ModelValue.CreatedOnEnd:
            return "CreatedDateTime";

        case ModelValue.UpdatedOnEnd:
            return "UpdatedDateTime";

        case ModelValue.Comment:
            return "Comments";

        default:
            throw new ArgumentOutOfRangeException("value");
    }
}

The switch will perform more quickly than a Dictionary, the amount of code is about the same after you consider the Dictionary instantiation.

If the values are not known until runtime, then use a Dictionary.


If, for some reason, a "Sort" can't have a Member of ModelValue and it has to be a string, you could alter GetStoredParamter like this

private static string GetStoredParameter(string value)
{
    switch(value)
    {
        case "UserId":
            return "CUsrID";

        case "Status":
            return "TransactionStatusTypeName";

        ...
    }
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • how can i call the method at here `if (request.Sorts != null && request.Sorts.Count > 0) { sortField = request.Sorts[0].Member; sortDirection = request.Sorts[0].SortDirection.ToString(); } string SortFieldParameter = GetSortedParameter(sortField);` and I am getting error in switch statement like `UserId` is not available in current context .... – Glory Raj Sep 05 '13 at 14:50
  • @BobVale model value is coming from this method `sortField = request.Sorts[0].Member;` and i need to pass only one string value to this one ...... like this GetSortedParameter(value) – Glory Raj Sep 05 '13 at 15:12
  • @pratapk, I edited to show you how to use a switch/case with strings – Jodrell Sep 05 '13 at 16:31