Hi,
The allForms list is empty, so ToArray will return an empty array. Also,
where and how is allFormInstance defined?
If allForms were to be populated with the Types first, then your loop would
work to iterate over the array correctly. If allFormInstance is something
like, List<Form>, then your code should successfully add instances of each
Form Type in the allForms list to the allFormInstance list as long as each
Form is constructed successfully.
The code could be more succinct:
List<TypeformTypes = new List<Type>();
formTypes.Add(typeof(MyForm));
formTypes.Add(typeof(MyForm2));
// formTypes contains 2 Types: MyForm and MyForm2
List<Formforms = new List<Form>();
foreach (Type type in formTypes)
{
forms.Add(Activator.CreateInstance(type));
}
// forms contains two instances: one of MyForm and one of MyForm2
Just in case, note that the code above isn't useful at all since I
hard-coded typeof(). It could easily have replaced it with a call to the
constructor of each of the two forms:
List<Formforms = new List<Form>();
forms.Add(new MyForm());
forms.Add(new MyForm2());
If you don't know the Types of Forms at design-time then you'll have to use
reflection, as in the previous example.
--
Dave Sexton
http://davesexton.com/blog http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)
"rGenius" <ru*********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
thanks Dave for a speedy reply,
its what i want, the only thing is how to populate the list with the
list of forms.
i've been trying these to get it done.
List<TypeallForms = new List<Type>();
foreach (Type type in allForms.ToArray())
{
Form curForm =
Assembly.GetEntryAssembly().CreateInstance(type.Fu llName) as Form;
allFormInstance.Add(curForm);
}
thanks,