How do I use for example OnRemove in the CollectionBase

T

Tony Johansson

Hi!

Here I have a simple program that is deriving from CollectionBase.
I just wonder if you can give me some guidlines how to use the methods that
start with On for example
OnRemove. These methods that start with On exist in the CollectionBase ??
I mean if I shall remove an item I can just call DischargePatient which in
my case perform an
List.Remove for the passed argument.

using System;
using System.Collections;

public class VeterinaryClinic
{
public static void Main()
{
Cat fluffy = new Cat();
Whale shamu = new Whale();
Fish goldy = new Fish();
Patients SickPets = new Patients();
SickPets.AdmitPatient(fluffy);
SickPets.AdmitPatient(shamu);
SickPets.AdmitPatient(goldy);

SickPets.DischargePatient(fluffy);
foreach (object pet in SickPets)
Console.WriteLine(pet);
}
}
public class Patients : CollectionBase
{
public void AdmitPatient(object patient)
{
if ((patient) is Whale)
Console.WriteLine("Can not add a Whale as a patient. Who do you
think we are Sea World!!");
else if ((patient) is Mammal | (patient) is Fish | (patient) is
Reptile)
this.List.Add(patient);
}

public void DischargePatient(object patient)
{
this.List.Remove(patient);
}

protected override void OnRemove(int index, object value)
{
//base.OnRemove(index, value);
}
}

public class Mammal { }
public class Dog : Mammal { }
public class Cat : Mammal { }
public class Whale : Mammal { }
public class Fish { }
public class Reptile { }

//Tony
 
F

Family Tree Mike

Hi!

Here I have a simple program that is deriving from CollectionBase.
I just wonder if you can give me some guidlines how to use the methods that
start with On for example
OnRemove. These methods that start with On exist in the CollectionBase ??
I mean if I shall remove an item I can just call DischargePatient which in
my case perform an
List.Remove for the passed argument.

using System;
using System.Collections;

public class VeterinaryClinic
{
public static void Main()
{
Cat fluffy = new Cat();
Whale shamu = new Whale();
Fish goldy = new Fish();
Patients SickPets = new Patients();
SickPets.AdmitPatient(fluffy);
SickPets.AdmitPatient(shamu);
SickPets.AdmitPatient(goldy);

SickPets.DischargePatient(fluffy);
foreach (object pet in SickPets)
Console.WriteLine(pet);
}
}
public class Patients : CollectionBase
{
public void AdmitPatient(object patient)
{
if ((patient) is Whale)
Console.WriteLine("Can not add a Whale as a patient. Who do you
think we are Sea World!!");
else if ((patient) is Mammal | (patient) is Fish | (patient) is
Reptile)
this.List.Add(patient);
}

public void DischargePatient(object patient)
{
this.List.Remove(patient);
}

protected override void OnRemove(int index, object value)
{
//base.OnRemove(index, value);
}
}

public class Mammal { }
public class Dog : Mammal { }
public class Cat : Mammal { }
public class Whale : Mammal { }
public class Fish { }
public class Reptile { }

//Tony

You can change the OnRemove code you have to something like this:


protected override void OnRemove(int index, object value)
{
base.OnRemove(index, value);
Console.WriteLine("Reminder: Send bill to " + value.ToString());
}

You will see it is called when you call DischargePatient.

You may want to specially process the object removed from a list. If it
was a shopping cart list, you may reinventory the item, as an example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top