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(MyBas eClass 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?
class MyBaseClass { ... }
class DerivedClass1 : MyBaseClass { ... };
class DerivedClass2 : MyBaseClass { ... };
And I have a function like this:
ArrayList GetAllInDatabaseLike(MyBas
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.