我在哪里放置一个数组和一个函数,我将通过我的所有程序c#使用?

时间:2021-07-11 01:49:12

I'm new to C# (coming from c++). I'm creating a WindowsAplication Form and I use an array (which is filled with a function) through all the program, my question is, Where do I put the function and the array? How do I declare them? I tried declaring them inside my Form1 class but it doesn't work with other forms.

我是C#的新手(来自c ++)。我正在创建一个WindowsAplication表单,我通过所有程序使用一个数组(填充了一个函数),我的问题是,我在哪里放置函数和数组?我如何申报?我尝试在我的Form1类中声明它们,但它不适用于其他表单。

3 个解决方案

#1


0  

You could set it up as a property of the Form, so you could do something like this:

您可以将其设置为Form的属性,因此您可以执行以下操作:

public partial class MyForm:Form
{
    public Integer[] MyArray {get; set;}
    ....


    public void PopulateArray()
    {
        MyArray = ...;
    }
}

Then, in some other form, you would simply have something like so:

然后,在其他形式,你只需要这样的东西:

public partial class MyOtherForm:Form
{
    private MyForm myForm;

    public MyOtherForm(MyForm sourceForm)
    {
        this.myForm = sourceForm
    }

    ...

    Integer[] array = this.myForm.MyArray;
}

The above assumes that the form which will contain the array will be passed as a parameter. So essentially, the first form, will initialize the second form and will pass a reference to itself, thus allowing you to access any public fields which MyForm will expose.

以上假设包含数组的表单将作为参数传递。基本上,第一个表单将初始化第二个表单并将传递给自己的引用,从而允许您访问MyForm将公开的任何公共字段。

If you will populate this array only once, and will then only read from it, you could use a Helper class to expose the array to all the relevant classes, so you would have something like so:

如果你只将这个数组填充一次,然后只读取它,你可以使用Helper类将数组暴露给所有相关的类,所以你会有这样的东西:

public static class Helper
{
     public static Integer[] MyArray {get; set;}
}

Then, in your forms, you would simply do: Helper.MyArray = ... to populate and Integer[] array = Helper.MyArray to read.

然后,在您的表单中,您只需执行:Helper.MyArray = ...填充和Integer [] array = Helper.MyArray读取。

#2


1  

You can create a public class in put array and function in it. And Where U want to use array and function, U have to create a object of that class and using object u can access array and function.

您可以在put数组中创建一个公共类并在其中运行。并且在U想要使用数组和函数的情况下,U必须创建该类的对象并使用对象u可以访问数组和函数。

You can also create a static class and And Where U want to use array and function, U can directly use them without creating object.

你也可以创建一个静态类和And Where U想要使用数组和函数,U可以直接使用它们而无需创建对象。

Thanks.

#3


1  

You could have this array as a public static member of any class in your program. For example:

您可以将此数组作为程序中任何类的公共静态成员。例如:

class MyClass {
    public static int[] MyArray;
}

Though, preferably, you should use a public static property to access this member.

但是,您最好使用公共静态属性来访问此成员。

You then access this variable:

然后,您可以访问此变量:

MyClass.MyArray = new int[15];
int myFirstElement = MyClass.MyArray[0];

#1


0  

You could set it up as a property of the Form, so you could do something like this:

您可以将其设置为Form的属性,因此您可以执行以下操作:

public partial class MyForm:Form
{
    public Integer[] MyArray {get; set;}
    ....


    public void PopulateArray()
    {
        MyArray = ...;
    }
}

Then, in some other form, you would simply have something like so:

然后,在其他形式,你只需要这样的东西:

public partial class MyOtherForm:Form
{
    private MyForm myForm;

    public MyOtherForm(MyForm sourceForm)
    {
        this.myForm = sourceForm
    }

    ...

    Integer[] array = this.myForm.MyArray;
}

The above assumes that the form which will contain the array will be passed as a parameter. So essentially, the first form, will initialize the second form and will pass a reference to itself, thus allowing you to access any public fields which MyForm will expose.

以上假设包含数组的表单将作为参数传递。基本上,第一个表单将初始化第二个表单并将传递给自己的引用,从而允许您访问MyForm将公开的任何公共字段。

If you will populate this array only once, and will then only read from it, you could use a Helper class to expose the array to all the relevant classes, so you would have something like so:

如果你只将这个数组填充一次,然后只读取它,你可以使用Helper类将数组暴露给所有相关的类,所以你会有这样的东西:

public static class Helper
{
     public static Integer[] MyArray {get; set;}
}

Then, in your forms, you would simply do: Helper.MyArray = ... to populate and Integer[] array = Helper.MyArray to read.

然后,在您的表单中,您只需执行:Helper.MyArray = ...填充和Integer [] array = Helper.MyArray读取。

#2


1  

You can create a public class in put array and function in it. And Where U want to use array and function, U have to create a object of that class and using object u can access array and function.

您可以在put数组中创建一个公共类并在其中运行。并且在U想要使用数组和函数的情况下,U必须创建该类的对象并使用对象u可以访问数组和函数。

You can also create a static class and And Where U want to use array and function, U can directly use them without creating object.

你也可以创建一个静态类和And Where U想要使用数组和函数,U可以直接使用它们而无需创建对象。

Thanks.

#3


1  

You could have this array as a public static member of any class in your program. For example:

您可以将此数组作为程序中任何类的公共静态成员。例如:

class MyClass {
    public static int[] MyArray;
}

Though, preferably, you should use a public static property to access this member.

但是,您最好使用公共静态属性来访问此成员。

You then access this variable:

然后,您可以访问此变量:

MyClass.MyArray = new int[15];
int myFirstElement = MyClass.MyArray[0];