使用List.add后所有数据都变成了最后一次添加的数据

skevil 2020-01-13 10:35:30
使用List.add后所有数据都变成了最后一次添加的数据,我在每次调用list.add前都new了一次,不知道为啥还是会把前面的数据都覆盖掉
我代码如下
 
public List<getCommentClass> old_comment_list = new List<getCommentClass>();

private void Button1_Click(object sender, EventArgs e)
{

getCommentClass old_comment_obj;

OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
old_file_path = ofd.FileName;
Console.WriteLine(ofd.FileName);

app_old = new Microsoft.Office.Interop.Excel.Application();
wbks_old = app_old.Workbooks;
_wbk_old = wbks_old.Add(old_file_path);
Sheets shs_old = _wbk_old.Sheets;
_Worksheet _wsh_old = (_Worksheet)shs_old.get_Item(1);

Console.WriteLine("old sheet count: " + shs_old.Count.ToString());
Console.WriteLine("_wsh_old :" + _wsh_old.Name);
//Range range1 = _wsh_old.Cells[3, 9];
//Console.WriteLine("_wsh_old.Cells :" + range1.Text);
//Console.WriteLine("_wsh_old.Cells.comment :" + range1.Comment.Text());
//return;


int shs_line_index = 2;
int shs_row_start = 2;
int shs_row_end = 22;
int shs_check_comment_row_index = 9;
int shs_start_check_comment_row = 9;
int shs_end_check_comment_row = 29;

button1.Text = "分析数据...";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;

//old_comment_list.Clear();

int need_to_import = 0;
while (true)
{


Console.WriteLine("shs_line_index: " + shs_line_index.ToString() +
"shs_row_start:" + shs_row_start.ToString());
Range range = _wsh_old.Cells[shs_line_index, shs_row_start];
if (range.Text == "")
{
break; //查询到为空时结束循环
}

Console.WriteLine("shs_line_index: " + shs_line_index.ToString() +
"shs_check_comment_mrow_index:" + shs_check_comment_row_index.ToString());
old_comment_obj = new getCommentClass();
List<Range> range_list = new List<Range>();
for (int i = 0; i < shs_end_check_comment_row - shs_start_check_comment_row; i++)
{
range_list.Add(_wsh_old.Cells[shs_line_index, shs_start_check_comment_row + i]);
}

old_comment_obj.cell_has_import = true;
if (old_comment_obj.process_comment(range_list))
{
old_comment_obj.cell_date_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_date_range_index].Text;
old_comment_obj.cell_client_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_client_range_index].Text;
old_comment_obj.cell_order_number_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_order_number_range_index].Text;
old_comment_obj.cell_type_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_type_range_index].Text;
old_comment_obj.cell_commodity_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_commodity_range_index].Text;
old_comment_obj.cell_number_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_number_range_index].Text;
old_comment_obj.cell_price_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_price_range_index].Text;
old_comment_obj.cell_has_import = false;
need_to_import++;
//old_comment_list[old_comment_list_count++] = old_comment_obj;
old_comment_list.Add(old_comment_obj);

Console.Write("comment_obj:--" + old_comment_obj.cell_date_range.ToString());
Console.Write("--" + old_comment_obj.cell_client_range.ToString());
Console.Write("--" + old_comment_obj.cell_order_number_range.ToString());
Console.Write("--" + old_comment_obj.cell_type_range.ToString());
Console.Write("--" + old_comment_obj.cell_commodity_range.ToString());
Console.Write("--" + old_comment_obj.cell_number_range.ToString());
Console.WriteLine("--" + old_comment_obj.cell_price_range.ToString());
}

shs_line_index++;
}


Console.WriteLine("old_comment_list.count: " + old_comment_list .Count.ToString() + "-----need_to_import:"+ need_to_import.ToString());
//Console.WriteLine("old_comment_list.count: " + old_comment_list_count.ToString() + "-----need_to_import:" + need_to_import.ToString());
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;

}


其中 getCommentClass 类实现如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;

