473,802 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

migrating to List<T> from ArrayList

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:Array List{}

public class Persons:BaseIte ms{}

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>:Li st<Twhere T:BaseItem
{}

public class Persons:BaseIte ms<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:Array List - 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:BaseIte ms - 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:Person s
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.Filter State = 'CA';
patients.Retrie ve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterS tate = 'CA';
persons.Retriev e();
Jun 27 '08 #1
6 1300
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...@gma il.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:Array List{}

public class Persons:BaseIte ms{}

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>:Li st<Twhere T:BaseItem
{}

public class Persons:BaseIte ms<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:Array List - 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:BaseIte ms - 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:Person s
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.Filter State = 'CA';
patients.Retrie ve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterS tate = 'CA';
persons.Retriev e();
Jun 27 '08 #2
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<Twher e T : Person {}

public class Patients : Persons<Patient {}

Jeremy Shovan
"David C" <pr********@gma il.comwrote in message
news:0a******** *************** ***********@c19 g2000prf.google groups.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:Array List{}

public class Persons:BaseIte ms{}

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>:Li st<Twhere T:BaseItem
{}

public class Persons:BaseIte ms<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:Array List - 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:BaseIte ms - 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:Person s
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.Filter State = 'CA';
patients.Retrie ve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterS tate = 'CA';
persons.Retriev e();
Jun 27 '08 #3
Off the top ....

public class BaseItems<T>:Li st<Twhere T:BaseItem
{}

public class BasePersons<T>: BaseItems<Twher e T:Person
{}

public class Persons:BasePer sons<Person>
{}

public class Patients:BasePe rsons<Patient>
{}

"David C" <pr********@gma il.comwrote in message
news:0a******** *************** ***********@c19 g2000prf.google groups.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:Array List{}

public class Persons:BaseIte ms{}

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>:Li st<Twhere T:BaseItem
{}

public class Persons:BaseIte ms<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:Array List - 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:BaseIte ms - 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:Person s
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.Filter State = 'CA';
patients.Retrie ve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterS tate = 'CA';
persons.Retriev e();

Jun 27 '08 #4
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<Twher e T : Person {}

public class Patients : Persons<Patient {}

Jeremy Shovan
Jun 27 '08 #5
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<Personper sonList = new List<Person>();
// do somethign with person list here.

IList<Patientpa tientList = 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 SpecialOpperati on()
{
// do stuff here.
}
}

public class Test
{
public static void DoSomething()
{
ItemList<Person personList = new ItemList<Person >();
ItemList<Patien tpatientList = new ItemList<Patien t>();

}
}

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 SpecialOpperati on()
{
// do stuff here.
}
}

public class PersonListProce ssor
{
public static void DoSomething(ILi st<Personperson List)
{
// ...
}
}

public class PatientListProc essor
{
public static void DoSomething(ILi st<Patientperso nList)
{
// ...
}
}

public class Test
{
public static void DoSomething()
{
ItemList<Person personList = new ItemList<Person >();
PersonListProce ssor.DoSomethin g(personList);

ItemList<Patien tpatientList = new ItemList<Patien t>();
PatientListProc essor.DoSomethi ng(patientList) ;
}
}

Jeremy Shovan
"David C" <pr********@gma il.comwrote in message
news:aa******** *************** ***********@i36 g2000prf.google groups.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<Twher e T : Person {}

public class Patients : Persons<Patient {}

Jeremy Shovan
Jun 27 '08 #6
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<Personper sonList = new List<Person>();
// do somethign with person list here.

IList<Patientpa tientList = 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 SpecialOpperati on()
{
// do stuff here.
}
}

public class Test
{
public static void DoSomething()
{
ItemList<Person personList = new ItemList<Person >();
ItemList<Patien tpatientList = new ItemList<Patien t>();

}
}

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 SpecialOpperati on()
{
// do stuff here.
}
}

public class PersonListProce ssor
{
public static void DoSomething(ILi st<Personperson List)
{
// ...
}
}

public class PatientListProc essor
{
public static void DoSomething(ILi st<Patientperso nList)
{
// ...
}
}

public class Test
{
public static void DoSomething()
{
ItemList<Person personList = new ItemList<Person >();
PersonListProce ssor.DoSomethin g(personList);

ItemList<Patien tpatientList = new ItemList<Patien t>();
PatientListProc essor.DoSomethi ng(patientList) ;
}
}

Jeremy Shovan
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
53018
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
6
13921
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an instance for the copy constructor). For example: C++ vector<classvClass;
7
57557
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
44
39195
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
2
1899
by: csharpula csharp | last post by:
Hello, I would like to know what is better for data binding and serialization purposes ArrayList or List<? Thank you! *** Sent via Developersdex http://www.developersdex.com ***
4
8933
by: =?Utf-8?B?SkI=?= | last post by:
Hello List<Tis said to be more powerful than ArrayLists but if you have something like this: List<intmylst = new List<>; myList.Add("Joe"); myList.Add(25); the list doesn't seem to accept the name "Joe".
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.