如何正确添加新项目到集合?

时间:2015-04-26 04:45:49

标签: c# collections

这里我试图存储自定义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记录)

的代码
    //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;
    }

1 个答案:

答案 0 :(得分:0)

  1. 如果您只是在之后重新定义它,则不应该实例化thisDataPoint。
  2. getValue每次都返回对同一对象的引用。这意味着您将始终看到相同的数据。