【发布时间】:2019-01-15 09:11:46
【问题描述】:
我有一个有 2 个列表的类作为它的数据成员。我想将这些数据成员作为不同的类对象。
我收到此错误:
“对象引用未设置为对象的实例。”
请告知正确的方法。
class dataPoints
{
public List<double> dataValues;
public List<DateTime> timeStamps;
public dataPoints()
{
this.timeStamps = new List<DateTime>();
this.dataValues = new List<double>();
}
}
//I want an object of dataPoints in this below classs
class wellGraph
{
int seriesAdded;
dataPoints[] graphDataPoints;
public wellGraph(int Entries)
{
this.seriesAdded = 0;
this.graphDataPoints = new dataPoints[Entries];
for(int i=0;i<Entries;i++)
{
graphDataPoints[i].timeStamps = new List<DateTime>();
graphDataPoints[i].dataValues = new List<double>();
}
}
}
在 dataPoints 类中删除构造函数后,同样的错误仍然存在。
【问题讨论】:
-
你永远不会初始化数组的元素。在设置字段值之前,您需要
graphDataPoints[i] = new dataPoints();。我还强烈建议:a) 您遵循 .NET 命名约定; b)您使用属性而不是公共字段。