Using Component (Create Partial Classes) in ASP.NET Part 7



Partial Classes

Partial class is a functionality that is included in Visual Studio and is supported in ASP.Net. This new functionality helps us 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, while we create an ASP.Net application, we might have seen that a single class has to be in a single file. We 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 us 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. Here is an example on Partial Class.
 

Class1.vb File Code

Imports Microsoft.VisualBasic
Partial Public Class Tweedle
    Private message1 As String = "Welcome to MINDCRACKER."

End Class

Class2.vb File Code

Imports Microsoft.VisualBasic
Partial Public Class Tweedle
    Private message2 As String = "This is my message will illustrate how pertial " _
        & 
"classes works."
End Class

Class3.vb File Code

Imports Microsoft.VisualBasic
Partial Public Class Tweedle
    Public Function GetMessage() As String
        Dim completeMsg As String
        completeMsg = message1 + message2
        
Return completeMsg
    
End Function
End Class

Default.aspx File Code

<%
@ Page Language="VB" AutoEventWireup="false" 
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>
</
head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</
body>
</
html>

Default.aspx.vb File Code

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object
ByVal e As System.EventArgsHandles Me.Load
        
Dim msg As New Tweedle()
        Label1.Text = msg.GetMessage
    
End Sub
End
 Class

Notice that the private message1 and message2 fields are defined in first two files but in third file we have added message1 and message2 and stored that result in completeMsg and the same returned as result. GetMessage() method exist in third file which is returning the completeMsg. When the GetMessage() method is called, it returns the value of the private fields from the other class. All files define a class with the same name. The class declaration includes the keyword Partial. The Partial keyword marks the classes as partial classes.

Note: Continue in Next Part.

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System