I am building a small application that allows users to view different types of weeds, flowers and vegetables, I have 3 class Weeds, Flowers and Vegetables.
I would like to clear the list_collection Weed Collection and replace with the Vegetables collection once the GetVegetablesCollection method has been called.
Weeds[] cWeeds = new Weeds[3];
Vegatables[] cVegatables = new Vegatables[2];
private void GetWeedCollection()
{
cWeeds[0] = new Weeds("Chicory", 100, 47);
cWeeds[1] = new Weeds("Chinese Cabbage", 105, 37);
cWeeds[2] = new Weeds("Cress", 81, 76);
foreach (Weeds x in cWeeds)
{list_collection.Items.Add(x.Name.ToString());}
}
private void GetVegatablesCollection()
{
cVegatables[0] = new Vegatables("Cucumber", 5, 3);
cVegatables[1] = new Vegatables("Carrot", 10, 15);
foreach (Vegatables x in cVegatables)
{ list_collection.Items.Add(x.Name.ToString()); }
The problem I get is when I call the method, I receive an 'Object reference not set to an instance of an object' on the SelectedItem property.
private void list_collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (list_collection.SelectedItem.ToString() == cWeeds[0].Name)
{
txt_name.Text = cWeeds[0].Name.ToString();
txt_cal.Text = cWeeds[0].Calcium.ToString() + "mg";
txt_phos.Text = cWeeds[0].Phosphrous.ToString() + "mg";
}
if (list_collection.SelectedItem.ToString() == cVegatables[0].Name)
{
txt_name.Text = cVegatables[0].Name.ToString();
txt_cal.Text = cVegatables[0].Calcium.ToString() + "mg";
txt_phos.Text = cVegatables[0].Phosphrous.ToString() + "mg";
}
Here is the event that calls the method;
private void btn_VegatablesChoice_Click(object sender, RoutedEventArgs e)
{ list_collection.Items.Clear(); GetVegatablesCollection(); }
Hope this made sense, thank you.