3

I have this If statement and I would like to move them into Case-Switch

Here is my if Stataments:

if (rResponse.ErrorCode[0] == 0x20 && rResponse.ErrorCode[1] == 0x03)
    ErrorMsg = "COMM_FRAME_ERROR";
if (rResponse.ErrorCode[0] == 0x20 && rResponse.ErrorCode[1] == 0x04)
    ErrorMsg = "JAM";
if (rResponse.ErrorCode[0] == 0x20 && rResponse.ErrorCode[1] == 0x05)
    ErrorMsg = "NO_CARD";

How can I do that?

5
  • I think Itay has better explained same way you could use nested switch also. Commented Dec 27, 2012 at 4:51
  • @Pratik in my experience it is better to not next switches. It can make it very hard to read. Since this looks like it would be from a error handling section of the program I would think it best to either do as Itay said or make a "parser" for both bytes I'll post my idea as a answer. Commented Dec 27, 2012 at 4:57
  • @RobertSnyder Agreed Completely. I always avoid use of switch statements, i better take use of Database or any generic function(list or any entity) Commented Dec 27, 2012 at 5:07
  • now that is something I don't know about. I will have to look into that because I just got done doing something similar to what the OP is doing on a project of mine..and my response is similar to my approach to solving it. Could you elaborate more on your thoughts? Commented Dec 27, 2012 at 5:11
  • @RobertSnyder i have posted my thought.It's not what is required for such small problems but to create Entities like enum which are expnadable this may be used. Commented Dec 27, 2012 at 6:23

7 Answers 7

9
if (rResponse.ErrorCode[0] == 0x20) {
  switch(rResponse.ErrorCode[1]) {
    case 0x03:
      ErrorMsg = "COMM_FRAME_ERROR";
      break;
    case 0x04:
      ErrorMsg = "JAM";
      break;
    case 0x05:
      ErrorMsg = "NO_CARD";
      break;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If u want both in switch case:

switch(rResponse.Errorcode[0])
case 0x20:
    switch(rResponse.ErrorCode[1]) {
        case 0x03:
            ErrorMsg = "COMM_FRAME_ERROR";
            break;
        case 0x04:
            ErrorMsg = "JAM";
            break;
        case 0x05:
            ErrorMsg = "NO_CARD";
            break;
    }
    break;

Comments

1

Can omit or change the exception,

if (rResponse.ErrorCode[0] == 0x20)
        {
            switch (rResponse.ErrorCode[1])
            {
                case 0x03:
                    ErrorMsg = "COMM_FRAME_ERROR";
                    break;
                case 0x04:
                    ErrorMsg = "JAM";
                    break;
                case 0x05:
                    ErrorMsg = "NO_CARD";
                    break;
                default:
                    throw new InvalidOperationException();
                    break;
            }
        }

Comments

0
switch(rResponse.Errorcode[0])
{
 case 0x20:
  switch(rResponse.ErrorCode[1]) 
  {
    case 0x03:
        ErrorMsg = "COMM_FRAME_ERROR";
        break;
    case 0x04:
        ErrorMsg = "JAM";
        break;
    case 0x05:
        ErrorMsg = "NO_CARD";
        break;
  }
  break;
}

1 Comment

Mr. Anand, Don't you you think we have the same the answers?
0

You can try something like this..

 switch (rResponse.ErrorCode[0] + rResponse.ErrorCode[1])
       {
        case (0x20 + 0x03):
              ErrorMsg = "COMM_FRAME_ERROR";
              break;
       case (0x20 + 0x04):
              ....
      }

though this may not suite all the situations,

Comments

0

Although with the information provided Itay's response might be best. But if this error portion is on a larger scale then maybe something as follows might be worth a go. (Similar approach, just makes it easy to add on.

private string GetError(byte response1, byte response2)
{
    var ErrorMessage = "";
    switch (response1)
    {
        case 0x20:
            ErrorMessage = ParseCardErrors(response2);
            break;
        case 0x21:
            ErrorMessage = ParseDeviceErrors(response2);
            break;
        default:
            ErrorMessage = "Unknown Error";
    }

    return ErrorMessage;
}

private string ParseCardErrors(byte response)
{
  switch(response)
    {
        case 0x03:
          return "COMM_FRAME_ERROR";
        case 0x04:
          return "JAM";
        case 0x05:
          return "NO_CARD";
        default:
            return "Unknown card Error";
    }
}

private string ParseDeviceErrors(byte response)
{
  switch(response)
    {
        case 0x03:
          return "COMM_FRAME_ERROR";
        case 0x04:
          return "Unplugged";
        case 0x05:
          return "No device";
        default:
            return "Unknown device Error";
    }
}

Comments

0

I might be posting a very diffrent solution this question but it can answer @Robert question:

class Program
{
    //This can be included in Seprate class also
    public enum ErrorMessages
    {
        //Store any Values as Key and Pair
        // Provide and easy way to update answers
        Error1 = 1,
        Error2 = 2,
        Error3 = 3,
        Error4 = 4
    }

    public static void Main()
    {
        ICollection<EnumValueDto> list = EnumValueDto.ConvertEnumToList<ErrorMessages>();

        foreach (var element in list)
        {
            Console.WriteLine(string.Format("Key: {0}; Value: {1}", element.Key, element.Value));
        }
        Console.Read();

        /* OUTPUT:
           Key: 1; Value: Error1
           Key: 2; Value: Error2
           Key: 3; Value: Error3 
           Key: 4; Value: Error4
        */
    }

    public class EnumValueDto
    {
        public int Key { get; set; }

        public string Value { get; set; }

        public static ICollection<EnumValueDto> ConvertEnumToList<T>() where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new Exception("Type given T must be an Enum");
            }

            var result = Enum.GetValues(typeof(T))
                             .Cast<T>()
                             .Select(x => new EnumValueDto
                             {
                                 Key = Convert.ToInt32(x),//values are arbitary here any datatype will work
                                 Value = x.ToString(new CultureInfo("en"))
                             })
                            .Where(err => err.Key==3) //Instead of 3 as key here use Response variable instead
                            .ToList()
                            .AsReadOnly();

            return result;
        }
    }


}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.