如何正确地将新项目添加到集合中?
How to properly add new items to a collection?
我在这里尝试存储自定义 WindDataPoint 类型的数据点负载。
但是,我最近发现我的代码一直在创建数以万计的重复数据点。数据点更改为最新值,是的,但不是添加新数据点,而是将所有数据点也设置为该值。
这里是关注的代码:
private void Timer_Data_Tick(object sender)
{
if (!Timer_Data_Enabled)
return;
for (int i = 0; (itsDAQ.getStreamCount() > 0) && (i < 6); i++)
{
WindDAQ.WindDataPoint thisDataPoint = new WindDAQ.WindDataPoint();
thisDataPoint = itsDAQ.getValue(Recording);
dataPointCollection.Add(thisDataPoint);
newChartPoint = true;
}
}
这是 getValue() 和 getValue(bool record) 的代码
//Get Real-world values
public WindDataPoint getValue()
{
holdDequeueValue = DAQStream.Dequeue();
holdWindDataPoint.Lift = Lift_Sensor.getForce(holdDequeueValue[ChannelOutOrder[0]]);
holdWindDataPoint.Drag = Drag_Sensor.getForce(holdDequeueValue[ChannelOutOrder[1]]);
holdWindDataPoint.Velocity = Pitot_Sensor.getVelocity(holdDequeueValue[ChannelOutOrder[2]]);
holdWindDataPoint.isRecorded = false;
//This translates the number of samples since start into actual time since start
//Why not get current time? I don't want the current time. I want the time the sample was taken.
holdWindDataPoint.Time = SamplesToTime(SamplesReadSinceStart);
SamplesReadSinceStart++;
return holdWindDataPoint;
}
//Get Read-world values and set whether the sample is recorded.
public WindDataPoint getValue(bool record)
{
getValue();
holdWindDataPoint.isRecorded = record;
return holdWindDataPoint;
}
- 如果你只是想在之后重新定义它,你不应该实例化 thisDataPoint。
- getValue 每次都返回对同一对象的引用。这意味着您将始终看到相同的数据。
我在这里尝试存储自定义 WindDataPoint 类型的数据点负载。
但是,我最近发现我的代码一直在创建数以万计的重复数据点。数据点更改为最新值,是的,但不是添加新数据点,而是将所有数据点也设置为该值。
这里是关注的代码:
private void Timer_Data_Tick(object sender)
{
if (!Timer_Data_Enabled)
return;
for (int i = 0; (itsDAQ.getStreamCount() > 0) && (i < 6); i++)
{
WindDAQ.WindDataPoint thisDataPoint = new WindDAQ.WindDataPoint();
thisDataPoint = itsDAQ.getValue(Recording);
dataPointCollection.Add(thisDataPoint);
newChartPoint = true;
}
}
这是 getValue() 和 getValue(bool record) 的代码
//Get Real-world values
public WindDataPoint getValue()
{
holdDequeueValue = DAQStream.Dequeue();
holdWindDataPoint.Lift = Lift_Sensor.getForce(holdDequeueValue[ChannelOutOrder[0]]);
holdWindDataPoint.Drag = Drag_Sensor.getForce(holdDequeueValue[ChannelOutOrder[1]]);
holdWindDataPoint.Velocity = Pitot_Sensor.getVelocity(holdDequeueValue[ChannelOutOrder[2]]);
holdWindDataPoint.isRecorded = false;
//This translates the number of samples since start into actual time since start
//Why not get current time? I don't want the current time. I want the time the sample was taken.
holdWindDataPoint.Time = SamplesToTime(SamplesReadSinceStart);
SamplesReadSinceStart++;
return holdWindDataPoint;
}
//Get Read-world values and set whether the sample is recorded.
public WindDataPoint getValue(bool record)
{
getValue();
holdWindDataPoint.isRecorded = record;
return holdWindDataPoint;
}
- 如果你只是想在之后重新定义它,你不应该实例化 thisDataPoint。
- getValue 每次都返回对同一对象的引用。这意味着您将始终看到相同的数据。