namespace excel_import_data
{
public struct storage_comment_obj
{
public string str;
public int row_num;
};
//
// 摘要:
// 用于存放有批注的行,及相关匹配字符串内容信息
public class getCommentClass
{
public static bool has_comment ;
public static string _cell_date_range ;
public static string _cell_client_range;
public static string _cell_order_number_range;
public static string _cell_type_range ;
public static string _cell_commodity_range;
public static string _cell_number_range ;
public static string _cell_price_range;

//
// 摘要:
// 缓存元素
public List<storage_comment_obj> cell_group ;



//
// 摘要:
// 是否该行有批注
public bool cell_has_comment { get { return has_comment; } }
//
// 摘要:
// 是否已导入
public bool cell_has_import { get; set; }


//
// 摘要:
// 元素:日期信息 --- 第二列
public string cell_date_range
{
get { return _cell_date_range; }
set { _cell_date_range = value; }
}
//
// 摘要:
// 元素:日期信息索引 --- 第二列
public int cell_date_range_index { get { return 2; } }
//
// 摘要:
// 元素:客户信息 --- 第三列
public string cell_client_range
{
get { return _cell_client_range; }
set { _cell_client_range = value; }
}
//
// 摘要:
// 元素:客户信息索引 --- 第三列
public int cell_client_range_index { get { return 3; } }
//
// 摘要:
// 元素:订单号 --- 第四列
public string cell_order_number_range
{
get { return _cell_order_number_range; }
set { _cell_order_number_range = value; }
}
//
// 摘要:
// 元素:订单号索引 --- 第四列
public int cell_order_number_range_index { get { return 4; } }
//
// 摘要:
// 元素:车型 --- 第五列
public string cell_type_range
{
get { return _cell_type_range; }
set { _cell_type_range = value; }
}
//
// 摘要:
// 元素:车型索引 --- 第五列
public int cell_type_range_index { get { return 5; } }
//
// 摘要:
// 元素:商品名称 --- 第六列
public string cell_commodity_range
{
get { return _cell_commodity_range; }
set { _cell_commodity_range = value; }
}
//
// 摘要:
// 元素:商品名称索引 --- 第六列
public int cell_commodity_range_index { get { return 6; } }
//
// 摘要:
// 元素:数量 --- 第七列
public string cell_number_range
{
get { return _cell_number_range; }
set { _cell_number_range = value; }
}
//
// 摘要:
// 元素:数量名称索引 --- 第七列
public int cell_number_range_index { get { return 7; } }
//
// 摘要:
// 元素:销售单价 --- 第八列
public string cell_price_range
{
get { return _cell_price_range; }
set { _cell_price_range = value; }
}
//
// 摘要:
// 元素:销售单价索引 --- 第八列
public int cell_price_range_index { get { return 8; } }









//
// 摘要:
// 最大缓存元素22个
public int cell_max_num { get { return 22; } }


//
// 摘要:
// 载入数据
public bool process_comment(List<Range> range_list)
{
int shs_start_check_comment_row = 9;
has_comment = false;
for (int index = 0; index < range_list.Count; index++)
{
if (range_list[index].Comment != null)
{
has_comment = true;
storage_comment_obj obj = new storage_comment_obj();
obj.row_num = index + shs_start_check_comment_row;
obj.str = range_list[index].Comment.Text();
cell_group.Add(obj);
}
}
return has_comment;
}

public getCommentClass()
{

cell_group = new List<storage_comment_obj>();
}

}
}



...全文
1082 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Uncle-黄 2020-01-17
  • 打赏
  • 举报
回复
引用 14 楼 skevil 的回复:
[quote=引用 13 楼 正怒月神 的回复:]
getCommentClass 
所有static 去掉

public static bool   has_comment ;
        public static string _cell_date_range ;
        public static string _cell_client_range;
        public static string _cell_order_number_range;
        public static string _cell_type_range ;
        public static string _cell_commodity_range;
        public static string _cell_number_range  ;
        public static string _cell_price_range;
 

谢谢,还真是这个问题[/quote]我也再一次注意到了静态static谢谢
流颜飞羽 2020-01-15
  • 打赏
  • 举报
回复
是不是线程的问题
正怒月神 2020-01-14
  • 打赏
  • 举报
回复
你说的哪个list? old_comment_obj??? range_list???
skevil 2020-01-14
  • 打赏
  • 举报
回复
引用 13 楼 正怒月神 的回复:
getCommentClass 
所有static 去掉

public static bool   has_comment ;
        public static string _cell_date_range ;
        public static string _cell_client_range;
        public static string _cell_order_number_range;
        public static string _cell_type_range ;
        public static string _cell_commodity_range;
        public static string _cell_number_range  ;
        public static string _cell_price_range;
 

谢谢,还真是这个问题
正怒月神 2020-01-14
  • 打赏
  • 举报
回复
getCommentClass  所有static 去掉
public static bool   has_comment ;
        public static string _cell_date_range ;
        public static string _cell_client_range;
        public static string _cell_order_number_range;
        public static string _cell_type_range ;
        public static string _cell_commodity_range;
        public static string _cell_number_range  ;
        public static string _cell_price_range;
 
skevil 2020-01-14
  • 打赏
  • 举报
回复
引用 11 楼 正怒月神 的回复:
你说的哪个list?
old_comment_obj???
range_list???
old_comment_obj
skevil 2020-01-13
  • 打赏
  • 举报
回复
引用 5 楼 datafansbj 的回复:
old_comment_obj 应定义在 While(true) 循环体里边,不要定义在外边。

我放在循环里面也是一样,所以有点懵
牧歌ing 2020-01-13
  • 打赏
  • 举报
回复
上面这种情况就会有问题,你要在循环里面new
牧歌ing 2020-01-13
  • 打赏
  • 举报
回复
List<Data> d = new List<Data>();
Data data3 = new Data();
for (int i = 0; i < 100; i++)
{
data3.Money = i;
d.Add(data3);
}


datafansbj 2020-01-13
  • 打赏
  • 举报
