By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

migrating to List<T> from ArrayList

P: n/a
In my business layer, I have Person, and Patient which derives from
Person.

//base class for all single classes
public class BaseItem{}

public class Person:BaseItem{}

public class Patient:Persons{}

Then I have a collection class for each of the above. Notice that the
pattern of inheritance follows that of the single classes. Just as
Person inherits from BaseItem, Persons inherits from BaseItems, and
just as Patient inherits from Person, Patients inherits from Persons.

public class BaseItems:ArrayList{}

public class Persons:BaseItems{}

public class Patients : Persons{}

This design pattern worked pretty well with 1.1. The collection class
Patients only had instances of Patient, but obviously only the naming
convention kept me from adding to Patients instances of other
classes. There is no type safety.

So enter List<T>. With this, I would like Persons to have instances
of only Person, and Patients only instances of Patient. This is what
I have so far.

public class BaseItems<T>:List<Twhere T:BaseItem
{}

public class Persons:BaseItems<Person>
{}

So far so good. I am stomped at Patients. I want it to inherit from
Persons (I don't want to get into why.), but in Patients, I would like
to allow only instances of Patient. Thanks in advance.

(Addendum) If you must know the justification behind my inheritance
pattern of collection classes, please read on. Someone from another
forum asked why not just just use plain List<Personand
List<Patient?

Here is my current design pattern. While not type safe, it has worked
pretty well.

BaseItems:ArrayList - This is the base class to all Collection
classes. It has all the data classes necessary to retrieve rows of
data necessary to populate collections.

Persons:BaseItems - This is a base class to Patients, Doctors, and
Nurses (I am making this up at this point, but the pattern is similar
my project). That is because the same table stores patients, doctors
and nurses. The Persons class knows all that.

Patients:Persons
Doctors:Persons
Nurses:Persons

This is what my client code looks like

Patients patients = new Patients();
//get all patients and only patients in California
patients.FilterState = 'CA';
patients.Retrieve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterState = 'CA';
persons.Retrieve();
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Try using

public class Persons<T: ItemsBase<T>
where T : Persons<T{ }

Generics and inheritance don't mix too well, but this should get you
what you want.

Andy

On May 15, 1:26 pm, David C <profnac...@gmail.comwrote:
In my business layer, I have Person, and Patient which derives from
Person.

//base class for all single classes
public class BaseItem{}

public class Person:BaseItem{}

public class Patient:Persons{}

Then I have a collection class for each of the above. Notice that the
pattern of inheritance follows that of the single classes. Just as
Person inherits from BaseItem, Persons inherits from BaseItems, and
just as Patient inherits from Person, Patients inherits from Persons.

public class BaseItems:ArrayList{}

public class Persons:BaseItems{}

public class Patients : Persons{}

This design pattern worked pretty well with 1.1. The collection class
Patients only had instances of Patient, but obviously only the naming
convention kept me from adding to Patients instances of other
classes. There is no type safety.

So enter List<T>. With this, I would like Persons to have instances
of only Person, and Patients only instances of Patient. This is what
I have so far.

public class BaseItems<T>:List<Twhere T:BaseItem
{}

public class Persons:BaseItems<Person>
{}

So far so good. I am stomped at Patients. I want it to inherit from
Persons (I don't want to get into why.), but in Patients, I would like
to allow only instances of Patient. Thanks in advance.

(Addendum) If you must know the justification behind my inheritance
pattern of collection classes, please read on. Someone from another
forum asked why not just just use plain List<Personand
List<Patient?

Here is my current design pattern. While not type safe, it has worked
pretty well.

BaseItems:ArrayList - This is the base class to all Collection
classes. It has all the data classes necessary to retrieve rows of
data necessary to populate collections.

Persons:BaseItems - This is a base class to Patients, Doctors, and
Nurses (I am making this up at this point, but the pattern is similar
my project). That is because the same table stores patients, doctors
and nurses. The Persons class knows all that.

Patients:Persons
Doctors:Persons
Nurses:Persons

This is what my client code looks like

Patients patients = new Patients();
//get all patients and only patients in California
patients.FilterState = 'CA';
patients.Retrieve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterState = 'CA';
persons.Retrieve();
Jun 27 '08 #2

P: n/a
Will this work for you?

public class BaseItem {}

public class Person : BaseItem {}

public class Patient : Person {}

public class BaseItems<T: List<Twhere T : BaseItem {}

public class Persons<T: BaseItems<Twhere T : Person {}

public class Patients : Persons<Patient{}

Jeremy Shovan
"David C" <pr********@gmail.comwrote in message
news:0a**********************************@c19g2000 prf.googlegroups.com...
In my business layer, I have Person, and Patient which derives from
Person.

//base class for all single classes
public class BaseItem{}

public class Person:BaseItem{}

public class Patient:Persons{}

Then I have a collection class for each of the above. Notice that the
pattern of inheritance follows that of the single classes. Just as
Person inherits from BaseItem, Persons inherits from BaseItems, and
just as Patient inherits from Person, Patients inherits from Persons.

public class BaseItems:ArrayList{}

public class Persons:BaseItems{}

public class Patients : Persons{}

This design pattern worked pretty well with 1.1. The collection class
Patients only had instances of Patient, but obviously only the naming
convention kept me from adding to Patients instances of other
classes. There is no type safety.

So enter List<T>. With this, I would like Persons to have instances
of only Person, and Patients only instances of Patient. This is what
I have so far.

public class BaseItems<T>:List<Twhere T:BaseItem
{}

public class Persons:BaseItems<Person>
{}

So far so good. I am stomped at Patients. I want it to inherit from
Persons (I don't want to get into why.), but in Patients, I would like
to allow only instances of Patient. Thanks in advance.

(Addendum) If you must know the justification behind my inheritance
pattern of collection classes, please read on. Someone from another
forum asked why not just just use plain List<Personand
List<Patient?

Here is my current design pattern. While not type safe, it has worked
pretty well.

BaseItems:ArrayList - This is the base class to all Collection
classes. It has all the data classes necessary to retrieve rows of
data necessary to populate collections.

Persons:BaseItems - This is a base class to Patients, Doctors, and
Nurses (I am making this up at this point, but the pattern is similar
my project). That is because the same table stores patients, doctors
and nurses. The Persons class knows all that.

Patients:Persons
Doctors:Persons
Nurses:Persons

This is what my client code looks like

Patients patients = new Patients();
//get all patients and only patients in California
patients.FilterState = 'CA';
patients.Retrieve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterState = 'CA';
persons.Retrieve();
Jun 27 '08 #3

P: n/a
Off the top ....

public class BaseItems<T>:List<Twhere T:BaseItem
{}

public class BasePersons<T>:BaseItems<Twhere T:Person
{}

public class Persons:BasePersons<Person>
{}

public class Patients:BasePersons<Patient>
{}

"David C" <pr********@gmail.comwrote in message
news:0a**********************************@c19g2000 prf.googlegroups.com...
In my business layer, I have Person, and Patient which derives from
Person.

//base class for all single classes
public class BaseItem{}

public class Person:BaseItem{}

public class Patient:Persons{}

Then I have a collection class for each of the above. Notice that the
pattern of inheritance follows that of the single classes. Just as
Person inherits from BaseItem, Persons inherits from BaseItems, and
just as Patient inherits from Person, Patients inherits from Persons.

public class BaseItems:ArrayList{}

public class Persons:BaseItems{}

public class Patients : Persons{}

This design pattern worked pretty well with 1.1. The collection class
Patients only had instances of Patient, but obviously only the naming
convention kept me from adding to Patients instances of other
classes. There is no type safety.

So enter List<T>. With this, I would like Persons to have instances
of only Person, and Patients only instances of Patient. This is what
I have so far.

public class BaseItems<T>:List<Twhere T:BaseItem
{}

public class Persons:BaseItems<Person>
{}

So far so good. I am stomped at Patients. I want it to inherit from
Persons (I don't want to get into why.), but in Patients, I would like
to allow only instances of Patient. Thanks in advance.

(Addendum) If you must know the justification behind my inheritance
pattern of collection classes, please read on. Someone from another
forum asked why not just just use plain List<Personand
List<Patient?

Here is my current design pattern. While not type safe, it has worked
pretty well.

BaseItems:ArrayList - This is the base class to all Collection
classes. It has all the data classes necessary to retrieve rows of
data necessary to populate collections.

Persons:BaseItems - This is a base class to Patients, Doctors, and
Nurses (I am making this up at this point, but the pattern is similar
my project). That is because the same table stores patients, doctors
and nurses. The Persons class knows all that.

Patients:Persons
Doctors:Persons
Nurses:Persons

This is what my client code looks like

Patients patients = new Patients();
//get all patients and only patients in California
patients.FilterState = 'CA';
patients.Retrieve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterState = 'CA';
persons.Retrieve();

Jun 27 '08 #4

P: n/a
It works, but now the problem is this.

If I want to have another class to inherit Patients, then I would have
to change Patients to

public class Patients<T: Persons<Twhere T : Patient{}

Then obviously I have to go through all the client code for Patients
and add <Patient>.

Not that I plan on going crazy with inheritance, but it seems to be a
major undertaking to derive from a class.

On May 15, 10:41 am, "Jeremy Shovan" <jer...@shovans.comwrote:
Will this work for you?

public class BaseItem {}

public class Person : BaseItem {}

public class Patient : Person {}

public class BaseItems<T: List<Twhere T : BaseItem {}

public class Persons<T: BaseItems<Twhere T : Person {}

public class Patients : Persons<Patient{}

Jeremy Shovan
Jun 27 '08 #5

P: n/a
Why is it that you need a class hierarchy for your collections anyways
unless you need custom code in your collections?

Why not just do this?

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class Test
{
public static void DoSomething()
{
IList<PersonpersonList = new List<Person>();
// do somethign with person list here.

IList<PatientpatientList = new List<Patient>();
// do somethign with patient list here.
}
}
Or if you really do need your own collection type to do some sort of
operation on your entities just do this.

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class ItemList<T: List<Twhere T : BaseItem
{
public void SpecialOpperation()
{
// do stuff here.
}
}

public class Test
{
public static void DoSomething()
{
ItemList<PersonpersonList = new ItemList<Person>();
ItemList<PatientpatientList = new ItemList<Patient>();

}
}

If you need to do some special process for each collection type you could
abstract the functionality into separate processor classes for each type
like the following. This will allow you to manipulate each collection type
differently but save you from creating an entire hierarchy of collection
types.

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class ItemList<T: List<Twhere T : BaseItem
{
public void SpecialOpperation()
{
// do stuff here.
}
}

public class PersonListProcessor
{
public static void DoSomething(IList<PersonpersonList)
{
// ...
}
}

public class PatientListProcessor
{
public static void DoSomething(IList<PatientpersonList)
{
// ...
}
}

public class Test
{
public static void DoSomething()
{
ItemList<PersonpersonList = new ItemList<Person>();
PersonListProcessor.DoSomething(personList);

ItemList<PatientpatientList = new ItemList<Patient>();
PatientListProcessor.DoSomething(patientList);
}
}

Jeremy Shovan
"David C" <pr********@gmail.comwrote in message
news:aa**********************************@i36g2000 prf.googlegroups.com...
It works, but now the problem is this.

If I want to have another class to inherit Patients, then I would have
to change Patients to

public class Patients<T: Persons<Twhere T : Patient{}

Then obviously I have to go through all the client code for Patients
and add <Patient>.

Not that I plan on going crazy with inheritance, but it seems to be a
major undertaking to derive from a class.

On May 15, 10:41 am, "Jeremy Shovan" <jer...@shovans.comwrote:
>Will this work for you?

public class BaseItem {}

public class Person : BaseItem {}

public class Patient : Person {}

public class BaseItems<T: List<Twhere T : BaseItem {}

public class Persons<T: BaseItems<Twhere T : Person {}

public class Patients : Persons<Patient{}

Jeremy Shovan
Jun 27 '08 #6

P: n/a
Thank you Jeremy. This is very helpful.
Why is it that you need a class hierarchy for your collections anyways
unless you need custom code in your collections?

Why not just do this?

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class Test
{
public static void DoSomething()
{
IList<PersonpersonList = new List<Person>();
// do somethign with person list here.

IList<PatientpatientList = new List<Patient>();
// do somethign with patient list here.
}
}

Or if you really do need your own collection type to do some sort of
operation on your entities just do this.

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class ItemList<T: List<Twhere T : BaseItem
{
public void SpecialOpperation()
{
// do stuff here.
}
}

public class Test
{
public static void DoSomething()
{
ItemList<PersonpersonList = new ItemList<Person>();
ItemList<PatientpatientList = new ItemList<Patient>();

}
}

If you need to do some special process for each collection type you could
abstract the functionality into separate processor classes for each type
like the following. This will allow you to manipulate each collection type
differently but save you from creating an entire hierarchy of collection
types.

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class ItemList<T: List<Twhere T : BaseItem
{
public void SpecialOpperation()
{
// do stuff here.
}
}

public class PersonListProcessor
{
public static void DoSomething(IList<PersonpersonList)
{
// ...
}
}

public class PatientListProcessor
{
public static void DoSomething(IList<PatientpersonList)
{
// ...
}
}

public class Test
{
public static void DoSomething()
{
ItemList<PersonpersonList = new ItemList<Person>();
PersonListProcessor.DoSomething(personList);

ItemList<PatientpatientList = new ItemList<Patient>();
PatientListProcessor.DoSomething(patientList);
}
}

Jeremy Shovan
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.