We would have to write the below code to add a new item (with value) to any of these 5 controls (BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList)
C#:
bulletedList.Items.Add(new ListItem("text", "value"));
checkBoxList.Items.Add(new ListItem("text", "value"));
dropDownList.Items.Add(new ListItem("text", "value"));
listBox.Items.Add(new ListItem("text", "value"));
radioButtonList.Items.Add(new ListItem("text", "value"));
VB:
bulletedList.Items.Add(New ListItem("text", "value"))
checkBoxList.Items.Add(New ListItem("text", "value"))
dropDownList.Items.Add(New ListItem("text", "value"))
listBox.Items.Add(New ListItem("text", "value"))
radioButtonList.Items.Add(New ListItem("text", "value"))
I find it quite painful to write so much code just to add a new item (with value) to these controls and so I created a new AddItem extension method for the ListControl class (since all these 5 controls inherit from the ListControl class)
C#:
public static void AddItem(this ListControl lc, string text, string value)
{
lc.Items.Add(new ListItem(text, value));
}
VB:
<Extension()> _
Public Sub AddItem(ByVal lc As ListControl, ByVal text As String, ByVal value As String)
lc.Items.Add(New ListItem(text, value))
End Sub
Now I can simply write the above code in a much simpler and cleaner way like below:
C#:
bulletedList.AddItem("text", "value");
checkBoxList.AddItem("text", "value");
dropDownList.AddItem("text", "value");
listBox.AddItem("text", "value");
radioButtonList.AddItem("text", "value");
VB:
bulletedList.AddItem("text", "value")
checkBoxList.AddItem("text", "value")
dropDownList.AddItem("text", "value")
listBox.AddItem("text", "value")
radioButtonList.AddItem("text", "value")
Just 1 extension method which works for 5 controls to solve the problem :-) Cheers to object inheritance and cheers to Extension Methods ;-)
Note: You can find a list of other cool and useful Extension Methods coded by me here
Cheers,
Raj
~~~ CODING FOR ETERNITY !!! ~~~