I’m sure I got the title terribly wrong (feel free to make it proper), but the example code would clear the confusion.
I have something to do like this:
private void a_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form1 f = new Form1(abc);
f.ShowDialog()
}
private void b_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form2 f = new Form2(abc);
f.ShowDialog()
}
private void c_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form3 f = new Form3(abc);
f.ShowDialog()
}
Now how can I write a single method to show up forms like these by passing the form class itself. Or am I spoiling the very concept of classes and objects? Something like this:
private void ShowForms(Form F)
{
if (abc = "cat")
return;
F f = new F(abc);
f.Showdialog();
}
and then
private void a_Click(object sender, EventArgs e)
{
ShowForms(Form1); // I cant pass string abc from here..
}
Thanks. I can live without it, but would be of great help if I can have one.
EDIT: I’ve slightly modified my example to make my requirement clearer, since the first answer wasnt exactly addressing my issue. Apologies.
EDIT2: My Question is not how to get my program running (that would be too trivial), but how to precisely use a third common function to show up forms by passing form as argument (as described above).
You could use generics and an
interfaceto accomplish this without vs2010 and .net 2.0.The interface would be something like
because you will need a property or setter method to set your parameter.
The
ShowFormmethod will look like:Usage would be:
Your forms will have to implement the interface:
}