回复
old_comment_obj 应定义在 While(true) 循环体里边,不要定义在外边。
  • 打赏
  • 举报
回复
断点跟踪一下ADD中U0看一下.
姎楹 2020-01-13
  • 打赏
  • 举报
回复

public List<Tree> UserList()
        {
            cmd.Connection = conn; //设置数据链接
            cmd.CommandText = "select ID,name,pid from Tree"; //设置查询语句
            conn.Open(); //打开数据库连接
            SqlDataReader dr = cmd.ExecuteReader(); //执行Reader查询,存入dr

            List<Tree> u1 = new List<Tree>(); //新建一个User类的集合

            while (dr.Read())//遍历dr
            {
                Tree u0 = new Tree(); //临时User类变量u0

                //把查询的当前记录各字段值赋值给对于的u0的属性
                u0.ID = Convert.ToInt32(dr["ID"]);
                u0.PID = Convert.ToInt32(dr["PID"].ToString());
                u0.NAME = dr["NAME"].ToString();

                u1.Add(u0);//把有数据的u0加入到User类的集合
            }
            conn.Close(); //关闭数据库连接

            //循环结束后,每一个用户数据都会被加入到用户类集合u1,最后返回u1
            return u1;

        }
一支糖包仔 2020-01-13
  • 打赏
  • 举报
回复
是哪一个list呢?我看到有好几个呀。
skevil 2020-01-13
  • 打赏
  • 举报
回复
引用 9 楼 datafansbj 的回复:
[quote=引用 8 楼 skevil 的回复:]
[quote=引用 5 楼 datafansbj 的回复:]
old_comment_obj 应定义在 While(true) 循环体里边,不要定义在外边。

我放在循环里面也是一样,所以有点懵[/quote]

你是怎么放在循环体里面的?不是仅在循环体里面 new,而是在循环体里面定义并 new。建议你先了解一下变量的作用域。
[/quote]

public List<getCommentClass> old_comment_list = new List<getCommentClass>();

private void Button1_Click(object sender, EventArgs e)
{

.....

while (true)
{

getCommentClass old_comment_obj = new getCommentClass();
Console.WriteLine("shs_line_index: " + shs_line_index.ToString() +
"shs_row_start:" + shs_row_start.ToString());
Range range = _wsh_old.Cells[shs_line_index, shs_row_start];
if (range.Text == "")
{
break; //查询到为空时结束循环
}

Console.WriteLine("shs_line_index: " + shs_line_index.ToString() +
"shs_check_comment_mrow_index:" + shs_check_comment_row_index.ToString());

List<Range> range_list = new List<Range>();
for (int i = 0; i < shs_end_check_comment_row - shs_start_check_comment_row; i++)
{
range_list.Add(_wsh_old.Cells[shs_line_index, shs_start_check_comment_row + i]);
}

old_comment_obj.cell_has_import = true;
if (old_comment_obj.process_comment(range_list))
{
old_comment_obj.cell_date_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_date_range_index].Text;
old_comment_obj.cell_client_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_client_range_index].Text;
old_comment_obj.cell_order_number_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_order_number_range_index].Text;
old_comment_obj.cell_type_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_type_range_index].Text;
old_comment_obj.cell_commodity_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_commodity_range_index].Text;
old_comment_obj.cell_number_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_number_range_index].Text;
old_comment_obj.cell_price_range = _wsh_old.Cells[shs_line_index, old_comment_obj.cell_price_range_index].Text;
old_comment_obj.cell_has_import = false;
need_to_import++;
//old_comment_list[old_comment_list_count++] = old_comment_obj;
old_comment_list.Add(old_comment_obj);

Console.Write("comment_obj:--" + old_comment_obj.cell_date_range.ToString());
Console.Write("--" + old_comment_obj.cell_client_range.ToString());
Console.Write("--" + old_comment_obj.cell_order_number_range.ToString());
Console.Write("--" + old_comment_obj.cell_type_range.ToString());
Console.Write("--" + old_comment_obj.cell_commodity_range.ToString());
Console.Write("--" + old_comment_obj.cell_number_range.ToString());
Console.WriteLine("--" + old_comment_obj.cell_price_range.ToString());
}

shs_line_index++;
}


Console.WriteLine("old_comment_list.count: " + old_comment_list .Count.ToString() + "-----need_to_import:"+ need_to_import.ToString());
//Console.WriteLine("old_comment_list.count: " + old_comment_list_count.ToString() + "-----need_to_import:" + need_to_import.ToString());
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;

}

datafansbj 2020-01-13
  • 打赏
  • 举报
回复
引用 8 楼 skevil 的回复:
[quote=引用 5 楼 datafansbj 的回复:]
old_comment_obj 应定义在 While(true) 循环体里边,不要定义在外边。

我放在循环里面也是一样,所以有点懵[/quote]

你是怎么放在循环体里面的?不是仅在循环体里面 new,而是在循环体里面定义并 new。建议你先了解一下变量的作用域。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