0

我只是创建一个类的对象。此类的属性之一是另一个类的对象列表。当我想从这个列表中的第二个类中转换一个对象时,将给出此错误“对象引用未设置为对象的实例”。

这是第一堂课的代码:

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 :(

  }
4

1 回答 1

2

我找到了答案。当我定义 RCSection 的新对象时,我应该定义与该对象相关的列表

RCSection myRCSection= new RCSection<Bar>();
myRCSection.longitudinalBar = new List<Bar>();
于 2012-09-06T13:16:28.510 回答