tags:

views:

90

answers:

1

Say I wanted to customize an OpenFileDialog and change, for example, the way the filter for file extensions work, as in the case of this question. After I pointed out to the author of said question that the OpenFileDialog is not inheritable, I got a comment with the following:

Even though the OpenFileDialog is sealed (not inheritable), you may use it as a nested type. For instance, using a property that will get the NativeDialog. Then, you write your method always using the NativeDialog property and you're done.

My question is, can someone provide me with an example code on how would I proceed on doing something like that? I'm kind of new to the concept of nested types so I'm having a hard time figuring that out by myself, and I searched around the web and couldn't find anything too concrete about it.

Thanks!

+1  A: 

Nested type is just another way of saying wrapper class (I am assuming). So you would create a new class that has a private member class of OpenFileDialog. Then you create all of the public members that you need.

So for OpenFileDialog you would create a class like this:

public class CustDialog
{
   private OpenFileDialog _dialog;

   public CustDialog()
   {
       //instantiate custom OpenFileDialog here
   }

   public DialogResult ShowDialog()
   {
       return _dialog.ShowDialog();
   }
}

You can even take this one step further and have the wrapper class inherit from the CommonDialog class. This would allow you to use your wrapper class exactly like a standard dialog.

Jason Webb
Thanks for the help, but I don't think it's the same thing, according to this msdn guide: http://msdn.microsoft.com/en-us/library/ms173120(v=VS.80).aspxYour answer would help me solve the problem, but I still wonder if I could do it using nested types...
vitorbal
A nested type still won't allow you to inherit from a sealed class. I am not sure what the person was meaning when they told you about that, but a class wrapper will do what you need.
Jason Webb
I agree, thanks for the help!
vitorbal