gpt4 book ai didi

list - 对象引用未设置为对象的实例- “Object includes a list property”

转载 作者:行者123 更新时间:2023-12-03 08:15:17 25 4
gpt4 key购买 nike

我只是创建一个类的对象。该类的属性之一是另一个类的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 :(

}

最佳答案

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

RCSection myRCSection= new RCSection<Bar>();
myRCSection.longitudinalBar = new List<Bar>();

关于list - 对象引用未设置为对象的实例- “Object includes a list property”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com