none
C# - switch case with readonly members RRS feed

  • Question

  • Hello,

    I am using a provided API in C#, that I cannot change. The error codes in the API are saved as follows.

    public class ClientErrors
    {
        public static readonly CodeMsgPair AlreadyConnected = new CodeMsgPair(501, "Already Connected.");
        public static readonly CodeMsgPair CONNECT_FAIL = new CodeMsgPair(502, "Couldn't connect.");
        public static readonly CodeMsgPair NOT_CONNECTED = new CodeMsgPair(504, "Not connected");
    }
    
    public class CodeMsgPair
    {
        private int code;
        private string message;
    
        public CodeMsgPair(int code, string message)
        {
            this.code = code;
            this.message = message;
        }
    
        public int Code
        {
            get { return code; } 
        }
    
        public string Message
        {
            get { return message; }
        }
    }


    Now on an error the API calls my callback method.

    void error(int id, int errorCode, string errorMsg)
    {
    }


    I would like to have a switch-case to differentiate between the errors and throw a specified Exception. Something like this.

    public virtual void error(int id, int errorCode, string errorMsg)
    {
        switch (errorCode)
        {
            case ClientErrors.NOT_CONNECTED.Code:
                {
                    throw new ConnectionException(errorMsg);
                }
        }
    }

    The problem is, that the case statement expects a const value. But I do now really want to write my own list of const for the error codes as they can change.

    Is there a way how I can use the defined error codes?

    Many thanks in advance!

    Friday, November 15, 2013 6:12 AM

Answers

  • case has the standing requirement that whatever you use in the case condition must be a constant. The values must be know at compile time.

    readonly is for values that are set once at runtime - where const would be to restrictive.
    The most common case for readonly is that a readonly value is set via the constructor. However Properties with Private set accessor have mostly replaced readonly for that case (using readonly Property instead of Readonly value).

    If you instead use a if, you would not need to havre constants. if can compare varriables as easily as cosntants. Here is an idea:

    bool Detected = false;
    
    if(!Detected && errorCode == NOT_CONNECTED.Code){
      Detected = true;
      //Other code
    }
    
    if(!Detected && errorCode == AlreadyConnected.Code){
      Detected = true;
      //Other code
    }
    
    //Execute code that has to run for all errors
    if(Detected){
      
    }
    
    //Code here is executed regardless of error


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Proposed as answer by Shruti Raturi Friday, November 15, 2013 8:35 AM
    • Marked as answer by mission2010 Friday, November 15, 2013 3:42 PM
    Friday, November 15, 2013 7:27 AM

All replies

  • one suggestion I can give is like having the error codes only as constants. and you can use these constants in declaring your CodeMsgPairs.

    In switch statements you can directly r

    public const int alreadyconntected = 500;
    
    switch (errorCode)
    {
    case alreadyconnected:
    {
    // do what you want here
    break;}
    }

    efer to the constants than


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, November 15, 2013 6:21 AM
  • case has the standing requirement that whatever you use in the case condition must be a constant. The values must be know at compile time.

    readonly is for values that are set once at runtime - where const would be to restrictive.
    The most common case for readonly is that a readonly value is set via the constructor. However Properties with Private set accessor have mostly replaced readonly for that case (using readonly Property instead of Readonly value).

    If you instead use a if, you would not need to havre constants. if can compare varriables as easily as cosntants. Here is an idea:

    bool Detected = false;
    
    if(!Detected && errorCode == NOT_CONNECTED.Code){
      Detected = true;
      //Other code
    }
    
    if(!Detected && errorCode == AlreadyConnected.Code){
      Detected = true;
      //Other code
    }
    
    //Execute code that has to run for all errors
    if(Detected){
      
    }
    
    //Code here is executed regardless of error


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Proposed as answer by Shruti Raturi Friday, November 15, 2013 8:35 AM
    • Marked as answer by mission2010 Friday, November 15, 2013 3:42 PM
    Friday, November 15, 2013 7:27 AM
  • If you instead use a if, you would not need to havre constants. if can compare varriables as easily as cosntants. Here is an idea:

    It looks like the only way to reuse the existing code. Thanks a lot.

    Cheers
    mission2010

    Friday, November 15, 2013 3:43 PM