【问题标题】:Is it possible to store a multi-dimensional array to an object?是否可以将多维数组存储到对象中?
【发布时间】:2020-12-27 12:40:40
【问题描述】:

这是我的课程:

public class Type_WorkSched
{
    public string EmployeeID { get; set; }
    public string RecordID { get; set; }
    public Type_Schedule[] Schedule { get; set; }
}

public class Type_Schedule
{
    public string Days { get; set; }
    public string TimeIn_AM { get; set; }
    public string TimeOut_AM { get; set; }
    public string TimeIn_PM { get; set; }
    public string TimeOut_PM { get; set; }
}

我想将MySchedule 的数组存储到对象MyWorkSched 中。

但我只得到数组的最后一个值。

GV_WorkSched 是数据网格的名称。

Type_Schedule[] MySchedule = new Type_Schedule[GV_WorkSched.RowCount-1];
Type_WorkSched MyWorkSched = new Type_WorkSched();
           
MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
MyWorkSched.RecordID = Txt_RecordNumber.Text;

for (int i = 0; i < GV_WorkSched.RowCount-1; i++)
{
    MySchedule[i] = new Type_Schedule();
    MySchedule[i].Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "Days"));
    MySchedule[i].TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_AM"));
    MySchedule[i].TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_AM"));
    MySchedule[i].TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_PM"));
    MySchedule[i].TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_PM"));
                
    MyWorkSched.Schedule = new Type_Schedule[] { MySchedule[i] };
}

或者有其他方法吗?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    你应该这样做

            Type_Schedule[] MySchedule = new Type_Schedule[GV_WorkSched.RowCount];
            Type_WorkSched MyWorkSched = new Type_WorkSched();
           
            MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
            MyWorkSched.RecordID = Txt_RecordNumber.Text;
    
            for (int i = 0; i < GV_WorkSched.RowCount; i++)
            {
                MySchedule[i] = new Type_Schedule();
                MySchedule[i].Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "Days"));
                MySchedule[i].TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_AM"));
                MySchedule[i].TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_AM"));
                MySchedule[i].TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_PM"));
                MySchedule[i].TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_PM"));
            }
    
            MyWorkSched.Schedule = MySchedule;
    

    【讨论】:

    • @RomanRyzhiy 它是循环中填充数据的数组
    • 你不能给MySchedule和索引[i],因为它不在循环中
    • @sin 在upvote下面有一个灰色的勾号✔点击它,它应该变成绿色
    • @sim 请注意,您已经将另一个答案标记为正确(尽管我认为它不正确:耸耸肩:)
    【解决方案2】:
            Type_Schedule[] MySchedule = new Type_Schedule[GV_WorkSched.RowCount-1];
                        Type_WorkSched MyWorkSched = new Type_WorkSched();
                       
                            MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
                            MyWorkSched.RecordID = Txt_RecordNumber.Text;
                       
        MyWorkSched.Schedule = new Type_Schedule[] { 
        new Type_Schedule {
                        Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "Days")),
                        TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeIn_AM")),
                        TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeOut_AM")),
                        TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeIn_PM")),
                        TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeOut_PM"))
                     };
         };
    Since you are any ways trying to fetch the last value only why do you want to reassign a new Type_Schedule array every time in the loop.Avoid the loop  and if  you want to store the entire schedule data then use a loop.
    MyWorkSched.Schedule = new Type_Schedule[] { MySchedule[i] };// this line if inside the loop at the end of the loop will give the same result as above 
    

    【讨论】:

      【解决方案3】:

      你的问题出在这一行

      MyWorkSched.Schedule = new Type_Schedule[] { MySchedule[i] }
      

      在每次迭代结束时,您都会创建一个新数组,其中只有一个值(最新的)。当你摆脱它时,这是唯一剩下的。

      试试下面的。我完全删除了中间数组,因为我认为这是混乱的一部分。

      
          Type_WorkSched MyWorkSched = new Type_WorkSched();
          MyWorkSched.Schedule = new Type_Schedule[GV_WorkSched.RowCount];
                 
          MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
          MyWorkSched.RecordID = Txt_RecordNumber.Text;
          for (int i = 0; i < GV_WorkSched.RowCount-1; i++)
          {
               MyWorkSched.Schedule[i] = new Type_Schedule();
               MyWorkSched.Schedule[i].Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "Days"));
               MyWorkSched.Schedule[i].TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_AM"));
               MyWorkSched.Schedule[i].TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_AM"));
               MyWorkSched.Schedule[i].TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_PM"));
               MyWorkSched.Schedule[i].TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_PM"));
        }
      

      【讨论】:

      • 不会工作的人..因为我想要返回值MyWorkSched 而不是MySchedule 这就是为什么我想将MySchedule 的值存储到MyWorkSched。
      • 我直接保存在MyWorkSchedule,返回它,它就会有MyWorkSchedule.Schedule中的值
      • 它返回:System.NullReferenceException: '对象引用未设置为对象的实例。'
      • 哪一行?顺便说一句,我将数组初始化更改为正确的大小,即Count,而不是Count-1
      【解决方案4】:

      我建议改用List&lt;&gt;。首先,将您的类型重新声明为

      public class Type_WorkSched
      {
          public string EmployeeID { get; set; }
          public string RecordID { get; set; }
          public List<Type_Schedule> Schedule { get; set; }
      
          // In case you really need it as Array
          public Type_Schedule[] ScheduleAsArray()
          {
              return Schedule.ToArray();
          }
      }
      

      其次,以foreach 方式迭代你的网格

      foreach (DataGridViewRow row in GV_WorkSched.Rows)
      {
      
          var schedule = new Type_Schedule
          {
              Days = row.Cells["Days"].Value.toString(),
              //...
          };
      
          MyWorkSched.Schedule.Add(schedule);
      }
      

      【讨论】:

      • 您正在使用更多的空间和时间进行转换。当您有固定大小时,使用数组总是更好,而且避免转换当然更好。
      • @AthanasiosKataras 不,我不是。从 .Net 源代码中我们知道,List 无论如何都是作为 Array 存储、操作和优化的,并且它的转换没有任何成本。
      • 当然有:stackoverflow.com/questions/1147497/… 它没有bad 的befrormance,但它仍然迭代并将项目从一个集合复制到另一个集合。
      • @AthanasiosKataras 不,它不会迭代任何东西。 Array.Copy(在您的参考中)“相当于标准 C/C++ 函数 memmove”docs.microsoft.com/en-us/dotnet/api/…
      • 来自您的链接:此方法是 O(n) 操作,其中 n 是长度。就像从一个列表迭代和复制到一个数组一样。所以没有成本的部分是不正确的。它确实花费了 O(n)。如果它确实花费了 O(1),我会同意你的观点,它可以忽略不计,但事实并非如此。如果您考虑一下,它也会使数组变得无用,不是吗?
      猜你喜欢
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 2015-05-27
      • 2019-06-08
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多