我想在项目中使用通用列表而不是数组. [英] I want to use generic list in my project instead of array.

查看:79
本文介绍了我想在项目中使用通用列表而不是数组.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我想在项目中使用通用列表而不是array.任何人都可以在我的代码中进行修改.这是我的代码.

Hello there,
I want to use generic list in my project instead of array.can anyone do modification in my code.Here is my code.

ClsConnection.Conn.Open();
               SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", ClsConnection.Conn);
               //SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", con);
               SqlDataReader dr1 = cmd1.ExecuteReader();

               //comboBox2.Items.Clear();
               int cnt1 = 0;
               string[,] arr1 = new string[100, 100];
               ArrayList subjects1 = new ArrayList();
               subjects1.Add(new AddValue("select ", 0));
               while (dr1.Read())
               {
                   arr1[cnt1, 0] = dr1[0].ToString();
                   arr1[cnt1, 1] = dr1[1].ToString();
                   subjects1.Add(new AddValue(arr1[cnt1, 1].ToString(), Convert.ToInt32(arr1[cnt1, 0])));
                   cnt1++;
               }
               comboBox2.DataSource = subjects1;
               // dataGridView1.DataSource = subjects1;
               this.comboBox2.DisplayMember = "Display";
               this.comboBox2.ValueMember = "Value";
               if (comboBox2.Items.Count == 2)
               {
                   comboBox2.Text = "";
                   comboBox2.SelectedText = (arr1[0, 2]);
               }
               comboBox2.Refresh();
               ClsConnection.Conn.Close();


请帮忙.
谢谢


Please help.
Thanks

推荐答案

您可以使用列表System.Collections.Generic.List<System.Collections.Generic.List<string>>来实现二维字符串数组的功能.
但是,此类型将比string[,]更加灵活.首先,它将代表锯齿状数组",更像string[][],其中内部数组的每个实例可以具有不同的长度,但是您也可以轻松地插入和删除元素.

请参见 http://en.wikipedia.org/wiki/Jagged_array [
You can implement the functionality of the two-dimensional array of strings using the list System.Collections.Generic.List<System.Collections.Generic.List<string>>.

However, this type will be more flexible then string[,]. First of, all, it will represent "jagged array", more like string[][] where each instance of an inner array could be of a different length, but you can also easily insert and delete elements.

See http://en.wikipedia.org/wiki/Jagged_array[^].

—SA


据我在这段代码片段中所看到的,字符串[,] arr1是一个完全的矫kill过正:您仅使用[..., 0],[...,1]和[...,2].

此外,[...,2]从未分配,但您在设置选择时使用它-都不起作用.

忘记这个二维数组,对当前读取的项目使用简单的简单实例.您可能还记得第一个要选择的内容,但这可以通过在组合框中选择第一个元素来完成.


现在您已经厌倦了版本,这里是一种可能的方法:

As far as I can see in this code snippet, the string[,] arr1 is a complete overkill: you only use [...,0], [...,1], and [...,2].

In addition, the [...,2] is never assigned, but you use it in setting the selection - won''t work neither.

Forget about this two-dimensional array and use plain simple instances for the currently read items. You might remember the first for selection, but this could be done by simply select the first element in the combo box.


Now that you have tired your version, here a possible approach:

List<Tuple<string, int>> data = new List<Tuple<string, int>>()
                  { new Tuple<string, int>("select", 0) };
...
var conn = ClsConnection.Conn;
using (SqlCommand cmd = new SqlCommand("SELECT * FROM gen_Master", conn))
using (SqlDataReader dr = cmd.ExecuteReader())
{
    while (dr.Read())
    {
        data.Add(new Tuple<string, int>(dr[1].ToString(),
                                        int.Parse(dr[0].ToString())));
    }
}
comboBox1.DataSource = data;
comboBox1.DisplayMember = "Item1";
comboBox1.ValueMember = "Item2";
comboBox1.SelectedIndex = comboBox1.Items.Count > 1 ? 1 : 0;



干杯

Andi



Cheers

Andi


如果您使用.Net3.5(或更高版本)进行编码,那么如何使用Linq?

if you coding with .Net3.5(or higher),how about using Linq?

using System.Linq;
using System.Collections.Generic;
//...
ClsConnection.Conn.Open();
SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", clsConnection.Conn);
DataTable dt = new DataTable();
dt.Load(cmd1.ExecuteReader());
List<AddValue> datas = new List<AddValue>();
datas.Add(new AddValue("Select", 0));
datas.AddRange(dt.AsEnumerable().Select(x => new AddValue(x[0].ToString(), Convert.ToInt32(x[1]))));
comboBox2.DataSource = datas;



othrewise



othrewise

ClsConnection.Conn.Open();
SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", ClsConnection.Conn);
SqlDataReader dr1 = cmd1.ExecuteReader();
//int cnt1 = 0;
//string[,] arr1 = new string[100, 100];
//ArrayList subjects1 = new ArrayList();
//instead of ArrayList
List<addvalue> subjects1 = new List<addvalue>();
subjects1.Add(new AddValue("select ", 0));
while (dr1.Read())
{
    //arr1[cnt1, 0] = dr1[0].ToString();
    //arr1[cnt1, 1] = dr1[1].ToString();
    //subjects1.Add(new AddValue(arr1[cnt1, 1].ToString(), Convert.ToInt32(arr1[cnt1, 0])));
    subjects1.Add(new AddValue(dr1[1].ToString(), Convert.ToInt32(dr1[0].ToString())));
    //cnt1++;
}
comboBox2.DataSource = subjects1;


这篇关于我想在项目中使用通用列表而不是数组.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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