我创建了几个自定义类 (NTDropDown
和NTBaseFreight
),用于存储从数据库中检索到的数据。我初始化一个列表NTBaseFreight
和 2 个列表NTDropDown
。
我可以成功地使用List.Add
将运费添加到运费列表,但是当我调试代码时,我的 2 个下拉列表只包含 1 NTDropDown
,它始终具有相同的值NTDropDown
(我假设这是一个引用问题,但我在做什么错误的)?
举个例子,在第二行,如果承运人和我carrier_label
在"001", "MyTruckingCompany"
if 语句上打断 for frt_carriers
,则 frt_carriers 和 frt_modes 都将在其列表中仅包含一项,其值"001", "MyTruckingCompany"
...与 中的值相同NTDropDown
。
代码:
List<NTDropDown> frt_carriers = new List<NTDropDown>();
List<NTDropDown> frt_modes = new List<NTDropDown>();
List<NTBaseFreight> freights = new List<NTBaseFreight>();
NTDropDown tempDropDown = new NTDropDown();
NTBaseFreight tempFreight = new NTBaseFreight();
//....Code to grab data from the DB...removed
while (myReader.Read())
{
tempFreight = readBaseFreight((IDataRecord)myReader);
//check if the carrier and mode are in the dropdown list (add them if not)
tempDropDown.value = tempFreight.carrier;
tempDropDown.label = tempFreight.carrier_label;
if (!frt_carriers.Contains(tempDropDown)) frt_carriers.Add(tempDropDown);
tempDropDown.value = tempFreight.mode;
tempDropDown.label = tempFreight.mode_label;
if (!frt_modes.Contains(tempDropDown)) frt_modes.Add(tempDropDown);
//Add the freight to the list
freights.Add(tempFreight);
}