Parameterized Constructor


  • A Parameterized Constructor accepts arguments to store the values into the Data Fields
  • Using Parameterized Constructor we can store different set of values into different objects created to the class

Example 

Class Diagram :-





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAConstructors
{
    class ClsEmployee3
    {
        int EmpId, EAge;
        string EName, EAddress;
        public ClsEmployee3(int Id, string Name, string Address, int Age)
        {
            this.EmpId = Id;
            this.EName = Name;
            this.EAddress = Address;
            this.EAge = Age;
        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Details:- ");
            Console.WriteLine("Employee id is       :-  " + this.EmpId);
            Console.WriteLine("Employee Name is     :-  " + this.EName);
            Console.WriteLine("Employee Address is  :-  " + this.EAddress);
            Console.WriteLine("Employee Age is      :-  " + this.EAge);
        }
    }
    class ClsPConstructor
    {
        static void Main()
        {
            ClsEmployee3 Obj1 = new ClsEmployee3(9, "Sampath kumar", "Karimnagr", 25);
            ClsEmployee3 Obj2 = new ClsEmployee3(99, "Samrat", "Hyderabad", 25);
            Obj1.DisplayEmpData();
            Obj2.DisplayEmpData();
            Console.Read();
        }

    }
}



Output:-

  • In the above example, if we create an object like,
     ClsEmployee3 obj3=new Employee3( );

  • It raises a compilation error like “CAConstructor. Clsemployee3doesn’t contain a constructor that takes 0 arguments
  • To avoid this, we should write a constructor with zero arg. In our class like. . . . .
        public ClsEmployee3( )
         {
             this.EmpId=100;
             this.EName=”Srinivas”;
             this.EAddress=”Hyderabad”;
             this.EAge=27;
        }


  • We can’t also create an object like by passing two arguments like,
      ClsEmployee3 obj4=new ClsEmployee3(100, ”Srinivas”);

  • It raises a compilation error like “CAConstructor.Clsemployee3 doesn’t contain a constructor that takes 2 arguments”
  • To avoid this, we should write a constructor with two arguments in  our class like,
      public ClsEmployee3(int Id, string S1)
      {
          this.EmpId=Id;
          this.EName=Name;
      }

After writing these two constructors in the class, the class code will be

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAConstructors
{
    class ClsEmployee3
    {
        int EmpId, EAge;
        string EName, EAddress;
        public ClsEmployee3( )
        {
                  this.EmpId=100;
                  this.EName="Srinivas";
                  this.EAddress="Hyderabad";
                  this.EAge=27;
        }
        public ClsEmployee3(int Id, string Name)
        {
            this.EmpId = Id;
            this.EName = Name;
        }

        public ClsEmployee3(int Id, string Name, string Address, int Age)
        {
            this.EmpId = Id;
            this.EName = Name;
            this.EAddress = Address;
            this.EAge = Age;
        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Details:- ");
            Console.WriteLine("Employee id is       :-  " + this.EmpId);
            Console.WriteLine("Employee Name is     :-  " + this.EName);
            Console.WriteLine("Employee Address is  :-  " + this.EAddress);
            Console.WriteLine("Employee Age is      :-  " + this.EAge);
        }
    }
    class ClsPConstructor
    {
        static void Main()
        {
           
            ClsEmployee3 Obj1 = new ClsEmployee3(9, "Sampath kumar", "Karimnagr", 25);
            ClsEmployee3 Obj2 = new ClsEmployee3(99, "Samrat", "Hyderabad", 25);
            ClsEmployee3 obj3 = new ClsEmployee3();
            ClsEmployee3 obj4 = new ClsEmployee3(100,"Srinivas");
            Obj1.DisplayEmpData();
            Obj2.DisplayEmpData();
            obj3.DisplayEmpData();
            obj4.DisplayEmpData();
            Console.Read();
        }

    }
}



Output:-


From the above points we can conclude that
  1. A class can contain more than one constructor
  2. A constructor can be overloaded for different no of arguments


No comments:

Post a Comment