I have a method to get what items are not checked in a checkbox list like so:
public static string[] GetNotSelectedCheckboxes(CheckBoxList list)
{
ArrayList values = new ArrayList();
for (int counter = 0; counter < list.Items.Count; counter++)
{
if (!list.Items[counter].Selected)
{
values.Add(list.Items[counter].Value);
}
}
return (String[])values.ToArray(typeof(string));
}
I now have a requirement to get not selected items in a radiobutton list, I copied the same method and instead of having a CheckBoxList as parameter I have a RadioButtonList.
public static string[] GetNotSelectedRadioButtons(RadioButtonList list)
{
ArrayList values = new ArrayList();
for (int counter = 0; counter < list.Items.Count; counter++)
{
if (!list.Items[counter].Selected)
{
values.Add(list.Items[counter].Value);
}
}
return (String[])values.ToArray(typeof(string));
}
Now the code does exactly the same thing and I read a bit about generic methods, is this where I should use one? I had a look at it but didn’t get the syntax right and I’m not sure how to use list.Items if I don’t specifically specify it as a radio or checkbox list.
Should I use a generic method here and any input on how I should go on about it?
Thanks in advance.
You could use Generics… but in this case, it seems easier just to use polymorphism using the base class for the simple data list controls
If you want to implement generics: