Press "Enter" to skip to content

Lesson 21.1 – Adding a price to game items

If we want the player to be able to buy and sell items, we need to add prices to each Item object.


Step 1: In the Engine project, edit the Item.cs class.

Add an integer “Price” auto-property to the class, add a price parameter to the constructor, and set the Price property’s value in the constructor.

The Item class should look like this now:

namespace Engine
   public class Item
       public int ID { get; set; }
       public string Name { get; set; }
       public string NamePlural { get; set; }
       public int Price { get; set; }
       public Item(int id, string name, string namePlural, int price)
           ID = id;
           Name = name;
           NamePlural = namePlural;
           Price = price;


Step 2: Now that the Item constructor requires a price parameter, we need to change the classes that use Item as their base class – Weapon and HealingPotion.

We will change their constructors to accept a “price” parameter, and pass it to the base (Item) class constructor. This is how they should look now:



namespace Engine
   public class HealingPotion : Item
       public int AmountToHeal { get; set; }
       public HealingPotion(int id, string name, string namePlural, int amountToHeal, int price)
           : base(id, name, namePlural, price)
           AmountToHeal = amountToHeal;



namespace Engine
   public class Weapon : Item
       public int MinimumDamage { get; set; }
       public int MaximumDamage { get; set; }
       public Weapon(int id, string name, string namePlural, int minimumDamage, int maximumDamage, int price)
           : base(id, name, namePlural, price)
           MinimumDamage = minimumDamage;
           MaximumDamage = maximumDamage;


Step 3: The next change will be to the World class, where we create instances of the items. Change the PopulateItems() function to this (you can use your own prices for the items):

private static void PopulateItems()
   Items.Add(new Weapon(ITEM_ID_RUSTY_SWORD, "Rusty sword", "Rusty swords", 0, 5, 5));
   Items.Add(new Item(ITEM_ID_RAT_TAIL, "Rat tail", "Rat tails", 1));
   Items.Add(new Item(ITEM_ID_PIECE_OF_FUR, "Piece of fur", "Pieces of fur", 1));
   Items.Add(new Item(ITEM_ID_SNAKE_FANG, "Snake fang", "Snake fangs", 1));
   Items.Add(new Item(ITEM_ID_SNAKESKIN, "Snakeskin", "Snakeskins", 2));
   Items.Add(new Weapon(ITEM_ID_CLUB, "Club", "Clubs", 3, 10, 8));
   Items.Add(new HealingPotion(ITEM_ID_HEALING_POTION, "Healing potion", "Healing potions", 5, 3));
   Items.Add(new Item(ITEM_ID_SPIDER_FANG, "Spider fang", "Spider fangs", 1));
   Items.Add(new Item(ITEM_ID_SPIDER_SILK, "Spider silk", "Spider silks", 1));
   Items.Add(new Item(ITEM_ID_ADVENTURER_PASS, "Adventurer pass", "Adventurer passes", -1));


NOTE: For the Adventurer pass, we set the item’s value to -1. We’re going to use that as a “flag” (indicator) value. In our code to display the player’s items, we won’t include any items that have a value of -1. Those will be unsellable items.


Step 4: So you can see how I think when I’m programming, I left the “Note” in step 3.

However, if I need to add a note for the lesson, that might also mean something is not clear in the code. I don’t want to make a change in the future, and forget why some items have a value of -1.

This is a “code smell” – a sign that the source code may have a problem.

Let’s make this easier to understand.

In the World class, add this new class-level, public constant, near the other constants at the top of the class:

public const int UNSELLABLE_ITEM_PRICE = -1;

Then, change the Adventurer pass line in PopulateItems() to this:

Items.Add(new Item(ITEM_ID_ADVENTURER_PASS, "Adventurer pass", "Adventurer passes", UNSELLABLE_ITEM_PRICE));

Now, it is very clear that we don’t want the player to be able to sell adventurer passes. We won’t ever be confused by a “magic number” – a hard-coded value that has a special meaning.


Step 5: Run your program, and make sure it still works. Right now, you won’t see the price anywhere. But, it’s good to check your program after each change.


Source code for this lesson

Source code on GitHub

Source code on Dropbox


Next lesson: Lesson 21.2 – Create the vendor class and add it to locations

Previous lesson: Lesson 21.0 – Plans for adding a vendor to locations

All lessons: Learn C# by Building a Simple RPG Index


  1. 101code
    101code September 15, 2018

    Thanks for this tutorial! I’m running into a strange problem here: I have added the property Price in just the right way to the Item class, and the Weapon and HealingPotion constructors both ask for it as part of the base, but when I add ‘price’, it’s not recognised. I checked my spelling and everything, but it won’t go away. Would you happen to know what the problem might be?

    • Scott Lilly
      Scott Lilly September 16, 2018

      Can you upload your solution and send me the link again?

Leave a Reply

Your email address will not be published. Required fields are marked *