Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Displaying Enum Names Using For Loop

Beginner question:

Currently (for testing purposes) this game has two playing pieces with the following names:

public enum JapaneseUnit
    {
        None,
        JapaneseDesDivOne,//playing piece
        JapaneseCruDivOne,//playing piece
        NotAllowed,
    }

The two playing pieces are in the following example (the string I’m looking for is "JapaneseDesDivOneJapaneseCruDivOne")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            string side2ForceType;
            if (Side2Army > -1)
            {
                var side2Army = campaign.Armies[Side2Army];
                var japaneseArmiesInArea = campaign.ActiveArmies().Where(x => x.Location == 
                side2Army.Location && side2Army.Realm == 0).ToList(); 
                //List of all Japanese armies in area (in this case it is two)
                

                for (var unitKey = 0; unitKey < japaneseArmiesInArea.Count(); unitKey++) 
                //count == 2
                
                {
                    side2ForceType = japaneseArmiesInArea[unitKey].JapaneseUnit.ToString();  
                    //Output is "None", I'm looking for "JapaneseDesDivOneJapaneseCruDivOne"
                }

             }

In the future there will be many more playing pieces so while the following is technically correct and closer to what I’m looking for it will be unworkable. I’d like to exclude the "None" Output which is why I’d like to use a loop:

          
                side2ForceType = japaneseArmiesInArea[0].JapaneseUnit.ToString() + 
                                 japaneseArmiesInArea[1].JapaneseUnit + 
                                 japaneseArmiesInArea[2].JapaneseUnit + 
                                 japaneseArmiesInArea[3].JapaneseUnit + etc etc; 
                        //Output is JapaneseDesDivOneJapaneseCruDivOneNoneNone
 

I’m sure I’m missing something basic.

>Solution :

Would something like this achieve what you are trying to do?

string.Join("", japaneseArmiesInArea.Where(a => a.JapaneseUnit != JapaneseUnit.None)
    .Select(a => a.JapaneseUnit.ToString()));

This is assuming that any of your items in japaneseArmiesInArea actually do have the value of JapaneseUnit set to something other than None. If not, you have a problem somewhere else.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading