Advertisement
elena1234

Working with Enum and how to parse string

Apr 9th, 2021 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using MilitaryElite.Enums;
  2. using MilitaryElite.Exceptions;
  3. using System;
  4. using System.Text;
  5.  
  6. //   ReportLevel reportLevel = Enum.Parse<ReportLevel>(commandArguments[0], false);
  7.  
  8. namespace MilitaryElite.Models
  9. {
  10.     public class Mission : IMission
  11.     {
  12.         public Mission(string codeName, string state)
  13.         {
  14.             this.CodeName = codeName;
  15.             this.State = this.TryParse(state);
  16.  
  17.         }
  18.         public string CodeName { get; private set; }
  19.  
  20.         public State State { get; private set; }
  21.  
  22.         public void CompleteMission()
  23.         {
  24.            if(this.State == State.Finished)
  25.             {
  26.                 throw new InvalidMissionCompletionException();
  27.             }
  28.  
  29.             this.State = State.Finished;
  30.         }
  31.  
  32.         private State TryParse(string stateStr)
  33.         {
  34.             bool parsed = Enum.TryParse(stateStr, out State state);
  35.             if (!parsed)
  36.             {
  37.                 throw new InvalidMissionStateException();
  38.             }
  39.  
  40.             return state;
  41.         }
  42.     }
  43. }
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement