Static properties are meant for the static field-data members, means you can have static property in place in-order to get and set static field-data. There's a separate post for the understanding of static members of the class and the importance of static keyword in C# language.
For the time being just understand that you need not to create object of a class using new keyword( as we use to do in : Company objRef = new Company(); ) to invoke static members of the class, only class reference will do the job.
Example
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
// Notice the static field-data
private static double stockPrice;
//Notice the static property in place to get and set private static field-data stockPrice
public static double mktStockPrice
{
get { return stockPrice; }
set { stockPrice = value; }
}
//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");
}
}
}
--------------------------------------------------------------------------------------------------
//Notice we used class reference instead of object-reference to set static property //'mktStockPrice'
Company.mktStockPrice = 1181.32;
//here also we used class reference because static members are invoked by class reference
For the time being just understand that you need not to create object of a class using new keyword( as we use to do in : Company objRef = new Company(); ) to invoke static members of the class, only class reference will do the job.
Example
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
// Notice the static field-data
private static double stockPrice;
//Notice the static property in place to get and set private static field-data stockPrice
public static double mktStockPrice
{
get { return stockPrice; }
set { stockPrice = value; }
}
//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)
{
// object creation of class Company
// object creation of class Company
Company objRef = new Company();
//Notice we used class reference instead of object-reference to set static property //'mktStockPrice'
Company.mktStockPrice = 1181.32;
//here also we used class reference because static members are invoked by class reference
Console.WriteLine("Company name is : {0}" , Company.mktStockPrice);
Console.ReadLine();
}
}
Note:
Static field-data members are class level or universal kind of members rather than object level, when I say class level and not object level, it means whatever value you set for any static field-data member then that field-data value will be available throughout the objects you create.
As shown in the above example, the value of private field-data 'stockPrice' is set to 1181.32 via static property 'mktStockPrice', now this value remains the same for all the objects you are going to create for the class, because there's only one copy of the static field-data member which works at the class level and all the objects of the class use that single copy of the static field-data.
No comments:
Post a Comment