需要帮助在循环内传递对象

时间:2015-01-14 07:07:22

标签: c# scope

我需要帮助将“dependentcombobox”传递给for循环。我得到的错误是“名称”dependentcombobox“在当前上下文中不存在”。我怎样才能进入for循环?我想尽量缩小范围。谢谢!语言是C#。

public frmMain() 
{
  InitializeComponent();

  populateDatacombobox(comboboxBlower1Series, comboboxBlower1Model);
  populateDatacombobox(comboboxBlower2Series, comboboxBlower2Model);
  populateDatacombobox(comboboxBlower3Series, comboboxBlower3Model);
}


public void populateDatacombobox(ComboBox independentcombobox, ComboBox dependentCombobox) 
{
  DataSet blowerData = Global.blowerData;

  DataTable blowerTable = blowerData.Tables["blower"];

  var comboBoxValues =
      (from records in blowerTable.AsEnumerable() where records.Field <
          string > ("SERIES") == independentcombobox.SelectedItem.ToString()
          select records.Field < string > ("MODEL"));

  foreach(var comboBoxValue in comboBoxValues) {
    dependentcombobox.Add(comboBoxValue);
  };

}

1 个答案:

答案 0 :(得分:2)

C#是case sensitive语言。因此dependentcombobox不等于dependentCombobox。使用in循环时,在编写dependentCombobox时,参数名称为dependentcombobox。而且你也不需要将终结符;放在循环体的末尾。

更正了块:

foreach (var comboBoxValue in comboBoxValues)
{
  dependentCombobox.Add(comboBoxValue);
}