Since we can access private field-data directly in a class where it's been declared, so we kind of have a habit of assigning values directly to these private field-data members in our constructors or any other method in the class.
example:
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company( string name; int Num)
{
nameOFCompany = name;
licenseNumber = Num;
}
//property 'Name' to get and set the value
public string Name
{
get
{ return nameOfCompany; }
set
{ // check condition before assigning value to private field-data
if(value.Length < 26)
nameOfCompany = value;
else
Console.WriteLine("Name exceeds maximum limit");
}
}
//similarly property 'LicenseNum' for private field-data licenseNumber
public int LicenseNum
{
get
{ return licenseNumber; }
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
----------------------------------------------------------------------------------
example:
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company( string name; int Num)
{
nameOFCompany = name;
licenseNumber = Num;
}
//property 'Name' to get and set the value
public string Name
{
get
{ return nameOfCompany; }
set
{ // check condition before assigning value to private field-data
if(value.Length < 26)
nameOfCompany = value;
else
Console.WriteLine("Name exceeds maximum limit");
}
}
//similarly property 'LicenseNum' for private field-data licenseNumber
public int LicenseNum
{
get
{ return licenseNumber; }
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
----------------------------------------------------------------------------------
class Program
{
static void Main(string[] args)
{
Company objRef = new Company("XYZ InfoTech Pvt. Ltd', 12343);
//here we're setting the property 'Name' to assign value to private field-data //'nameOfCompany' and it's commented coz the constructor is doing same job
//objRef.Name = "XYZ InfoTech Pvt. Ltd";
//here we're setting the property 'LicenseNumber' to assign value to private field-data //'licenseNumber' and it's commented coz the constructor is doing same job
//objRef.LicenseNum = 12343;
//here we're calling the property 'Name' which further will call the get scope of the property //and returns the value it's supposed to send back
Console.WriteLine("Company name is : {0}" , objRef.Name);
Console.ReadLine();
}
}
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
Note:
If you notice closely, you'll observe a sense of code duplication, where the constructor and the set scope of the properties have same code, which can be avoided by using the properties in the constructor rather than assigning to the private field-data, which is already happening at set { } scope of the properties( this'll make the re-use the code written at set{ } scope properties)
Therefore the class Company can be modified as:
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company( string name; int Num)
{
//notice now properties are used to assign values to field-data rather than assigning to //private field-data as done previously
Name = name;
LicenseNum = Num;
}
//property 'Name' to get and set the value
public string Name
{
get
{ return nameOfCompany; }
set
{ // check condition before assigning value to private field-data
if(value.Length < 26)
nameOfCompany = value;
else
Console.WriteLine("Name exceeds maximum limit");
}
}
//similarly property 'LicenseNum' for private field-data licenseNumber
public int LicenseNum
{
get
{ return licenseNumber; }
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company( string name; int Num)
{
//notice now properties are used to assign values to field-data rather than assigning to //private field-data as done previously
Name = name;
LicenseNum = Num;
}
//property 'Name' to get and set the value
public string Name
{
get
{ return nameOfCompany; }
set
{ // check condition before assigning value to private field-data
if(value.Length < 26)
nameOfCompany = value;
else
Console.WriteLine("Name exceeds maximum limit");
}
}
//similarly property 'LicenseNum' for private field-data licenseNumber
public int LicenseNum
{
get
{ return licenseNumber; }
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
This is how we can make use of properties in the class itself where it's been declare, rather wherever value has to be assigned or to get the existing value of private field-data, we can comfortably make use of property syntax avoiding code-duplication.
No comments:
Post a Comment