我找到了答案。当我定义RCS部分的新对象时,我应该定义与该对象相关的列表
我只是创建了一个类的对象。这个类的属性之一是另一个类的对象列表。当我想从该列表中的第二个类中强制转换对象时,将给出错误"对象引用未设置为对象实例"。
这是第一类的代码:
public class RCSection<Bar>
{
private string RCSectionName;
private int NumberOfBars;
private double NumberOfInnerBars;
private double NumberOfOuterBars;
private double TransverseSpacing;
private Steel LongitudinalSteel;
private Steel TransevrseSteel;
private Concrete Concrete;
private List<Bar> LongitudinalBar;
private Bar TransverseBar;
private Section Section;
public string rCSectionName
{
set { RCSectionName = value; }
get { return RCSectionName; }
}
public int numberOfBars
{
set { NumberOfBars = value; }
get { return NumberOfBars; }
}
public double transverseSpacing
{
set { TransverseSpacing = value; }
get { return TransverseSpacing; }
}
public double numberOfInnerBars
{
set { NumberOfInnerBars = value; }
get { return NumberOfInnerBars; }
}
public double numberOfOuterBars
{
set { NumberOfOuterBars = value; }
get { return NumberOfOuterBars; }
}
public Steel longitudinalSteel
{
set { LongitudinalSteel = value; }
get { return LongitudinalSteel; }
}
public Steel transverseSteel
{
set { TransevrseSteel = value; }
get { return TransevrseSteel; }
}
public Concrete concrete
{
set { Concrete = value; }
get { return Concrete; }
}
public List<Bar> longitudinalBar
{
set { LongitudinalBar = value; }
get { return LongitudinalBar; }
}
public Bar transverseBar
{
set { TransverseBar = value; }
get { return TransverseBar; }
}
public Section section
{
set { Section = value; }
get { return Section; }
}
}
一开始我想知道,我为列表创建属性的方式,对吗?!在那之后,下面的代码与这个类的对象的使用以及在其中投射一个对象有关
for (int i = 0; i < myRCSection.numberOfBars; i++)
{
Bar mybar = new Bar(newFormRCSection.comboBoxSteelSize1.Text,"SI");
myRCSection.longitudinalBar[i] = mybar;//Error will appear here :(
}
RCSection myRCSection= new RCSection<Bar>();
myRCSection.longitudinalBar = new List<Bar>();