Advertisement
Guest User

Ordering Example

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1.     public partial class Form1 : Form
  2.     {
  3.         private static Random random = new Random();
  4.  
  5.         private string[] ItemNames =  { "Leather", "Shield", "Apple", "Sword"};
  6.  
  7.         private List<GameItem> Items = new List<GameItem>();
  8.  
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void addButton_Click(object sender, EventArgs e)
  15.         {
  16.             //Create a dummy game item
  17.             var gameItem = new GameItem();
  18.             gameItem.Name = ItemNames[random.Next(0, ItemNames.Length)]; //Random name
  19.             gameItem.Quantity = random.Next(1, 15); //Random quantity
  20.  
  21.             Items.Add(gameItem); //Add to collection
  22.  
  23.             //Create an rodered collection
  24.             var orderedList = Items.OrderByDescending(x => x.Quantity).ToList();
  25.  
  26.             //Clear current listbox
  27.             inventoryListbox.Items.Clear();
  28.            
  29.             //Add ordered items
  30.             foreach(var item in orderedList)
  31.             {
  32.                 inventoryListbox.Items.Add(item.Name + " : " + item.Quantity);
  33.             }
  34.         }
  35.     }
  36.  
  37.     //Item container object
  38.     public class GameItem
  39.     {
  40.         public string Name { get; set; }
  41.         public int Quantity { get; set; }
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement