Wednesday, September 12, 2007
Dispose() and Partial Classes
C#.NET 2.0 introduced the concept of partial classes. Basically, this feature allows the fields and methods of a single class to be distributed across multiple files. Although it is not necessarily good design to have a class that is so big that it needs to be distributed across multiple files, it does come in handy -- especially for forms.
If you create new form
But, wait! The
If you need to add code to the
The answer is simple. It is safe to change the
For you own sanity, however, you may even choose to move
If you create new form
MyForm in Visual Studio 2005, then VS2005 creates a partial class with two C# source files: MyForm.cs and MyForm.Designer.cs. From the file names, Visual Studio obviously intends for the designer-generated code to be placed in the second file. One may even argue that the main reason for allowing partial classes is to better isolate the designer-generated code from the programmer's code.But, wait! The
Dispose() method is in the MyForm.Designer.cs file, and if you try to define a Dispose() method in MyForm.cs the compiler will complain and say you have duplicate method definitions. So, the big question becomes...If you need to add code to the
Dispose() method, is it safe to modify the Dispose() method in the designer-generated file? If you do modify Dispose() in MyForm.Designer.cs, will the designer overwrite your changes at some point in the future?The answer is simple. It is safe to change the
Dipose() method in MyForm.Designer.cs. If you look closely at the MyForm.Designer.cs file you will notice that the Dispose() method is outside of "the region", and that the Dispose() method does not actually ever have to be modified by the designer again.For you own sanity, however, you may even choose to move
Dispose() into MyForm.cs instead of MyForm.Designer.cs.Subscribe to Posts [Atom]