@Musigenesis, you really don't want to force client code to break when you change your dialog, and using an out parameter that is only sometimes valid isn't a good design. As @Daok says, when you have more than 1 value returned this starts to get messy and ugly fast.
You also can't force client code to use the result any more than the .net framework ensures that you call properties on a file dialog. You're also not forcing the caller to do anything with the out parameter, all you've forced them to do is to accept a variable that they may not want to use.
If the dialog is very generic this may not be applicable, but instead of chucking all sorts of properties to the dialog itself, have a single method that you use consistently throughout your application, and have that return a specific class that holds the relevant data.
public sealed class MySaveDialogResult
{
public static MySaveDialogResult NonOkResult(); // Null Object pattern
public MySaveDialogResult( string filePath ) { ... }
// encapsulate the dialog result
public DialogResult DialogResult { get; private set; }
// some property that was set in the dialog
public string FilePath { get; private set; }
// another property set in the dialog
public bool AllowOVerwrite { get; private set; }
}
and your dialog is
public MySaveDialog ...
{
public MySaveDialogResult GetDialogResult() { .... }
}
The essence is a small immutable utility class that also implements null object pattern. The null object is returned whenever the dialog result wasn't OK. Obviously the above is a shot in the dark for your needs, so alter it at will, make inheritance hierarchies, etc.
The main point is to have GetDialogResult()
, a single method on the dialog, to returned a class that encapsulates all the relevant dialog data.
edit:
@yapiskan wonders why not just 'out' the MyDialogResult
versus calling GetDialogResult()
.
IMO - The points are simply:
- That's not the convention
- A method call is trivially easy, and made easier when you follow the 'convention' argument as made above.
out
is awkward to use. GetDialogResult()
is not forcing the caller to write awkward code, and it doesn't force the user to consume the dialog result at the point of invoking the dialog.
- Normally the dialog isn't re-instantiated or re-shown to get the result, it's already there. Show() and Hide() do just that.
The reality is you're trading a method call for an awkward ShowDialog() syntax. Method calls are cheap, and you can't guarantee the caller will use your out parameter any more than you can guarantee they will call GetDialogResult(). So why bother. Make the thing easy to use, or don't overload ShowDialog in the first place.
Maybe whatever you're sub-classing is funky and acts differently and it's not applicable to your situation, but general design is forms don't go away when you click OK, they go away when they are Disposed() of.