c# - How do I open a form in a method if passed its name as a parameter -


i trying create standard method open form based on parameter passed it. basically, done:

using (quotes newqte = new quotes()) {     newqte.showdialog(); } 

by replacing:

quotes passed parameter, e.g. formtoopen. 

is @ possible?

it possible using "factory method" so.

you define formtoopen (i'm renaming createform() clarity):

func<form> createform; 

so code this:

private void makeanddisplayform(func<form> createform) {     using (var form = createform())     {         form.showdialog();     } } 

you call this:

makeanddisplayform(() => new myform()); 

where myform type of form want makeanddisplayform() create.

it's common kind of thing; pass creator function constructor of class. class uses creator function later on create things can use, without knowing how created.

this form of depencency injection.

(disclaimer: error checking elided brevity)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -