locked
Passing value as enum? RRS feed

  • Question

  • Hi, 

    Can anyone tell me how to pass a string to a method which expects an enum? 

    String should be any one of the following; CCA, PFA, PSP & ACC.  I want to pass this to a 

    little validation method which checks these values are in the list of allowed values, these are held as enums. 

     

    public enum PaymentType
      { 
        CCA, PFA, ACC, PSP
      }
    

    public Boolean CheckCreditCardType(PaymentType paymentType)
        {
          switch (paymentType)
          {
            case PaymentType.CCA:
              return true;
            case PaymentType.PFA:
              return true;
            case PaymentType.ACC:
              return true;
            case PaymentType.PSP:
              return true;
            default:
              return false;
          } 
        
        }
    

    I'm not sure how to call the method when the value I'm passing is a string. e.g. 

    CheckCreditCardType(???); 
    

     

    I'm sure this is obvious, I'm just not sure how to do it. :-( 

    Thanks 

     

    Saturday, June 11, 2011 6:47 PM

Answers

  • Firstly you cannot pass a string variable if your method expects a enum.

    However if you need it is to pass a string value and then verify if the supplied value is with the enum then you need to do this...

      class Program
      {
        public enum PaymentType
        {
          CCA, PFA, ACC, PSP
        }
    
        public static bool CheckCreditCardType(string paymentType)
        {
          PaymentType paymentValue;
          return Enum.TryParse<PaymentType>(paymentType, out paymentValue);
        }
    
        static void Main(string[] args)
        {
          CheckCreditCardType("CCA");
        }
      }
    

    This will return true if the value that has been passed is within the enum and false if it is not within the enum...

    The solution below given by Vishvvas will also work but the caller method as to know that value exists in enum, if a value that is not there in the enum us provided you would get a run-time error.

     


    Regards, Krishnakant
    • Marked as answer by york0001 Sunday, June 12, 2011 9:25 AM
    Sunday, June 12, 2011 5:41 AM

All replies

  • Enum.Parse will handle this.  See the docs and example at http://msdn.microsoft.com/en-us/library/kxydatf9.aspx
     

    --
    Mike
    Saturday, June 11, 2011 6:55 PM
  • CheckCreditCardType(0);

    CheckCreditCardType(1);

    CheckCreditCardType(2);

    CheckCreditCardType(3);

    should be able to work.

    chanmm    


    chanmm
    Sunday, June 12, 2011 2:30 AM
  • Basesd on Familty Tree Mike referred arcticle, the method can be changed to

    public Boolean CheckCreditCardType(string paymentType)
      {
    PaymentType paymentValue = (PaymentType) Enum.Parse(typeof(PaymentType), paymentType, true);
    if(Enum.IsDefined(typeof(PaymentType), paymentValue))
    {
       switch (paymentValue)
       {
        case PaymentType.CCA:
         return true;
        case PaymentType.PFA:
         return true;
        case PaymentType.ACC:
         return true;
        case PaymentType.PSP:
         return true;
        default:
         return false;
       } 
    }
    else
    {
    //logic to handle when the string doesn't matches
    }
      
      }
    
    Hope this helps.

    Please mark this as "Answer" or "Vote as helpful" if it has resolved your issue/question/problem.
    Sunday, June 12, 2011 3:28 AM
  • Firstly you cannot pass a string variable if your method expects a enum.

    However if you need it is to pass a string value and then verify if the supplied value is with the enum then you need to do this...

      class Program
      {
        public enum PaymentType
        {
          CCA, PFA, ACC, PSP
        }
    
        public static bool CheckCreditCardType(string paymentType)
        {
          PaymentType paymentValue;
          return Enum.TryParse<PaymentType>(paymentType, out paymentValue);
        }
    
        static void Main(string[] args)
        {
          CheckCreditCardType("CCA");
        }
      }
    

    This will return true if the value that has been passed is within the enum and false if it is not within the enum...

    The solution below given by Vishvvas will also work but the caller method as to know that value exists in enum, if a value that is not there in the enum us provided you would get a run-time error.

     


    Regards, Krishnakant
    • Marked as answer by york0001 Sunday, June 12, 2011 9:25 AM
    Sunday, June 12, 2011 5:41 AM
  • Thanks everyone for their answers. 

     

    Regards

    Dave 

    Sunday, June 12, 2011 9:25 AM