対応する 3 つの列挙型の値を 3 つのリスト ボックスに入力しています。3 つの別個の非常に類似した方法を回避する方法はありますか? これが私が今持っているものです:
private void PopulateListBoxOne()
{
foreach (EnumOne one in Enum.GetValues(typeof(EnumOne)))
{
lstOne.Items.Add(one);
}
lstOne.SelectedIndex = 0;
}
private void PopulateListBoxTwo()
{
foreach (EnumTwo two in Enum.GetValues(typeof(EnumTwo)))
{
lstTwo.Items.Add(two);
}
lstTwo.SelectedIndex = 0;
}
private void PopulateListBoxThree()
{
foreach (EnumThree three in Enum.GetValues(typeof(EnumThree)))
{
lstThree.Items.Add(three);
}
lstThree.SelectedIndex = 0;
}
しかし、代わりに、次のような 1 つのメソッド (3 回呼び出すことができる) を使用することをお勧めします。
private void PopulateListBox(ListBox ListBoxName, Enum EnumName)
{
// ... code here!
}
私はかなり経験の浅いプログラマーなので、検索はしましたが、何を検索しているのかよくわかりませんでした。これが以前に回答されている場合はお詫びします。既存の回答が表示されることにも同様に感謝します。ありがとう!