0

is it possible to create instances of a class programmatically?

for example, I will need to create anywhere from 10-20 forms programmatically

so I will do:

   Form graphs = new Form1(dateStart.Value.ToShortDateString(), dateEnd.Value.ToShortDateString(),cbQCValues.Text,cbAnalytes.Text,cbInstruments.Text);

however the constructors are going to be different each time so I need to put this in a for loop

so I will need to run the above code how ever many times I need to. for example 10-20 times. I will need to have form classes graphs1, graphs2, graphs3...

is this possible?

6
  • 2
    You can use reflection. See Activator.CreateInstance. But if you really want to is another question. It feels like your concrete problem is the symptom of an architectural problem. msdn.microsoft.com/en-us/library/… Commented Nov 8, 2010 at 22:52
  • Are you merely trying to avoid the required typing, or is there some other more general problem you're trying to solve? Are these classes unknown at compile time? Commented Nov 8, 2010 at 22:54
  • You want to create many forms and call a different constructor each time? Use a for-loop and if-else. I don't see the problem. Commented Nov 8, 2010 at 22:56
  • @mark i need to display 10 different graphs thats all Commented Nov 8, 2010 at 22:56
  • @qwertie i didnt know that u can have multiple classes with the same name Commented Nov 8, 2010 at 22:57

4 Answers 4

4

Not sure if I understand it correctly but are you after this??

List<Form1> forms = new List<Form1>();
for(int i=0;i<20;i++)
{
    forms.Add(new Form1(, ......))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it will work if you can figure out what value to pass in each loop cycle based on the value of i.
so how would i call the form.Show() method with this?
4

I'd recommend you look into a factory design pattern. There are many options, depending on what you are trying to to achieve.

You might find this useful: http://www.dofactory.com/Patterns/Patterns.aspx

1 Comment

+1: but IMHO an "abstract factory" would be the recommended option here.
1

You can emit new classes at runtime, sure, but I suspect that you are asking the wrong question here. If you only need to add random controls to a form, just create a new Form object and add those controls. If you really need to create new classes at runtime, have a look at the System.Reflection.Emit namespace.

If you think this is a vague answer, you should consider writing a less vague question. ;)

Comments

0

Use the Activator class provided by the framework:

   public T CreateInstance<T>() where T: new() {

     return (T)Activator.CreateInstane(Assembly.GetEntryAssembly().CodeBase, 
                                     typeof(SomeType).FullName)

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.