Finding specific item in a list

I'm able to read a series of bytes from a file into a byte array. I have successfully mapped the data from the byte array to a series of lists of classes. Here is a part of my GetData method that does some of this mapping:

 foreach(FlyableShipType playerShip in Enum.GetValues(typeof(FlyableShipType))) { TrainingRecordList.Add(new TrainingRecord(playerShip, bytes, trainingScoreOffset + scoreMultiplier, trainingCompletedOffset + completedMultuplier)); scoreMultiplier += 4; // increments offset to read score completedMultuplier += 1; // increments offset to read how many training levels were completed } 

This is the TrainingRecord class that becomes a list item:

public class TrainingRecord { public FlyableShipType TrainingShipType { get; set; } public uint TrainingScore { get; set; } public int TrainingCompleted { get; set; } public TrainingRecord(FlyableShipType trainingShipType, byte[] bytes, int trainingScoreOffset, int trainingCompletedOffset) { TrainingShipType = trainingShipType; TrainingScore = BitConverter.ToUInt32(bytes, trainingScoreOffset); TrainingCompleted = bytes[trainingCompletedOffset]; } } 

What I want to do is find a specific item in the list, then populate various controls on my windows forms with the data in the list. This is where I'm struggling. This is how I'm currently doing it:

 foreach(FlyableShipType playerShip in Enum.GetValues(typeof(FlyableShipType))) { switch (playerShip) { case FlyableShipType.XWing: upDownTrainingLevelXWing.Value = (decimal)pilot.TrainingRecordList[0].TrainingCompleted; break; case FlyableShipType.YWing: upDownTrainingLevelYWing.Value = (decimal)pilot.TrainingRecordList[1].TrainingCompleted; break; case FlyableShipType.AWing: upDownTrainingLevelAWing.Value = (decimal)pilot.TrainingRecordList[2].TrainingCompleted; break; case FlyableShipType.BWing: upDownTrainingLevelBWing.Value = (decimal)pilot.TrainingRecordList[3].TrainingCompleted; break; } } 

It seems there must be a better way.

Also, is there a better way to reference blocks of code in a reddit post other than copying and pasting them in the text? Something like CodePlex or GitHub?

by LordDrakken via /r/csharp

Leave a Reply