Override ShowDialog()

J

Jonathan Wood

I want to create a Windows form that requires an argument in order to
display the form.

I tried overriding ShowDialog() to give it an argument (I had to figure out
the method syntax as I couldn't find anyway to create an override
automatically). This appears to work but it doesn't replace the version that
takes no argument.

Is there an easy way to require a form always is passed an argument?

Thanks.

Jonathan
 
A

Alberto Poblacion

Jonathan Wood said:
I want to create a Windows form that requires an argument in order to
display the form.

I tried overriding ShowDialog() to give it an argument (I had to figure
out the method syntax as I couldn't find anyway to create an override
automatically). This appears to work but it doesn't replace the version
that takes no argument.

An override of a method always has the same arguments as the overridden
method. If you change the arguments, it is not an override, but an overload.
The overload will not prevent the original method (with different arguments)
from working. Also, note that the compiler will complain if you try to use
the keyword "overload" on a method that is not matched to a corresponding
virtual method with the same signature in the base class.
Is there an easy way to require a form always is passed an argument?

Pass the argument in the constructor. If you create a constructor with an
argument (or rather, you modify the original default constructor to add an
argument), the class will not have a default constructor, so it will not be
possible to do a "new myForm()" without arguments. In that way, every time
you call ShowDialog, the form will already have the argument:

myForm frm = new myForm(argument);
frm.ShowDialog();
 
J

Jonathan Wood

An override of a method always has the same arguments as the overridden
method. If you change the arguments, it is not an override, but an
overload. The overload will not prevent the original method (with
different arguments) from working. Also, note that the compiler will
complain if you try to use the keyword "overload" on a method that is not
matched to a corresponding virtual method with the same signature in the
base class.

Yeah, okay. I guess I was still thinking in terms of C++ there.
Pass the argument in the constructor. If you create a constructor with
an argument (or rather, you modify the original default constructor to add
an argument), the class will not have a default constructor, so it will
not be possible to do a "new myForm()" without arguments. In that way,
every time you call ShowDialog, the form will already have the argument:

myForm frm = new myForm(argument);
frm.ShowDialog();

Okay. I'm not sure if that would've been my preference but it should
definitely work.

Thanks!

Jonathan
 
P

Pavel Minaev

Okay. I'm not sure if that would've been my preference but it should
definitely work.

Actually, an even better way is to provide a property on your class.
That's the pattern that all the common dialog classes (FileDialog etc)
follow, and it's better to stick to the existing conventions unless
you have a good reason not to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top