传递形式之间阵列和阵列中的管理 [英] Passing array between forms and managing in arrays

查看:91
本文介绍了传递形式之间阵列和阵列中的管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Form1中传递一个数组到窗体2,然后在Form2的一些值添加到阵列中。
在Form1上按一下按钮,我把这个code:

  INT [] = arrayOfInt新INT [4];
        arrayOfInt [0] = 19;
        arrayOfInt [1] = 12;
        窗体2 FRM2 =新Form2的();
        frm2.TakeThis(arrayOfInt);
        frm2.Show();

在我创建的窗体2 TAKETHIS(INT [])映入从Form1中的值和标签上显示数组的值。现在,我找不出如何一些值添加到阵列。例如 arrayOfInt [2] arrayOfInt [3] 并传送到Form3。

修改

我决定用一个列表来存储我的数据,但最后我必须转换
清单阵列,因为我做的一些数据报告和数学运算。

这个例子是从上面的例子一样。在这家店从textboxs所有输入,comboxes在列表中。最后我需要将列表转换为数组。

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

 静态类列表{   静态列表<串GT;清单;   公共静态无效NewList(){
   名单=新名单,LT;串>();
   }   公共静态无效保存(字符串值){
   list.Add(值);
   }   公共静态无效的显示(){
   的foreach(列表中的VAR值){
   MessageBox.Show(值);
   }
   }}

将数据插入表单之间,

  List.save(some_strings);
 ...

但最后我需要将列表转换在数组中。我一派,我找到 ToArray的()函数,但我不知道如何在我的code使用。我试着用属性,但我不能做转换。请帮助。

EDIT2
我找到了解决办法。
在全球静态类名单,我创建一个返回字符串列表的方法:

 公共静态列表<串GT;的GetList(){
              返回列表;
               }

然后,我创建Form2上一个新的字符串空列表。复制旧列表新列表和数组转换它。

 列表<串GT; LI =新的List<串GT;();
李= List.GetList();
字符串[] TEMP = li.ToArray();


解决方案

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

使用泛型集合如 列表< T>

在你的情况,整数列表将是列表与LT; INT方式>

 的IList< INT> listOfInt =新的List< INT>();
listOfInt.Add(19);
listOfInt.Add(12);
窗体2 FRM2 =新Form2的();
frm2.TakeThis(listOfInt);
frm2.Show();

在对窗体2 ,您 TAKETHIS 函数应该是这样的:

 公共voidTakeThis(IList的< INT> listOfInt)
{
  listOfInt.Add(34);
}

传递列表时,为另一种形式,如列表这也将工作是引用类型,而数组是值类型。如果你不知道这是什么意思,请参见这篇文章

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();

解决方案

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.

这篇关于传递形式之间阵列和阵列中的管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