我只是创建一个类的对象。此类的一个属性是另一个类的objcet列表。当我想从此列表中的第二个类转换对象时,将给出错误“对象引用未设置为对象的实例”。
这是第一堂课的代码:
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 :(
}
答案 0 :(得分:1)
我找到了答案。当我定义RCSection的新对象时,我应该定义与该对象相关的List
RCSection myRCSection= new RCSection<Bar>();
myRCSection.longitudinalBar = new List<Bar>();