Link to home
Start Free TrialLog in
Avatar of CDFA_Inc
CDFA_Inc

asked on

Working with System.Type and new

Suppose I have these classes:

class MyBaseClass { ... }
class DerivedClass1 : MyBaseClass { ... };
class DerivedClass2 : MyBaseClass { ... };

And I have a function like this:

ArrayList GetAllInDatabaseLike(MyBaseClass template);

that you can pass an instance of one of the derived classes, and it will fill the array list filled with new objects of DerivedClass1 or DerivedClass2.

I.e., if you say

GetAllInDatabaseLike(new DerivedClass1); it will return an array list full of new DerivedClass1 objects filled with data from the database,

and if you say

GetAllInDatabaseLike(new DerivedClass2); it will return an array list full of new DerivedClass2 objects filled with data from the database.

How do I ensure that it is able to create objects of the correct derived (not base!) class?

Can I say something like:

ArrayList[2] = new typeof(template);

Or do I have to do something via System.Type?
ASKER CERTIFIED SOLUTION
Avatar of NipNFriar_Tuck
NipNFriar_Tuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial