在表单之间传递数组并在数组中进行管理

发布于 2024-10-07 17:55:39 字数 1500 浏览 0 评论 0原文

我想将一个数组从 Form1 传输到 Form2,然后在 Form2 上向该数组添加一些值。 在 Form1 按钮上单击我输入以下代码:

        int[] arrayOfInt = new int[4];
        arrayOfInt[0] = 19;
        arrayOfInt[1] = 12;
        Form2 frm2 = new Form2();
        frm2.TakeThis(arrayOfInt);
        frm2.Show();

在 form2 中,我创建了 TakeThis(int[]) ,它捕获 form1 中的值并在标签上显示数组的值。现在,我不知道如何向数组添加一些值。例如 arrayOfInt[2]arrayOfInt[3] 并发送到 Form3。

编辑

我决定使用列表来存储我的数据,但最后我必须转换 列表到数组,因为我正在对数据进行一些报告和数学运算。

这个例子与上面的例子不同。在此存储列表中文本框、组合框的所有输入。最后我需要将列表转换为数组。

我做了什么, 我创建了一个新的全局类:

static class List{

   static List<string> list;

   public static void NewList(){
   list=new List<string>();
   }

   public  static void Save(string value){
   list.Add(value);
   }

   public static void Display(){
   foreach(var value in list){
   MessageBox.Show(value);
   }
   }

}

数据插入到表单之间,

List.save(some_strings);
 ...

但最后我需要将列表转换为数组。我用谷歌搜索,找到了 ToArray() 函数,但我不知道如何在我的代码中使用。我尝试使用属性,但无法进行转换。请帮忙。

编辑2 我找到了解决方案。 在全局静态类 List 中,我创建了一个返回字符串列表的方法:

public static List<string> GetList(){
              return list;
               }

然后,我在 Form2 上创建了一个新的空字符串列表。将旧列表复制到新列表并将其转换为数组。

List<string> li=new List<string>();
li=List.GetList();
string[] temp=li.ToArray();

I want to transfer an array from Form1 to Form2, then on Form2 add some values to the array.
On Form1 button click I put this code:

        int[] arrayOfInt = new int[4];
        arrayOfInt[0] = 19;
        arrayOfInt[1] = 12;
        Form2 frm2 = new Form2();
        frm2.TakeThis(arrayOfInt);
        frm2.Show();

In the form2 i created TakeThis(int[]) that catches the values from the form1 and display the values of the array on a label. Now, i cannot figure how to add some values to array. For example arrayOfInt[2] and arrayOfInt[3] and send to Form3.

Edit:

I decided to use a List to store my data but at the end I must convert a
list to an array because I am doing some reporting and math operations with the data.

This example is different from the example above. In this store all inputs from textboxs,comboxes in the list. At the end i need to convert the list into an array.

What have I done,
I created a new global class:

static class List{

   static List<string> list;

   public static void NewList(){
   list=new List<string>();
   }

   public  static void Save(string value){
   list.Add(value);
   }

   public static void Display(){
   foreach(var value in list){
   MessageBox.Show(value);
   }
   }

}

The data is inserted between forms,

List.save(some_strings);
 ...

but at the end I need to convert the list in an array. I googled and I find the ToArray() function but i dont know how to use in my code. I tried with properties but i couldn't do the conversion. Please help.

Edit2
I found the solution.
In the global static class List I created a method that returns a List of strings:

public static List<string> GetList(){
              return list;
               }

Then, I created a new empty List of strings on Form2. Copied the old list to the new list and converted it in an array.

List<string> li=new List<string>();
li=List.GetList();
string[] temp=li.ToArray();

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

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

发布评论

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

评论(4

慕巷 2024-10-14 17:55:47

使用 List 并使用 oList.Add(item) 方法添加元素,因为数组的长度是固定的(您已经在初始化时指定了它的大小)
但是,如果您想不惜一切代价使用数组,则创建一个逻辑来创建一个新数组,其大小不超过旧+新添加元素的大小并返回该数组。

已更新
我相信您面临问题,因为您使用了 String 列表或 Int。

            List<int> oIntList = new List<int>();
            oIntList.Add(1);
            oIntList.Add(3);
            oIntList.Add(4);
            oIntList.Add(5);

            int[] oIntArr = oIntList.ToArray();

Use List<Int> and use oList.Add(item) method to add elements as array is Fixed in Lenght (you already giving it size at a time of initialization)
But if you want to use array at any cost then make a logic to create a new array upto the size of Old+ new added element and return that.

UPDATED
I believe you are facing problem because you have taken List of String instead or Int.

            List<int> oIntList = new List<int>();
            oIntList.Add(1);
            oIntList.Add(3);
            oIntList.Add(4);
            oIntList.Add(5);

            int[] oIntArr = oIntList.ToArray();
凉城已无爱 2024-10-14 17:55:46

您必须在 frm2.TakeThis() 方法上使用 ref 参数:

这是一篇关于它的 MSDN 文章:使用 ref 和 out 传递数组(C# 编程指南)

void TakeThis(ref Int32[] array)
{
    // Change array
}

并使用类似:

frm2.TakeThis(ref arrayOfInt);

如果要保留对数组的更改,则需要通过引用传递数组。

如果您不能使用Array,请使用Collection,例如System.Collections.Generic.List

You will have to use ref argument on frm2.TakeThis() method:

Here is an MSDN article on it : Passing Arrays Using ref and out (C# Programming Guide).

void TakeThis(ref Int32[] array)
{
    // Change array
}

and use like:

frm2.TakeThis(ref arrayOfInt);

Arrays need to passed by reference if you want to persist changes to them.

If you must not use an Array use a Collection like System.Collections.Generic.List<T>.

一世旳自豪 2024-10-14 17:55:45

而是使用 ArrayList 甚至更好的 IList
C# 中数组无法调整大小

Rather use ArrayList or even better IList<T>.
Arrays cannot be resized in C#

寒江雪… 2024-10-14 17:55:44

如果您想要一个需要添加项目的数据结构,请不要使用数组。

使用通用集合,例如 List

在您的情况下,整数列表将是 List

IList<int> listOfInt = new List<int>();
listOfInt.Add(19);
listOfInt.Add(12);
Form2 frm2 = new Form2();
frm2.TakeThis(listOfInt);
frm2.Show();

Form2 上时,您的 TakeThis 函数将如下所示:

public voidTakeThis(IList<int> listOfInt)
{
  listOfInt.Add(34);
}

当将列表传递到另一个表单时,这也将起作用,因为 List 是一个引用类型,而数组是值类型。如果您不知道这意味着什么,请参阅本文

Don't use arrays if you want a data structure that you need to add items to.

Use a generic collection like List<T>.

In your case, a list of integers would be a List<int>.

IList<int> listOfInt = new List<int>();
listOfInt.Add(19);
listOfInt.Add(12);
Form2 frm2 = new Form2();
frm2.TakeThis(listOfInt);
frm2.Show();

When on Form2, your TakeThis function would look like this:

public voidTakeThis(IList<int> listOfInt)
{
  listOfInt.Add(34);
}

This will also work when passing the list to another form, as List is a reference type whereas arrays are value types. If you don't know what this means, see this article.

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