c# Alpha Telephone Number Translator

前端 未结 5 1566
忘掉有多难
忘掉有多难 2021-01-28 01:35

I have a homework assignment where the program will accept any phone number in the format similar to 555-GET-FOOD. The task is to map the alphabetic letters to numbers and tran

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-28 02:00

    It looks like you did attempt to use TryParse:

    //ch = Enum.TryParse(AlphaNumber);

    That is the method that you probably want to use. But there are a number of problems with it in that form, which probably gave you errors as you mentioned.

    The parameters this method expects are a string (which matches the enumerated constant, or name from the enum) and an out parameter of the type of the enum you want to parse. The method return a bool.

    If the TryParse is successful then the method returns TRUE with the corresponding value from the enum set in the out parameter.

    This code should allow you to get the result you want as an int using your variable ch as a string for the input to parse:

    AlphaNumber parsedCh;
    int? chValue = null;
    
    if (Enum.TryParse(ch.ToString().ToUpper(), out parsedCh)) 
    {
        chValue = (int)parsedCh;
        Console.WriteLine(chValue); 
    }
    

提交回复
热议问题