【发布时间】:2012-11-07 18:22:16
【问题描述】:
我创建了几个自定义类(NTDropDown 和NTBaseFreight),用于存储从数据库中检索的数据。我初始化了NTBaseFreight 的列表和NTDropDown 的2 个列表。
我可以成功使用List.Add 将运费添加到运费列表中,但是在我调试代码时,我的 2 个下拉列表仅包含 1 个 NTDropDown,它始终具有与 NTDropDown 相同的值(我是假设这是一个引用问题,但我做错了什么)?
举个例子,在第二行,如果运营商和carrier_label 是"001", "MyTruckingCompany",并且我在frt_carriers 的 if 语句上打了个断点,那么 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);
}
【问题讨论】:
-
好的,我想通了...我每次都需要初始化一个新的 NTDropDown(不要一遍又一遍地重复使用 tempDropDown)。所以,在我每次使用它之前添加
tempDropDown = new NTDropDrop();。我应该删除这个问题吗? -
没有。留下问题。解决自己的问题对每个人仍然有用。