Erwan Maigne Montamat
Erwan Maigne Montamat

Reputation: 51

Getting an issue when converting a string to an enum type using Enum.Parse

So I am working on a school project and I should make saves of different kind of people profils and jobs offers profils, so I got my enum "Skills" which correspond to the differents certificate, and I I got in a profil argument a list of Skills.

I'm actually trying to load a new profil from a text file and I know how it's organized, I have for the first line the password, then the name etc etc, until the lines 7 or 6 (depending if you begin counting to 0 or 1), from there there is skills name written, and I'm actually getting them as a string, so I'm trying to convert them in Skills enum type with the "Enum.Parse(Type EnumType, string Value);"

Here is my problem, it says that "Skills" is an unavailable type and I don't really understand why even after trying to read the microsoft documentation, I founded a site explaining how to convert from a string to an enum type and they made as I did so if anyone can help me seing clear I would be grateful. Thanks you.

    {
        Baccalauréat,
        FormationPremierSecours,
        PréventionEtSecoursCiviquesN1,
        PremierSecoursEnEquipeN2,
        BrevetNationalDeSécuritéEtDeSauvetage,
        SauveteurSecouristeDuTravail,
        BAFA,
        BAFD,
        BASE,
        BSB,
        CAPaccessoiristeRealisateur,
        CAPaccordeurPiano,
        CAPagentAccueilConduiteRoutiereTransportVoyageur,
        CAPagentAssainissementCollecteDéchetsLiquidesSpéciaux,
        CAPagentEntreposageMessagerie,
        CAPagentQualitéDeLeau,
        CAPagentDePreventionMediation,
        CAPagentDeSécurité,
        CAPpolyvalentRestauration,
        CAPVerificaterExtincteurs,
        CAParmurerieFabricationEtReparation,
        CAPartEtTechniquesBijouterieJoaillerieOptionJoaillerie,
        CAPartEtTechniquesBijouterieJoaillerieOptionSertissage,
        CAPartEtTechniquesBijouterieJoaillerieOptionFinition,
        CAPartDeLaBroderie,
        CAPartDentelle,
        CAPArtsReliure,
        CAPartBoisMarqueteur,
        CAPartBoisSculpteurOrnemaniste
    }

public static void RecupProfile(profil profile)
        {
            string mdp;
            string Name;
            string Description;
            string Mail;
            uint Age;
            string PExperiences;
            string skill;
            Skills diplome;
            List<Skills> Diplomes = new List<Skills>();
            string Path = "..\\profilsave\\" + profile.GetNickname() + ".txt";
            string text = File.ReadAllText(Path);
            string[] lines = text.Split(Environment.NewLine);

            uint borne = (uint) lines.Length;
            
            mdp = lines[0];
            Name = lines[1];
            Description = lines[2];
            Mail = lines[3];
            Age = Convert.ToUInt32(lines[4]);
            PExperiences = lines[5];
            uint i = 6;
            while (i< borne)
                
            {
                
                skill = lines[i];
                if(Enum.IsDefined(typeof(Skills), skill))
                {
                    diplome = Enum.Parse(Skills, skill); //Here is the problem
                    
                    
                }
                Diplomes.Add(diplome);
                i += 1;
            }
        }

Upvotes: 2

Views: 89

Answers (2)

MrMoeinM
MrMoeinM

Reputation: 2378

Try this

diplome = (Skills)Enum.Parse(typeof(Skills), skill);

or this

diplome = Enum.Parse<Skills>(skill);

Upvotes: 4

Just casting Skills type

Enum.Parse(typeof(Skills), skill);

Upvotes: -1

Related Questions