从单独的类中的数组中读取数据

时间:2018-03-28 23:03:36

标签: c# arrays

我正在尝试从另一个数组中的一个类中读取数据。

第一堂课:

using System;
using static System.Console;

namespace Munchkin
{
    class GameInfo
    {
        public static void CardInfo()
        {

            //Array for Monster Cards {Name, HP}
            string[,] monsterCards = new string[,] { { "Bob", "500" }, { "Billy", "600" }, { "Joe", "700" }, { "Frank", "750" }, { "BillyBob", "850" } };
            //for (int i = 0; i < ((monsterCards.Length) / 2); i++)
            //{
            //  WriteLine("Monster name {0}, Health Points {1}", monsterCards[i, 0], monsterCards[i, 1]);
            //}
        }

    }
}

我的另一课是:

using System;
using static System.Console;
using System.Threading;


namespace Munchkin
{
    class Battle
    {
        public static bool Big;
        public static void BigChance()
        {
            int chance;
            Random rand1 = new Random();
            chance = rand1.Next(1, 101);

            if(chance > 95)
            {
                Big = true;
            }
            else
            {
                Big = false;
            }
        }

        public static void BattleStart()
        {
            BigChance();
            GameInfo CardInfo = new GameInfo();
            Random pickMonster = new Random();
            int monster = pickMonster.Next(0, (monsterCards.Length) / 2);
        }
    }
}

我尝试过各种各样的事情,包括更改权限,我似乎无法以任何方式读取数据或传递数据。

3 个答案:

答案 0 :(得分:0)

符号monsterCards被声明为局部变量,并且在本地(方法)范围之外的任何地方都不可访问。

您可以将变量移动到类范围,如下所示:

class GameInfo
{
    private string[,] monsterCards = new string[,] { { "Bob", "500" }, { etc...

    public static void CardInfo()
    {
        //Stuff
    }
}

然后添加一个属性,使其在课堂外可用:

class GameInfo
{
    ....
    public string[,] GetMonsterCards()
    {
        return this.monsterCards;
    }
}

现在你可以从其他课程中读到它:

 public static void BattleStart()
 {
     BigChance();
     GameInfo CardInfo = new GameInfo();
     Random pickMonster = new Random();
     int monster = pickMonster.Next(0, (CardInfo.GetMonsterCards().Length) / 2);
 }

答案 1 :(得分:0)

如果您希望其他类可以访问GameInfo类的数组,请将其设为公共属性:public property:

class GameInfo
{
    public string[,] monsterCards;

    public static void GameInfo()
    {
        //Array for Monster Cards {Name, HP}
        monsterCards = new string[,] 
        { 
            { "Bob", "500" }, { "Billy", "600" }, { "Joe", "700" }, 
            { "Frank", "750" }, { "BillyBob", "850" } 
        };
    }           
}

然后在您的其他方法中,您可以像访问它一样访问它:

public static void BattleStart()
{
    BigChance();
    GameInfo CardInfo = new GameInfo();
    Random pickMonster = new Random();
    int monster = pickMonster.Next(0, (CardInfo.monsterCards.Length) / 2);
}

答案 2 :(得分:0)

你可能想要更像这样的东西:

class GameInfo
{
    public Dictionary<string, int> MonsterCards { get; private set; }
    public GameInfo()
    {
        this.MonsterCards = new Dictionary<string, int>()
        {
            { "Bob", 500 }, { "Billy", 600 }, { "Joe", 700 }, { "Frank", 750 }, { "BillyBob", 850 }
        };
    }
}

class Battle
{
    private Random _random = new Random();
    public bool Big { get; private set; }
    public void BigChance()
    {
        this.Big = _random.NextDouble() >= 0.95;
    }

    public void BattleStart()
    {
        this.BigChance();
        GameInfo gameInfo = new GameInfo();
        int monster = _random.Next(0, (gameInfo.MonsterCards.Count) / 2);
    }
}