Advertisement
Dosk3n

CollectionExampleLists

Mar 19th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace AddtoUnknownListSize
  8. {
  9.     /// <summary>
  10.     /// A very basic class designed to take any string input for name and class.
  11.     /// Using a declaration method we can set name and class as we create an object.
  12.     /// </summary>
  13.     class Program
  14.     {
  15.         class Player
  16.         {
  17.             public string playerName = "unknown";
  18.             public string playerClass = "unknown";
  19.  
  20.             public Player(string pName, string pClass)
  21.             {
  22.                 playerName = pName;
  23.                 playerClass = pClass;
  24.             }
  25.         }
  26.         /// <summary>
  27.         /// Since we dont always know how much data we want to keep an array may not always be
  28.         /// the best solution. An array is great when we want to set how many objects we want but
  29.         /// what if we didnt know if we wanted 1 players or 100 players? Well by using a list
  30.         /// we can manipulate data in a similar way but not need to declare the ndex size as
  31.         /// collections such as lists does that on the fly as we keep adding more objects.
  32.         /// </summary>
  33.         /// <param name="args"></param>
  34.         static void Main(string[] args)
  35.         {
  36.             PrintWelcome();
  37.             // Create a list collection. Very similar to how an array is created
  38.             List<Player> gamePlayers = new List<Player>();// create list
  39.             gamePlayers.Add(new Player("Player One", "Human"));// Set the first player in the list
  40.             while (true)
  41.             {
  42.                 foreach (var player in gamePlayers) // list how many players we currently have in the list
  43.                 {
  44.                     Console.ForegroundColor = ConsoleColor.Cyan;
  45.                     Console.WriteLine("Player Name: {0} \tPlayer Class: {1}", player.playerName, player.playerClass);
  46.                     Console.ResetColor();
  47.                 }
  48.  
  49.                 Console.WriteLine();
  50.                 Console.Write("Do you want to create another player? (y) or (n):");
  51.                 string makeNewPlayer = Console.ReadLine();
  52.                 Console.WriteLine();
  53.  
  54.                 if (makeNewPlayer == "y")
  55.                 {
  56.                     CreateAPlayer(gamePlayers); // Send our list called gamePlayers to the method below
  57.                                                 // to add more to the list.
  58.                 }
  59.                 else
  60.                 {
  61.                     Console.WriteLine("Thank you for looking at an example of collections"
  62.                                     + "\nPress enter to end the program");
  63.                     Console.ReadLine();
  64.                     break;
  65.                 }
  66.  
  67.             }
  68.         }
  69.  
  70.         //############ METHODS #################
  71.         static void CreateAPlayer(List<Player> gamePlayers)
  72.         {
  73.             Console.WriteLine("Ok lets create a player!");
  74.             Console.WriteLine();
  75.             Console.Write("Choose your players name: ");
  76.             string theName = Console.ReadLine();
  77.             Console.Write("Choose your players class: ");
  78.             string theClass = Console.ReadLine();
  79.             gamePlayers.Add(new Player(theName, theClass)); // takes the name and class string and adds it to our list.
  80.             Console.Clear();
  81.         }
  82.  
  83.         static void PrintWelcome()
  84.         {
  85.             Console.WriteLine("AN EXAMPLE OF COLLECTIONS BY DEAN T (Twitter: @Dosk3n)"
  86.                              +"\nCODED FOR CODING 101 - WWW.TWIT.TV/CODE");
  87.             Console.WriteLine();
  88.         }
  89.  
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement