Purpose and Usage of Partial Class

What is a Partial Class (or struct or interface)? A partial class is a feature in which you split a class definition into two or more files. All the parts are joined together when the application is compiled. An simple example to demonstrate Partial class.

Purpose and Usage of Partial Class

 

What is a Partial Class (or struct or interface)?

            A partial class is a feature in which you split a class definition into two or more files. All the parts are joined together when the application is compiled.

 

Situations on which it can be used:

            When multiple programmers need to work on a same class then Partial class is an obvious choice.

 

Conditions:

  • All files should use Partial keyword
  • All files should be available at compile time
  • All files should have same scope specifies.

 

On which it cannot be used:

            It can’t be applied on delegate or enumeration.

 

An Example:

            Let us consider an example where we have 3 .cs files one for educational marks, one for sports marks and one for Cumulative marks and average of a student. A single class name Marks is split across these three .cs files respectively.

 

Create a Visual Studio Windows Application Project. Then add 3 .cs files with the following names respectively EducationMarks.cs, SportsMarks.cs, and Cumulative.cs.

 

            The form which uses these classes and displays the results are shown in the following figure:

 

 

   

  

 

The three .cs files used in the example are:

 

  1. EducationMarks.cs
  2. SportsMarks.cs
  3. Cumulative.cs

 

 

 

The Marks class is split into the three .cs files. The three .cs files are given below with explanations.

 

  1. EducationMarks.cs

 

This class has an integer variable eduMarks in it. There is one method to set value to the eduMarks called  setEducationMark and one method to get value from the variable called getEducationMark.

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace PartialSamp

{

    partial  class Marks

    {

        int eduMarks;

 

        public void setEducationMark(int em)

        {

            eduMarks = em;

        }

 

        public int getEducationMark()

        {

            return eduMarks;

        }

    }

}

 

  1. SportsMarks.cs

 

This class has an integer variable sportMarks in it. There is one method to set value to the sportMarks called setSportsMark and one method to get value from the variable called getSportsMark.

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace PartialSamp

{

  

 

    partial class Marks

    {

        int sportMarks;

 

        public void setSportsMark(int spm)

        {

            sportMarks=spm;

        }

 

        public int getSportsMark()

        {

            return sportMarks;

        }

 

 

    }

 

}

 

  1. Cumulative.cs

 

This class is the one which sums the sports and educational marks to get the total and then finds the average out of it.

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace PartialSamp

{

   partial  class Marks

    {

       int totMarks;

       float avg;

 

    

       public int getTotMarks()

       {

           totMarks = sportMarks + eduMarks;

           return totMarks;

       }

 

       public float getavgMarks()

       {

           return totMarks/2;

       }

 

 

    }

}

 

 

 

Note: You can even find the intellisense of the methods of prior .cs files in the current files. For example after typing the EducationMarks class, you will get the methods in it being populated in intellisense in the SportsMarks and Cumulative classes.

 

 

Visual Design:

 

            Create 4 text boxes and 4 labels and a button. Name them as shown in the Figure 1. Then type the following code in the button1_click,

 

private void button1_Click(object sender, EventArgs e)

        {

            Marks m = new Marks();

            m.setEducationMark(int.Parse (  txtEM.Text ) );

            m.setSportsMark(int.Parse(txtSM.Text));

           

            txtTM.Text = m.getTotMarks().ToString() ;

            txtAvg.Text = m.getavgMarks().ToString();

        }

 

 

            Similarly you can try with struct and interface.

 

By Sakshi a   Popularity  (1209 Views)