访问 C# 中的私有方法

发布于 2024-11-13 23:30:23 字数 1248 浏览 3 评论 0原文

大家好,我是 C# 世界的新手,我遇到了一个问题。我在程序的 Form_Load 方法中完成了一个数组,但我需要在 picture_box 方法中访问该数组,如下所示:

        private void Form2_Load(object sender, EventArgs e)
    {
        //In this method we get a random array to set the images

        int[] imgArray = new int[20];

        Random aleatorio = new Random();

        int num, contador = 0;

        num = aleatorio.Next(1, 21);

        imgArray[contador] = num;
        contador++;

        while (contador < 20)
        {
            num = aleatorio.Next(1, 21);
            for (int i = 0; i <= contador; i++)
            {
                if (num == imgArray[i])
                {
                    i = contador;
                }
                else
                {
                    if (i + 1 == contador)
                    {
                        imgArray[contador] = num;
                        contador++;
                        i = contador;
                    }
                }
            }
        }

    }


    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(@"C:\Users\UserName\Desktop\MyMemoryGame\" + imgArray[0] + ".jpg");
    }

但我只收到错误: 错误 1 ​​名称“imgArray”在当前上下文中不存在

Hi People I'm newbie in the C# world and I'm having a problem. I have done an array in the Form_Load method of my program, but I need to access the array in a picture_box method like this:

        private void Form2_Load(object sender, EventArgs e)
    {
        //In this method we get a random array to set the images

        int[] imgArray = new int[20];

        Random aleatorio = new Random();

        int num, contador = 0;

        num = aleatorio.Next(1, 21);

        imgArray[contador] = num;
        contador++;

        while (contador < 20)
        {
            num = aleatorio.Next(1, 21);
            for (int i = 0; i <= contador; i++)
            {
                if (num == imgArray[i])
                {
                    i = contador;
                }
                else
                {
                    if (i + 1 == contador)
                    {
                        imgArray[contador] = num;
                        contador++;
                        i = contador;
                    }
                }
            }
        }

    }


    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(@"C:\Users\UserName\Desktop\MyMemoryGame\" + imgArray[0] + ".jpg");
    }

But I only get the error: Error 1 The name 'imgArray' does not exist in the current context

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2024-11-20 23:30:23

您需要在类级别(Form2_Load 之外)而不是在类级别定义 int[] imgArray。否则该变量的“范围”仅限于该函数。您需要删除 Form2_Load 中的第一个“int[]”部分,以防止您只是声明一个新变量。

例如:

public class MyClass
{ 
    private int[] myInt;

    public void Form2_Load(...) {
        myInt = ...;
    }

}

You need to define int[] imgArray at the class level (outside of Form2_Load) rather than inside it. Otherwise the "scope" of that variable is limited to that function. You will need to knock off the first "int[]" part in Form2_Load to prevent you from just declaring a new variable.

For example:

public class MyClass
{ 
    private int[] myInt;

    public void Form2_Load(...) {
        myInt = ...;
    }

}
终陌 2024-11-20 23:30:23

该错误的含义与它所说的完全一样。

您已在 Form2_Load 函数的范围中声明了该数组。在它之外,它不会存在。

要实现您想要实现的目标,请向表单本身添加一个私有数组。

private int[] _imgArray = new int[20];

private void Form2_Load(object sender, EventArgs e)
{
    //Setup the imgArray
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    //_imgArray is now available as its scope is to the class, not just the Form2_Load method

}

希望这有帮助。

The error means exactly what it says.

You've declared the array in the scope of the Form2_Load function. Outside of it, it will not exist.

To do what you're trying to achieve, add a private array to the form itself.

private int[] _imgArray = new int[20];

private void Form2_Load(object sender, EventArgs e)
{
    //Setup the imgArray
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    //_imgArray is now available as its scope is to the class, not just the Form2_Load method

}

Hopefully that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文