How to create a partial class?
Hi all. I am using C# as well as VB.NET for my ASP.NET applications. I have come across the keyword partial used to split up a single class' definition into two .cs files. I hope that is correct. I do not, however, know what is the purpose of doing so? Also, I have seen this concept used in windows programming with winforms/.NET more frequently. The code behind file for the windows forms is a partial class file in C# and the default name is Form1. In C++/CLI, it is ref class and in VB.NET the inheritance clause is omitted altogether. The difference in the three environments is evident. However, I have the following questions:
<1> What is the purpose of using the keyword partial in C#?
<2> How can I use partial in web applications (using C# as a language) in a web-forms code behind file? I mean to say that, I want to split a single code behind file into 2 (or more) code behind files for a web form.
<3> Also, looking at the code, I found that VB.NET environment does not show up the details of the form designer code at all, C# exposes a few code chunks where as C++/CLI gives minute details such as ResumeLayout() and PerformLayout()...So why is it that the level of exposure of the designer generated code in the three different languages different?
Thanks and Regards,
Bhushan.
Re: How to create a partial class?
Re: How to create a partial class?
The greatest benifit of it is you can arrange your files/class in your application. You can separate your interface from implementations, and business logic from user interface, something like that. In other words you have a good and clean separation of concerns. Hope that helsp.
Re: How to create a partial class?
Partial classes would be useful, when:
1. Multiple programmers are working on same implementation class, each of the can code in separate file, separate set of methods (of same class) independently.
2. Large files can be splitted into multiple files. In C++ this can be achieved by "declaring" class in (small) header file, and one or more CPP files.
3. As Thread1 said, you can divide functionality, viz. UI, business logic, database read etc, into multiple .cs files. You can also place them into different directories.
Re: How to create a partial class?
I like these:
Quote:
1. They allow programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: If you find your class file getting too large, it may well signal a design fault and refactoring may be required.
2. The most compelling reason for using partial class is to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from your business logic (we will discuss this in a later section). This will prevent developers from messing with the code that is used for the UI. At the same time, it will prevent you from losing your changes to the designer-generated code when you change the UI.
Re: How to create a partial class?
Of my point of you, the major benefit of partial classes is to separated automatically generated code (e.g. by designer) from the code written by the developer, you can let the tool to regenerate the code without any fear of losing your own coding.
Spliting the source to parts maintained independly by several developers doesn't soud good, because it indicates bad design. Having classes so huge that many guys can work on them together is not good idea, I'd said.
Re: How to create a partial class?
Quote:
Originally Posted by boudino
Spliting the source to parts maintained independly by several developers doesn't soud good, because it indicates bad design. Having classes so huge that many guys can work on them together is not good idea, I'd said.
That's true. But functions may be slightly longer. Some method of class may be dependent on other part, a business logic glued with database logic, for example.
In CVS, locking whole file may become deadlock, multiple programmer are available but file is not available to all of them - only one can lock the file and amend it.
With partial classes and multi-file class, each programmer can lock the relevant file, and can change it.
Re: How to create a partial class?
First of all thankyou all for the replies you have posted here. I now understand the use of the keyword partial and also the implementation of the partial keyword as well. I still have a question if the code behind file for a .aspx page can be split into two .cs files? The reason is, we include the name of the code behind file in the .aspx code in the first line. However, I was just wondering if there was a way to split the code behind as well? What would I have to do, if I have to split up the code behind file? Also, looking at the devx link posted by dglienna the use of partial keyword is clear. But, I still havenot understood, why VB.NET uses Partial for web forms, while it does not use it for winforms? Also, my question to the different levels of exposure of the form designer code in VB.NET, C# and C++/CLI remains unanswered! So, if you can please let me know about that.
Thankyou,
Bhushan.
Re: How to create a partial class?
Are you mixing web programming with desktop programming? They are in different model. Just follow on what is in the ASP.Net achitecture, then all will be fine. For ASP.Net, read http://msdn.microsoft.com/en-us/library/015103yb.aspx
Re: How to create a partial class?
Partial classes have nothing to do with the Web vs. Desktop.
In fact, in 2.0 (and later), there is no more "code behind" it is now "code beside".
This reflects the difference in technology. In 1.x the as?x was parsed and compiled into a DERIVED class with the base class "behind" it in the code. In 2.0 (and later) the two are each partial implementations of one class.
I leverage the designer/manual paradigm VERY heavily. Nearly 80% of all of the classes in my designs have at least some auto generated code (I write alot of code generators ;) )
I do tend to disagreee in general with the splitting because" it is too big". This really begs for a look at the design. One common set of guidelines.
1) Properties should outnumber actions by 3-1 or more for most "noun" classes. "Verb" classes should have primarily (only) methods, and are often stateless.
2) A Maxiumum of 10-15 public/internal items per class.
3) The implementation of a method should fit on the screen, unless it is simple "fall through" code. (such as initializers).
Following these guidlines has really helped the process here.... [I only continue to do things if I am convinced that they save me $$$$ :D ]
Re: How to create a partial class?
Thanks for the heads-up TheCPUWizard! :wave:
Actually, I was just thinking that it is not possible to dessiminate parts of a class to cs files through partial because of the model until I found that there is something interesting with the CodeFileBaseClass attribute in the page directive. Oh well.. :D
Test.aspx:
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" CodeFileBaseClass="TestBase" %>
Test.aspx.cs:
Code:
public partial class Test : TestBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
App_Code/TestBase.cs:
Code:
public partial class TestBase : System.Web.UI.Page
{
public void Method1()
{
}
App_Code/TestBase1.cs:
Code:
public partial class TestBase : System.Web.UI.Page
{
public void Method2()
{
}
App_Code/TestBase2.cs:
Code:
public partial class TestBase : System.Web.UI.Page
{
public void Method3()
{
}
etc..
Re: How to create a partial class?
Thank you for all your replies. I might be new to Thread1, but 'THECPUWINZARD' has been answering my queries eversince....
As Thread1 asked if I was mixing web programming and windows programming, I would say 'No' but they can be mixed as in the complex applications that are either web or windows through various interfacing techniques. Albeit, a web application remains as a web application and a windows appllication remains a windows application. I was just asking about the difference in the way of representing the classes for the UIs in web and windows applications for VB.NET where as for C#, the keyword partial is used in both the cases? Also, I asked for the reason as to why VB.NET and C# (to an extent a little lesser than VB.NET) hides the form designer code that is visible in VC++.NET? But again, even if it be a bad practise to split up the code behind class into 2 .cs (or more) files I would like to ask TheCPUWizard, whether that is possible? I am personally not going to do it, but I am just asking this out of curiousity...
Thankyou,
Bhushan.
Re: How to create a partial class?
You can see the designer code just fine for VB.Net and C#.
It is in the filename.designer.cs (or .vb) file. There should be a little "+" sign next to the file name.. Click it....
Re: How to create a partial class?
No. Infact, the Q I put on the forum, was only after checking that my code did not contain any such section called:
Code:
Region "Form Designer Code"
-
-
-
End Region
in VB.NET. I see only two files Solution Explorer for VB.NET and those are MyProject and Form1.vb.
In C# I see other files apart from Form1 and the project file such as AssemblyInfo.cs, Program.cs, resource file, etc. Yet I do not find any region showing the windows form generated code anywhere.
Come to Visual C++.Net, it shows the entire code for the form designer! Also you can see the body of InitializeComponent() function and in that every control and associated variables are intialized and this function is then called by the Form constructor.
The other day I tried to go in project properties as well as solution properties, to see if there is any setting that hides that hides this designer code in VB.NET by default. But I could not find any. But, I am sure that there imust be an option somewhere which is turned off by default. So if anyone knows about it please let me know. By the way, I generated the same application for all the three environments, that is the part of MSDN tutorials, to illustrate Windows Forms application in the MSDNs "How Do I?" section. The sample contains a menu bar control demo, a web browser control, a combo box and a command button. All the three samples run fine.
Thankyou,
Bhushan.
Re: How to create a partial class?
Quote:
Originally Posted by bhushan1980
In C# I see other files apart from Form1 and the project file such as AssemblyInfo.cs, Program.cs, resource file, etc. Yet I do not find any region showing the windows form generated code anywhere.
Are you saying that if you create a new C# Windows forms project, you can't find the Form1.Designer.cs file?
And within this file, there isn't a #region called "Windows Form Designer generated code"?