C# 在列表中添加新项会将上一个值添加到上一个索引的列表中

C# 在列表中添加新项会将上一个值添加到上一个索引的列表中,c#,list,C#,List,这是我的代码,应该向列表中添加对象值 public List<tempTable> GetAttendanceLeaveReportForChart(double totalWorkingdays, double attendanceDays, double leaveData, double absentData) { List<tempTable> temp = new List<tempTable>(); for (int

这是我的代码,应该向列表中添加对象值

public List<tempTable> GetAttendanceLeaveReportForChart(double totalWorkingdays, double attendanceDays, double leaveData, double absentData) 
{       
    List<tempTable> temp = new List<tempTable>();
    for (int ct = 0; ct < 4; ct++)
    {
        if (ct == 0) 
        {
            tempTable tempTableForAttendAndLeaveReport = new tempTable();          
            tempTableForAttendAndLeaveReport.FieldName = "Total working days";
            tempTableForAttendAndLeaveReport.FieldValue = Convert.ToDouble(totalWorkingdays);
            temp.Add(tempTableForAttendAndLeaveReport); 
        }
        else if (ct == 1)
        {
            tempTable tempTableForAttendAndLeaveReport = new tempTable(); 
            tempTableForAttendAndLeaveReport.FieldName = "Attendance"; 
            tempTableForAttendAndLeaveReport.FieldValue = Convert.ToDouble(attendanceDays); 
            temp.Add(tempTableForAttendAndLeaveReport); 
        }
        else if (ct == 2)
        {
            tempTable tempTableForAttendAndLeaveReport = new tempTable();
            tempTableForAttendAndLeaveReport.FieldName = "Leave"; 
            tempTableForAttendAndLeaveReport.FieldValue = Convert.ToDouble(leaveData); 
            temp.Add(tempTableForAttendAndLeaveReport); 
        }
        else if (ct == 3) 
        {
            tempTable tempTableForAttendAndLeaveReport = new tempTable();
            tempTableForAttendAndLeaveReport.FieldName = "Absent";
            tempTableForAttendAndLeaveReport.FieldValue = Convert.ToDouble(absentData); 
            temp.Add(tempTableForAttendAndLeaveReport); 
        }
    }     
    return temp;
}
fieldValue
来自从参数接收的变量。但当循环第二次迭代时,在第一次循环中接收到的值在列表中被覆盖。在第二次迭代中,列表索引0和1处的值是添加到列表中的最新值。 我不知道这是怎么发生的


任何帮助。提前感谢

尝试使用不同的名称创建新的TESTERABLE变量,或者仅使用重新实例化的对象。在这里,您不需要if条件,因为在
for loop
中没有提到
ct

为了更好的可读性,不要使用冗长的变量:

 public List<tempTable> GetAttendanceLeaveReportForChart(double totalWorkingdays, double attendanceDays, double leaveData, double absentData) 
 { 
     List<tempTable> temp = new List<tempTable>();

     tempTable obj = new tempTable();          
     obj.FieldName = "Total working days";
     obj.FieldValue = Convert.ToDouble(totalWorkingdays);
     temp.Add(obj);        

     obj = new tempTable(); 
     obj.FieldName = "Attendance"; 
     obj.FieldValue = Convert.ToDouble(attendanceDays); 
     temp.Add(obj); 

     obj = new tempTable();
     obj.FieldName = "Leave"; 
     obj.FieldValue = Convert.ToDouble(leaveData); 
     temp.Add(obj);    

     obj = new tempTable();
     obj.FieldName = "Absent";
     obj.FieldValue = Convert.ToDouble(absentData); 
     temp.Add(obj);        

     return temp;
}

更改变量名,它就会工作。

从查看当前代码开始,您不需要循环,也不需要使用
ct
的值,也不需要
if
-语句。要获得相同的结果,您只需要以下代码:

public List<tempTable> GetAttendanceLeaveReportForChart(double totalWorkingdays, double attendanceDays, double leaveData, double absentData) 
{       
    List<tempTable> temp = new List<tempTable>();
    temp.add(new tempTable("Total working days", Convert.ToDouble(totalWorkingdays));
    //and etc for other 3 values
    return temp;
}
public List getAttendanceEleaverReportForChart(双倍总工作日、双倍attendanceDays、双倍休假数据、双倍缺席数据)
{       
列表温度=新列表();
临时添加(新工作日(“总工作日”,转换为双倍(总工作日));
//其他3个值的等
返回温度;
}
假设你有一个构造函数,你可以设置这两个值

解决方案


问题是,可诱惑的字段值是静态的,因此始终保留分配给它们的最后一个值。字段不应该是静态的。

“可诱惑的ForAttendandLeverReport”我正是这样做的。still temp返回4个列表,所有字段名为“缺席”和字段值作为absentData。我就是这么做的。still temp返回4个列表,所有字段名为“缺席”,字段值为absentData。公共类可诱惑{private static string\u FieldName;private static double\u FieldValue;公共字符串FieldName{get{return\u FieldName;}set{u FieldName=value;}公共双字段值{get{return}FieldValue;}set{{U FieldValue=value;}}}你的字段是
静态的
,这意味着它们对所有可诱惑的实例只有一个值-这是分配给它们的最后一个值。删除
静态的
关键字,它就会起作用。哦,伙计…DJ_polly,非常感谢。我挠头两天了。非常感谢..对于询问
可诱惑的
类和确定实际问题。
public class tempTable 
{ 
   private string _FieldName;   //don't use static
   private double _FieldValue;  //don't use static
   public string FieldName { get { return _FieldName; } set { _FieldName = value; } }
   public double FieldValue { get { return _FieldValue; } set { _FieldValue = value;} }     
}       
public List<tempTable> GetAttendanceLeaveReportForChart(double totalWorkingdays, double attendanceDays, double leaveData, double absentData) 
{       
    List<tempTable> temp = new List<tempTable>();
    temp.add(new tempTable("Total working days", Convert.ToDouble(totalWorkingdays));
    //and etc for other 3 values
    return temp;
}