Answered by:
Passing parameters to a method

Question
-
Is there any way to get a method to recognise different List controls, At present I'm having to write three almost identical methods to transfer a list of items from a list box to : -
a) a combobox,
b) another listbox
c) a checkedlistbox
private void ListToComboCopy(ref ListBox LBx, ref ComboBox CBx)
{
int T;
T = LBx.Items.Count;
CBx.Items.Clear();
for (int i = 0; i < T; i++)
{
CBx.Items.Add(LBx.Items[i]);
}
}
private void ListToCheckListCopy(ref ListBox LBx, ref CheckedListBox CBx)
{
int T;
T = LBx.Items.Count;
CBx.Items.Clear();
for (int i = 0; i < T; i++)
{
CBx.Items.Add(LBx.Items[i]);
}
}
private void ListToListCopy(ref ListBox LBx, ref ListBox CBx)
{
int T;
T = LBx.Items.Count;
CBx.Items.Clear();
for (int i = 0; i < T; i++)
{
CBx.Items.Add(LBx.Items[i]);
}
}
I'm sure there must be a way to use just one method for all three! (I can do it in Visual Basic, but can't seem to find out how in C#.)
Thursday, November 10, 2016 10:26 AM
Answers
-
The Items property of a ListBox, ComboBox and CheckedListBox are all of type ObjectCollection. Better still, this implements IList.
So you could just have a single method that takes an IList, and call it by passing in the Combobox.Items property or ListBox.Items property etc.
private void ListToListCopy(ListBox LBx, IList list) { int T; T = list.Count; list.Clear(); for (int i = 0; i < T; i++) { list.Add(LBx.Items[i]); } } // ... elsewhere in your code ... ListToListCopy(SourceListBox, DestListBox.Items); ListToListCopy(SourceListBox, DestComboBox.Items); // ... etc
P.S. Note I do not pass the arguments by ref. This is unnecessary since you are not changing the actual controls passed in (only the items contained in their Items property).
- Proposed as answer by Thorsten Gudera Friday, November 11, 2016 12:59 AM
- Marked as answer by Swildons Saturday, November 12, 2016 3:05 PM
Thursday, November 10, 2016 11:33 AM
All replies
-
Hi,
method overloading is a common way and is the way to go, if you dont want to check for the types inside your method. So either use it your way, or pass a base-object like System.Windows.Forms.Control as parameter and check inside the method for the type.
private void Form1_Load(object sender, EventArgs e) { GetSomeMsg(this); } private void GetSomeMsg(Control ctrl) { if (ctrl is Form) MessageBox.Show("yes"); else if (ctrl is ComboBox) MessageBox.Show("no"); }
Regards,
Thorsten
- Edited by Thorsten Gudera Thursday, November 10, 2016 10:39 AM
Thursday, November 10, 2016 10:35 AM -
The Items property of a ListBox, ComboBox and CheckedListBox are all of type ObjectCollection. Better still, this implements IList.
So you could just have a single method that takes an IList, and call it by passing in the Combobox.Items property or ListBox.Items property etc.
private void ListToListCopy(ListBox LBx, IList list) { int T; T = list.Count; list.Clear(); for (int i = 0; i < T; i++) { list.Add(LBx.Items[i]); } } // ... elsewhere in your code ... ListToListCopy(SourceListBox, DestListBox.Items); ListToListCopy(SourceListBox, DestComboBox.Items); // ... etc
P.S. Note I do not pass the arguments by ref. This is unnecessary since you are not changing the actual controls passed in (only the items contained in their Items property).
- Proposed as answer by Thorsten Gudera Friday, November 11, 2016 12:59 AM
- Marked as answer by Swildons Saturday, November 12, 2016 3:05 PM
Thursday, November 10, 2016 11:33 AM -
The Items property of a ListBox, ComboBox and CheckedListBox are all of type ObjectCollection. Better still, this implements IList.
So you could just have a single method that takes an IList, and call it by passing in the Combobox.Items property or ListBox.Items property etc.
private void ListToListCopy(ListBox LBx, IList list) { int T; T = list.Count; list.Clear(); for (int i = 0; i < T; i++) { list.Add(LBx.Items[i]); } } // ... elsewhere in your code ... ListToListCopy(SourceListBox, DestListBox.Items); ListToListCopy(SourceListBox, DestComboBox.Items); // ... etc
P.S. Note I do not pass the arguments by ref. This is unnecessary since you are not changing the actual controls passed in (only the items contained in their Items property).
Hi,
this is a very good way to solve the problem! Thanks.
Regards,
Thorsten
Friday, November 11, 2016 12:59 AM -
Many Thanks!
Worked as soon as I realized that I needed a
"using System.Collections;"
statement as well.
Max Symons
Friday, November 11, 2016 10:59 AM