Partial Classes

Partial Classes

Old forum URL: forums.lhotka.net/forums/t/1418.aspx


smark posted on Saturday, October 07, 2006

A simple question: When you use partial classes with CodeSmith CSLA 2.1 templates, how do you add a separate partial class file with the same name in VS 2005? For example, I use CodeSmith to generate a EditableRoot partial class "User" and then add another partial class "User". VS 2005 would not let me add another class named "User" in the class library, saying "a class named 'User' already exists." I have gotten around this by adding the custom partial class below the generated partial class in the same codefile. Is it possible to have a separate code files for partial classes with the same name? How would you do that? Thanks.

Skafa replied on Saturday, October 07, 2006

just name the files differently, like .Generated.cs and .User.cs (or place them in seperate folders) and use this syntax:

public partial class MyClass { }

note the use of the keyword 'partial'

David replied on Sunday, October 08, 2006

I discovered (by chance) that if you add .Designer to the name, visual studio associates the two classes and hides the generated class in the same way it does with the generated code created for a windows form.

So if you have a Person class, add the user file first and call it Person.vb. Then add the generated class as Person.Designer.vb.

Obviously you will need to click the Show All Files button if you need to see the generated class.

smark replied on Tuesday, October 10, 2006

David, that's great information, thank you very much! I will try that out. It just seemed so un-intuitive to have two separate code files with names such as "PermissionCategoryGenerated.cs" and "PermissionCategoryCustom.cs" when all they contained were the public partial class "PermissionCategory".

smark replied on Tuesday, October 10, 2006

Okay, it works. I first created a partial class "PermissionCategory.cs" and then generated the partial class "PermissionCategory.cs" and saved it as PermissionCategory.Designer.cs. Thanks.

Copyright (c) Marimer LLC