Replacing Items in a Loop

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;

public class Gamer
{
	public int HoursPlayeedPerWeek;
	// Going to make it so you can't change the value after it's created
	public readonly string GamerStatus;
	
	// In the real world, maybe this information came from a database so we want to load it as it came to us
	public Gamer(int hoursPlayedPerWeek, string gamerStatus)
	{
		HoursPlayeedPerWeek = hoursPlayedPerWeek;
		GamerStatus = gamerStatus;
	}
	
	// Got smarter over time and wanted to set the status based soley off the number of hours per week
	public Gamer(int hoursPlayedPerWeek)
	{
		HoursPlayeedPerWeek = hoursPlayedPerWeek;
		GamerStatus = GetGamerStatusFromHoursPlayed(hoursPlayedPerWeek);
	}
	
	// Nice standard method for getting the gamer status based off the number of hours played per week
	public static string GetGamerStatusFromHoursPlayed(int hoursPlayedPerWeek){
		if(hoursPlayedPerWeek <= 5){
			return "Casual";
		}
		if(hoursPlayedPerWeek >5 && hoursPlayedPerWeek < 10){
			return "Enthusiast";
		}
		return "Fanatic";
	}
	
	public override string ToString()
	{
		return string.Format("Hours per Week: {0}, {1}", HoursPlayeedPerWeek, GamerStatus);
	}
}



public class Program
{
	public static void Main()
	{
            List<Gamer> gamers = new List<Gamer> { 
                new Gamer(4, "Casual"),
                new Gamer(2, "Enthusiast"),
                new Gamer(10, "Enthusiast"),
                new Gamer(20, "Fanatic")
            };


            // In this very contrived example, this code will fail because you can't change an item that's being
            // enumerated - otherwise, how would the enumerator know what to do next?
            /*
            foreach (var gamer in gamers)
            {
                if (gamer.GamerStatus != Gamer.GetGamerStatusFromHoursPlayed(gamer.HoursPlayeedPerWeek))
                {
                    gamer = new Gamer { HoursPlayeedPerWeek = gamer.HoursPlayeedPerWeek, GamerStatus = "Casual" };
                }
            }
            */

            // We're going to create a dictionary that will contain the index of the gamer in the list, and the new gamer
            // that will replace the incorrect one later
			Console.WriteLine("Original List of Gamers");
            var replacements = new Dictionary<int, Gamer>();
            
			foreach (var gamer in gamers)
            {
                int gamerIndex = gamers.IndexOf(gamer);
                Console.WriteLine("{0}. {1}", gamerIndex+1, gamer);

                if (gamer.GamerStatus != Gamer.GetGamerStatusFromHoursPlayed(gamer.HoursPlayeedPerWeek))
                {
                    replacements.Add(gamerIndex, new Gamer(gamer.HoursPlayeedPerWeek));
                }
            }

            // Here's where we're going to swap in the replacement objects
            foreach (var kvp in replacements)
            {
                gamers[kvp.Key] = kvp.Value;
            }

			Console.WriteLine("{0}Updated List of Gamers", System.Environment.NewLine, System.Environment.NewLine);
            foreach (var gamer in gamers)
            {
                int gamerIndex = gamers.IndexOf(gamer);
                Console.WriteLine("{0}. {1}", gamerIndex+1, gamer);
            }

	}
}
;