Partial Classes in ASP.NET

Partial class is a new functionality that is included in Visual Studio .Net 2005 and is supported in ASP.Net 2.0. This new functionality helps you to split a single class into multiple partial classes. These partial classes can be in different individual files.

In the earlier versions of Visual Studio .Net 2005, while you create an ASP.Net application, you might have seen that a single class has to be in a single file. You will be beginning a class and ending that class in the same file. It was not possible to split a single class across multiple files. This new feature, partial class, allows you to allot different developers to develop the code for different functionalities that are available in a single class. These functionalities can be developed in partial classes and then compiled to form the required assembly.

In the previous versions of the Visual Studio .Net IDE, when you create a new ASP.Net webform, the name of the web form is used as a class in the code-behind file. Apart from that, you would have seen lots of code generated by Visual Studio .Net itself. In the latest version of the Visual Studio .Net IDE, the codes that are generated by Visual Studio .Net are in a separate file as a partial class. Hence a user who creates a new webform would see a partial class for that page, when the user uses the code-behind file. This way the code that is seen in the code-behind file is minimal for a particular webform.

The compilers for VB.Net or C# look for the partial classes and integrate them while compiling, to form the intermediate language. This intermediate language is the same when compared to the intermediate language that is generated, if all the partial classes are combined to form a single class in a single file. There is no modification done in the CLR for the implementation of partial classes.

Let us look at a sample code for the implementation of the partial classes. The code would have an interface which is implemented across different classes. When compiled, the compiler would integrate all the partial classes and see whether all the methods of the interface have been implemented. If it is not, then the compiler will throw an error. If all the interfaces are implemented then it will compile to form the intermediate language. The code given below gives the implementation of the partial classes.

Imports System
Interface IPartialClass
Sub PrintEmployeeName(ByVal sName As String)
Sub PrintEmployeeName()
End Interface

Partial Class MyPartialClass
Private sName As String = "sName Variable - John Peter - From Partial Class."
End Class

Imports System
Partial Public Class MyPartialClass
Implements IPartialClass

Public Sub PrintEmployeeName(string str)
End Sub
End Class

Imports System
Partial Public Class MyPartialClass
Public Sub PrintEmployeeName()
End Sub
End Class

'-----Write the following code in the click event of a button control '-----in the .aspx page
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim PClass as New MyPartialClass()
PClass.PrintEmployeeName("String from a Partial Class.")
End Sub

There are three class files namely, File_1.vb, File_2.vb, and File_3.vb. The first file has the interface IPartialClass declared and a partial class MyPartialClass which declares a string variable. The second file has the same partial class which implements the interface IPartialClass. This interface has two methods to be implemented. The first method is implemented in the second file and the second method is implemented in the third file. Altogether, all the methods are implemented in some partial class. It is not necessary for the class in the first file File_1.vb to have the Partial keyword. But if you are writing your code for partial classes in C#, it is necessary for all the classes to have the ‘partial’ keyword. This is a significant difference between VB.Net and C# in using partial classes. Note that the method PrintEmployeeName() is implemented in the third file File_3.vb and the method PrintEmployeeName(string str) is implemented in the second file File_2.vb. The string variable sName that is used in the third file is declared in the first file File_1.vb.

Create a new webform in your project and add a button control. In the click event of the button control, write the code which calls both the methods of the interface as given in the above code listing. Upon compilation, the .aspx page will execute properly displaying the appropriate strings. This proves that although the classes are split in different files, the usage of the Partial keyword helps in binding all the partial classes together during compilation and produces a single intermediate language file. The CLR finds no difference in the intermediate language, even if the IL is produced by having a single class in a single class file, where all the methods of the interface are implemented in a single class in a single class file. Since we are dealing with different files for a single class, even if you missed out implementing one method of an interface, the intellisense of Visual Studio .Net 2005 will point it out to you, thus enabling you to implement the missed out method of the interface. This is one among the advantage of using a Visual Studio .Net 2005 IDE for ASP.Net 2.0 applications.

This type of splitting the class file is particularly useful if a single class runs to thousands of lines of code with different functionalities across different methods. Productivity of the project team is increased since a single class file is split across the team members and implemented as partial classes.

| How do you implement Observer Design Pattern in .NET? | Introduction to C# (C Sharp)|Debugging in ASP.NET| How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |

“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.