简体   繁体   中英

Use class variables defined in an other function in C#

I'm working on a textbased adventure in the Console with items, shop and so on.

Now I wanna shorten down the code for buying swords by using classes. I put the swords definition in a seperate function and the code to randomize the swords you can buy in an other:

namespace Text_Adventure
{
       class Game
   {
//Weapon class

   public class Weapon{
           public string name;
           public int damage;
           public int magic;
           public int durability;
           public int price;
       }   

 //the weapon showed in the shop
     public class Weaponshop{
           public string name;
           public int damage;
           public int magic;
           public int durability;
           public int price;
        }

...


// defenition of swords

public void swords()
    {
        Weapon sword1 = new Weapon();

        sword1.name = "Wooden Sword";
        sword1.damage = 2;
        sword1.magic = 1;
        sword1.durability = 20;
        sword1.price = 10;

        Weapon sword2 = new Weapon();

        sword2.name = "Iron Sword";
        sword2.damage = 3;
        sword2.magic = 2;
        sword2.durability = 50;
        sword2.price = 20;
 ...
    }

//gamble sword that are shown in the shop
 public void swordgamble()
    {

        Random shopgamble = new Random();
        Weaponshop shopsword1 = new Weaponshop();
        //gamble shopsword 1

        int shopgamblenumber = shopgamble.Next(1, 8 + 1);

Now the problemcode

    --->  if (shopgamblenumber == 1)
        {
            shopsword1 = sword1;
        }

        if (shopgamblenumber == 2)
        {
            shopsword1 = sword1;
        }
                                       <----

the same happens with shopsword2 and shopsword3

public void buysword
{

swordgamble();
Console.WriteLine("Shop");
        Console.WriteLine();
        Console.WriteLine("Which sword do you would like to have?");
        Console.WriteLine();

        while (correct == 0)
        {
            Console.WriteLine("1. " + shopsword1.name);
            Console.WriteLine("2. " + shopsword2.name);
            Console.WriteLine("3. " + shopsword3.name);
            Console.WriteLine("4. None of these");
            Console.WriteLine("");
...

 }

My programm can't read the variables I set in swords and assing it to shopsword1

My old version was with only variables and because of this pretty big when i took 8 swords you can buy. Does anyone knows how you can read the variables I set in swords and read the shopsword in the function buysword(); ?

As noted by someone, you have to store the generated swords in a variable. I would use a IDictionary<int, Weapon> , which can simplify the code in swordgamble() . The key of this dictionary is the shopgamblenumber used to access it.

public IDictionary<int, Weapon> swords()
    {
       var dict = new Dictionary<int, Weapon> swords() {
       1, new Weapon {
           name = "Wooden Sword",
           damage = 2,
           magic = 1,
           durability = 20,
           price = 10
       },
       2, new Weapon {
           name = "Iron Sword",
           damage = 3,
           magic = 2,
           durability = 50,
           price = 20
       }
    };
    return dict;
}

Then use this to generate the swords.

public void swordgamble() {

        var swordsDict = swords();
        Random shopgamble = new Random(Guid.NewGuid().GetHashCode()); // seed rng
        Weaponshop shopsword1 = new Weaponshop();
        //gamble shopsword 1

        int shopgamblenumber = shopgamble.Next(1, 8 + 1);
        var sword = swordsDict[shopgamblenumber];
        //...
}

Also, Weaponshop should inherit from Weapon so shopsword1 = sword can work.

Using Georg's answer you could save the overhead of creating the dictionary each time a call to swords is made by in effect creating a Weapon factory method.

        public Weapon swords(int weaponNumber)
        {
           switch (weaponNumber)
           {
                case (1);
                     return new Weapon() {
                        name = "Wooden Sword",
                        damage = 2,
                        magic = 1,
                        durability = 20,
                        price = 10
                      };
                case (2):
                   return new Weapon() {
                       name = "Iron Sword",
                       damage = 3,
                       magic = 2,
                       durability = 50,
                       price = 20
                     };
                default:
                     return null; //Or some other default for when it doesn't exist
            }
       }

Then your calling code would look like this

public void swordgamble() {      
        Random shopgamble = new Random(Guid.NewGuid().GetHashCode()); // seed rng
        Weapon shopsword1;
        //gamble shopsword 1

        int shopgamblenumber = shopgamble.Next(1, 8 + 1);
        shopsword1 = swords(shopgamblenumber);
        //...
}

And as Flater commented there is no need for the WeaponShop class you can just use the Weapon class again to hold the instance of the Weapon retrieved from the shop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM