Tuesday, March 30, 2010

Visual Studio 2008 -- Grouping Partial Classes

You have have noticed in Visual Studio that items can be grouped (take a look at the designer file... it's grouped with the normal class.

The grouping feature that allows the designer to be combined with the normal class file is rooted in the partial class. Partial classes are not unique to .NET, but were introduced to the framework with the 2.0 release.

If your interested in setting up your own grouping feature you have to edit the project file directly, or use a Visual Studio plug-in (like VsCommands).

In this example we are going to to add a partial class for data access, and group it so that it appears under the class in visual studio.

1. Create the Data Access partial class file in Visual Studio


2. One of the two classes must contain the partial definition declaration, so in visual studio, select the data access file, and add "partial" before the class name


3. Next unload the project file


4. Click 'Edit .csproj

5. Locate the compile in ItemGroup for your item to be grouped, and in a DependentUpon element:
<Compile Include="Class1.DataAccess.cs">
<DependentUpon>Class1.cs</DependentUpon>
</Compile>

6. Reload the project.

That's it.

No comments: