简体   繁体   English

如何从不同的 button_click 访问一个 button_click 内的数组?

[英]How to access an array within one button_click, from a different button_click?

When I click the "newRound" button, the program generates the Minor Arcana Deck and shuffles it via FisherYatesShuffle method.当我单击“newRound”按钮时,程序会生成 Minor Arcana Deck 并通过 FisherYatesShuffle 方法对其进行洗牌。

public partial class MainWindow : Window
    {
     public MainWindow()
            {
                InitializeComponent();
            }
    
     private void newRound_Click(object sender, RoutedEventArgs e)
            {
                var minorArcanaDeck = new string[] {"Ace of Wands","Two of Wands", etc.};
                var rng = new Random();
                rng.Shuffle(minorArcanaDeck);
            }  
    }

static class FisherYatesShuffle
        {
            public static void Shuffle<T>(this Random rng, T[] array)
            {
                int n = array.Length;
                while (n > 1)
                {
                    int k = rng.Next(n--);
                    T temp = array[n];
                    array[n] = array[k];
                    array[k] = temp;
                }
            }
        }

In a different button (which is also within the partial class), I want to access this minorArcanaDeck Array, but I don't know how.在另一个按钮(也在部分类中)中,我想访问这个 minorArcanaDeck Array,但我不知道如何访问。 How would I make this code below work, so that a textbox displays the value of the array when the "drawCard" button is clicked?我如何使下面的代码工作,以便在单击“drawCard”按钮时文本框显示数组的值?

private void drawCard_Click(object sender, RoutedEventArgs e)
    {
        myText.Text = minorArcanaDeck[0];  
   
    }

You need to understand the difference between local variables and class variables(fields/properties)您需要了解局部变量和类变量(字段/属性)之间的区别

You should define the array as a field, this way each (non-static) method is able to access it.您应该将数组定义为一个字段,这样每个(非静态)方法都可以访问它。

public partial class MainWindow : Window
{
    // fields
    private string[] _minorArcanaDeck = new string[] {"Ace of Wands","Two of Wands", etc.};
    private Random _rng = rand = new Random(Guid.NewGuid().GetHashCode());

    public MainWindow()
    {
        InitializeComponent();
    }

    private void newRound_Click(object sender, RoutedEventArgs e)
    {
        _rng.Shuffle(_minorArcanaDeck);
    }  
    
    private void drawCard_Click(object sender, RoutedEventArgs e)
    {
        myText.Text = _minorArcanaDeck[0];  
    }
}

static class FisherYatesShuffle
{
    public static void Shuffle<T>(this Random rng, T[] array)
    {
        int n = array.Length;
        while (n > 1)
        {
            int k = rng.Next(n--);
            T temp = array[n];
            array[n] = array[k];
            array[k] = temp;
        }
    }
}

In my case I use the _ prefix, so I have a clear naming convention.在我的例子中,我使用_前缀,所以我有一个明确的命名约定。 But it isn't required.但这不是必需的。 By moving the Random out of the newRound_Click method and using a random seed, will generate better random numbers.通过将 Random 移出newRound_Click方法并使用随机种子,将生成更好的随机数。 (else you would always have the same random sequence) (否则您将始终拥有相同的随机序列)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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