2

In my class, PlayerInventory i have a method called buy and sell. i have an int in the parameter called statItem. It is to be used as a place holder for items in my object player.

public void Buy(PlayerStats player, string myName, int myItem, int statItem)
{
    Console.WriteLine("{0} Costs {1:C} per unit.", myName, myItem);
    int MaxPurchase = player.Money / myItem;
    Console.WriteLine("You can afford {0} units.", MaxPurchase);
    Console.WriteLine("How many units would you like to purchase?");
    string Purchasex = Console.ReadLine();
    int ammount = int.Parse(Purchasex);
    player.Money -= (myItem * ammount);
    statItem += ammount;
    Console.WriteLine("You have Purchased {0} units at {1:C} per unit.", ammount, myItem);
    Console.ReadLine();
}

In the method BuyMerchandise/SellMerchandise i have a switch and i replaced the statItem parameter with player.LuxuryWatches, player.LuxuryHandbags etc.

Console.WriteLine("What would you like to purchase? \n");
            ViewInventory(player);
            switch (GetChoice())
            {
                case "1":
                    Price(player);
                    Buy(player, "Luxury Watches", _LuxuryWatches, player.LuxuryWatches);
                    break;
                case "2":
                    Price(player);
                    Buy(player, "Luxury Handbags", _LuxuryHandbags, player.LuxuryHandbags);
                    break; 

This is not passing the value back to thevariablein the player object.

If i replace statItem with player.LuxuryWatch for example it does pass the value back to the object. But i would prefer to use a parameter so i can use one method and change the parameter for each one of the ten items.

    public void Buy(PlayerStats player, string myName, int myItem)
{
    Console.WriteLine("{0} Costs {1:C} per unit.", myName, myItem);
    int MaxPurchase = player.Money / myItem;
    Console.WriteLine("You can afford {0} units.", MaxPurchase);
    Console.WriteLine("How many units would you like to purchase?");
    string Purchasex = Console.ReadLine();
    int ammount = int.Parse(Purchasex);
    player.Money -= (myItem * ammount);
    player.LuxuryWatches += ammount;
    Console.WriteLine("You have Purchased {0} units at {1:C} per unit.", ammount, myItem);
    Console.ReadLine();
}

2 Answers2

0

Since int is a value type, statItem is just a copy of the value passed in as an argument to the Buy method.

A Primitive Solution

A solution that requires the minimum amount of change to your current design is using the ref keyword like:

public void Buy(PlayerStats player, string myName, int myItem, ref int statItem)
{
    // your code here
}

And then call it like:

int amount = player.LuxuryWatches;
Buy(player, "Luxury Watches", _LuxuryWatches, ref amount);
player.LuxuryWatches = amount;

There are other alternatives that are cleaner, more flexible and maintainable, but requires more change to your overall design.

  • I had literally just posted half of this solution at the same moment you had posted this! Except i stated it was half a solution because i completely forgot i can change statItem to amount or what ever i please. This is the missing half thank you for your help and solving my problem! I voted this up but i do not have enough rep at the moment – devinthebox Feb 04 '17 at 22:05
0

OK if theres any body else out there wondering what the solution is i found a partial answer based on a comment that has since disappeared so i do not know the users name...

The user stated that i must use ref in my parameters BUT this still threw up an error: A property, indexer or dynamic member access may not be passed as an out or ref parameter.

I had to use a work around and create a temporary property due to limitations of C# as described here: https://stackoverflow.com/a/4520101/7516711

My code is now as follows:

    public void Buy(PlayerStats player, string myName, int myItem, ref int statItem)
{
    Console.WriteLine("{0} Costs {1:C} per unit.", myName, myItem);
    int MaxPurchase = player.Money / myItem;
    Console.WriteLine("You can afford {0} units.", MaxPurchase);
    Console.WriteLine("How many units would you like to purchase?");
    string Purchasex = Console.ReadLine();
    int ammount = int.Parse(Purchasex);
    player.Money -= (myItem * ammount);
    statItem += ammount;
    Console.WriteLine("You have Purchased {0} units at {1:C} per unit.", ammount, myItem);
    Console.ReadLine();
}

And

   public void BuyMerchandise(PlayerStats player)
    {
        Console.WriteLine("What would you like to purchase? \n");
        ViewInventory(player);
        switch (GetChoice())
        {
            case "1":
                Price();
                int statItems = player.LuxuryWatches;
                Buy(player, "Luxury Watches", _LuxuryWatches, ref statItems);
                player.LuxuryWatches = statItems;
                break;
            case "2":
                Price();
                int statItems = player.LuxuryHandbags;
                Buy(player, "Luxury Handbags", _LuxuryHandbags, ref statItems);
                player.LuxuryHandbags = statItems;
                break;

Though this is only a partial solution as i can not use the same variable name twice thus i am back to square one of creating multiple methods etc.

If any body has another solution or work around please let me know but i do not think it is possible at least using a switch anyway.

Community
  • 1
  • 1