How to use Enum.Parse()?

Asked

Viewed 621 times

3

My code displays error when using Enum.Parse<enum>(Console.ReadLine());

I am asking the user to enter the level of the position occupied, however to receive the data entered by the user Parse<> displays error, does not pass to the Level.

 Classe program


    static void Main(string[] args)
    {
        Console.WriteLine("Enter client data: ");
        Console.Write("Name: ");
        string clientName = Console.ReadLine();
        Console.Write("Email: ");
        string email = Console.ReadLine();
        Console.Write("Birth date (DD/MM/YYYY): ");
        DateTime birthDate = DateTime.Parse(Console.ReadLine());
        Console.WriteLine("Enter order data: ");
        Console.Write("Level: (Junior/MidLevel/Senior) ");
        OrderStatus Level = Enum.Parse<OrderStatus>(Console.ReadLine());

    }



classe Enums


enum OrderStatus : int
{
    Junior = 0,
    MidLevel = 1,
    Senior = 2
}

CS0308 Error The non-generic "Enum.Parse(Type, string)" method cannot be used as Salesorde type arguments

https://github.com/thuliomariano/Exercicio

  • CS0308 Error The non-generic "Enum.Parse(Type, string)" method cannot be used as Salesorder type arguments

  • Your code here doesn’t have SalesOrder. The code on Github has no enumeration.

  • sorry, corrected

  • Enum is inside the Entitie

2 answers

1


You shouldn’t use the Parse(), this is explained in Differences between Parse vs Tryparse. Using the TryParse() would look like this and it works:

using System;
using static System.Console;

public class Program {
    static int Main() {
        WriteLine("Enter client data: ");
        Write("Name: ");
        string clientName = ReadLine();
        Write("Email: ");
        string email = ReadLine();
        Write("Birth date (DD/MM/YYYY): ");
        if (!DateTime.TryParse(ReadLine(), out var birthDate)) return 1;
        WriteLine("Enter order data: ");
        Write("Status: ");
        if (!Enum.TryParse<OrderStatus>(ReadLine(), true, out var status)) return 1;
        return 0;
    }
}
enum OrderStatus {
    Junior = 0,
    MidLevel = 1,
    Senior = 2
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I still find worrying this thing of letting the person type the level, but as a simple solution even passes if using the TryParse(), without it can not use at all, is not robust.

  • I’m studying Numerators and when I use git clone the teacher’s code runs normally and when I do the same code shows error, follow the teacher’s git: https://github.com/acenelio/composition1-csharp. Obs: I couldn’t do it the way you mentioned, even with tryParse error persists

  • I answered what’s in the question and I can ask link showing working. Are you making any error that is not in the question.

0

//class program main ----------------------------------------------

using System;   

 namespace Worker
 {
      class Program
      {
          static void Main(string[] args)
          {
               Console.WriteLine("Enter client data: ");
               Console.Write("Name: ");
               string clientName = Console.ReadLine();
               Console.Write("Email: ");
               string email = Console.ReadLine();
               Console.Write("Birth date (DD/MM/YYYY): ");
               DateTime birthDate = DateTime.Parse(Console.ReadLine());
               Console.WriteLine("Enter order data: ");
               Console.Write("Level: (Junior/MidLevel/Senior) ");
               Enum.TryParse(Console.ReadLine(), out OrderStatus level);
                      
          }
      }
 }
    
    
    
 // enums ----------------------------------------------

 namespace Worker.Entities.Enums
 {
     enum OrderStatus : int
     {
          Junior,
          MidLevel,
          Senior
     }
 }
  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.