Question

If I look at some classes in the framework, using reflector, I can see that forms and user controls are made private and nested into a parent class.

For instance, I have a control which makes use of pop-up form that is specific to that control. At the moment, I make the pop-up form friend accessible. If I wanted to do it the framework way, I'd make it private and nest it into the control class. If I do this, however, I can no longer use the ide to design the form and I get errors when I try to compile. So, I have 2 questions:

(1) Do Microsoft do something at the last minute to nest all things private?

(2) Is their way the preferred way or should I stick to my friend accessors?

Was it helpful?

Solution

The nested form is better, because it enforces correct encapsulation and means the final control will end up in one nice neat package for distribution. If neither of those are a concern for you keep doing it your way. But if you want to at least try nesting the class, you can do something like this:

  1. Use the designer to build your nested form outside the class as your normally would.
  2. Add a second empty form as a private nested form as they do in the CLR examples with the same name as the form you built in step 1.
  3. Migrate the code from *.designer.vb or *.designer.cs for your first form to the constructor for your 2nd form. It'll mostly be just a big copy/paste.
  4. Remove the form from step 1. You might want preserve by moving it to a separate class library project so you can use when you need to make changes.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top