The Properties of a Class
In C++ and Java, when creating the member variables of a class, programmers usually "hide" these members in private sections (C++) or create them as…
Overview of Properties#
Introduction#
In languages like C++ and Java, developers often create specific methods to manage access to class variables.
Accessories for Properties#
Properties act as intermediaries within a class, providing a controlled way to read or write values. Here is an example:
using System;
public class Square
{
private double _side;
}
public class Exercise
{
public static int Main()
{
return 0;
}
}Obviously this private field cannot be accessed by an outside class. To let the outside classes access this variable, you would/can create a property.
Creating a Property#
To indicate that you are creating a property, there is a syntax you must follow. To start, you must create a member whose formula resembles a method without the parentheses. Since, or if, the property will be accessed only by objects of the same program, you can mark it with the internal keyword. If the property will be accessed by objects of this and other programs, you should mark it as public. Therefore, to create a property, specify an access modifier, usually public. Then specify the data type of the property. This is followed by a name. The name of a property primarily follows the rules we reviewed for names of variables. We will adopt other conventions as follows. For the name of private field:
- The name of the private field corresponding to the property can be a single letter. In this case, the letter will be in lowercase except for l, in which case we will use L
- The name of the private field corresponding to the property can start with an underscore or a letter If the name is in one word, it will be in lowercase If the name is a combination of words, the first word will be in lowercase and the first letter of each subsequent word will be in uppercase
For the name of the property
- The name of a property will always start with a letter
- After the first letter, the name can include letters and digits
- If the name is made of one word, the first letter will be in uppercase. The other letters will be in lowercase. If the name includes digits, they will be kept
- If the name is a combination of words, the first letter of each word will be in uppercase
Here is an example of a property:
public class Square
{
private double _side;
// This is a new property
public double Side
{
}
}With regards to their role, there are two types of properties. Application: Introducing Properties
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New Project...
- In the middle list, click Empty Project
- Change the Name to DepartmentStore9
- Click OK
- To create a new class, on the main menu, click Project -> Add Class...
- Set the Name to ShoppingItem and press Enter
- Change the content of the file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { public class DepartmentStore { private long itemNo; private string nm; private string sz; private decimal price; } }
- To create a property for each member variable, change the ShoppingItem class as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { class ShoppingItem { private string itemNo; private string nm; private string sze; private decimal price; // A constructor used to initialize an item public ShoppingItem(long nbr, string nme, string siz, decimal prc) { itemNo = nbr; nm = nme; sz = siz; price = prc; } // A property for the stock number of an item public string ItemNumber { } // A property for the name of an item public string Name { } // A property for size of a merchandise public string Size { } // A property for the marked price of an item public decimal UnitPrice { } } }
Types of Properties#
Property Readers#
A property is referred to as read if its role is only to make available the value of the member variable it represents. To create a read property, in the body of the property, type the get keyword and create a body for the keyword, using the traditional curly brackets that delimit a section of code. Here is an example:
public class Square
{
private double _side;
// This is a new property
public double Side
{
get
{
}
}
}In the body of the get clause, you can implement the behavior that would be used to make the field's value available outside. The simplest way consists of just returning the corresponding field. Here is an example:
public class Square
{
private double _side;
// This is a new property
public double Side
{
get
{
return _side;
}
}
}Since the private field is a member of the class, you can also use this to access it. Here is an example:
public class Square
{
private double _side;
// This is a new property
public double Side
{
get
{
return this._side;
}
}
}A read property is also referred to as read-only property because the clients of the class can only retrieve the value of the property but they cannot change it. Therefore, if you create (only) a read property, you should provide the users with the ability to primarily specify the value of the member variable. To do this, you can create an accessory method or a constructor for the class . Here is an example of such a constructor:
public class Square
{
private double _side;
// This is a new property
public double Side
{
get
{
return this._side;
}
}
public Square(double s)
{
this._side = s;
}
}Once a read property has been created, other classes can access it, for example they can read its value as follows:
using System;
public class Square
{
private double _side;
// This is a new property
public double Side
{
get
{
return this._side;
}
}
public Square(double s)
{
this._side = s;
}
}
public class Exercise
{
public static int Main()
{
var sq = new Square(-25.55);
Console.WriteLine("Square Side: {0}", sq.Side);
return 0;
}
}This would produce:
Square Side: -25.55
Press any key to continue...We can think of a property as a gateway that regulates interaction with a class's internal data from the outside.
using System;
public class Square
{
private double _side;
// This is a new property
public double Side
{
get
{
// If the value given to the side is negative,
// then set it to 0
if (_side < 0)
return 0;
else
return _side;
}
}
public Square(double s)
{
_side = s;
}
}
public class Exercise
{
public static int Main()
{
var sq1 = new Square(-12.48);
var sq2 = new Square(25.55);
Console.WriteLine("First Square Characteristics");
Console.WriteLine("Side: {0}\n", sq1.Side);
Console.WriteLine("Second Square Characteristics");
Console.WriteLine("Side: {0}\n", sq2.Side);
return 0;
}
}This would produce:
First Square Characteristics
Side: 0
Second Square Characteristics
Side: 25.55Application: Creating Property Readers
- To create read properties, change the contents of the ShoppingStore.cs file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { class ShoppingItem { private long itemNo; private string nm; private string sz; private decimal price; // A constructor used to initialize an item public ShoppingItem(long nbr, string nme, string siz, decimal prc) { itemNo = nbr; nm = nme; sz = siz; price = prc; } // A property for the stock number of an item public long ItemNumber { get { return itemNo; } } // A property for the name of an item public string Name { get { return nm; } } // A property for size of a merchandise public string Size { get { if( sz == "0" ) return "Unknown Size or Fits All"; else return sz; } } // A property for the marked price of an item public decimal UnitPrice { get { return price; } } } }
- To create a new file, on the main menu, click Project -> Add New Item...
- In the middle list, click Code File
- Change the Name to DepartmentStore and press Enter
- In the empty file, type the following: using System; using DepartmentStore9; public class DepartmentStore { public static int Main() { long number; string name; string size; decimal price; Console.WriteLine("/-/Fun Department Store/-/"); Console.Write("Enter the Item #: "); number = long.Parse(Console.ReadLine()); Console.Write("Enter the Item Name: "); name = Console.ReadLine(); Console.Write("Enter the Item Size: "); size = Console.ReadLine(); Console.Write("Enter the Unit Price: "); price = decimal.Parse(Console.ReadLine()); ShoppingItem store = new ShoppingItem(number, name, size, price); Console.Clear(); Console.WriteLine("================================"); Console.WriteLine("/-/Fun Department Store/-/"); Console.WriteLine("--------------------------------"); Console.WriteLine("Customer Invoice"); Console.WriteLine("Item #: {0}", store.ItemNumber); Console.WriteLine("Description: {0}", store.Name); Console.WriteLine("Item Size: {0}", store.Size); Console.WriteLine("Unit Price: {0:C}", store.UnitPrice); Console.WriteLine("================================\n"); System.Console.ReadKey(); return 0; } }
- To execute the application, on the main menu, click Debug -> Start Debugging
- When requested to enter values in the DOS window, use the following (remember to press Enter after each value: /-/Fun Department Store/-/ Enter the Item #: 622805 Enter the Item Name: Black Leather Hand Bag Enter the Item Size: Medium Enter the Unit Price: 85.95 ================================ /-/Fun Department Store/-/ -------------------------------- Customer Invoice Item #: 622805 Description: Black Leather Hand Bag Item Size: Medium Unit Price: $85.95 ================================ Press any key to continue . . .
- Close the DOS window
Property Writers#
In our Square class so far, we were using a constructor to create a value for each of the necessary member variables. This meant that we had to always make sure that we knew the value of the field when we declared an instance of the class. Sometimes, this is not effective. For example, you cannot just call a constructor in the middle of the program, that is after the object has been declared, to assign a new value to the field. To solve this kind of problem, you can provide another means of accessing the field any time to change its value. Besides, or instead of, retrieving the value of a field of a class, you may want external classes to be able to change the value of that member. Continuing with our concern to hide a field as private, you can create another type of property. A property is referred to as write if it can change (or write) the value of its corresponding field. To create a write property, type the set keyword followed by the curly bracket delimiters. Here is an example:
public class Square
{
private double _side;
// This is a new property
public double Side
{
set
{
}
}
}The minimum assignment you can perform with a write property is to assign it a value that would be provided by the outside world. To support this, C# provides the value contextual keyword (contextual means the word is a keyword only in some cases, depending on how it is being used). Here is an example:
public class Square
{
private double _side;
// This is a new property
public double Side
{
set
{
_side = value;
}
}
}As you see, clients of a class can change the corresponding field of a member variable through the property writer. Based on this relationship, it is not unusual for a client of a class to make an attempt to "mess" with a field. For example, an external object can assign an invalid value to a member variable. Consider the following program:
using System;
public class Square
{
public double _side;
// This is a new property
public double Side
{
set
{
_side = value;
}
}
public Square()
{
_side = 0;
}
public Square(double s)
{
_side = s;
}
public double Perimeter()
{
return _side * 4;
}
public double Area()
{
return _side * _side;
}
}
public class Exercise
{
public static int Main()
{
var sq1 = new Square();
var sq2 = new Square();
sq1._side = -12.48;
sq2._side = 25.55;
Console.WriteLine("First Square Characteristics");
Console.WriteLine("Side: {0}", sq1._side);
Console.WriteLine("Perimeter: {0}", sq1.Perimeter());
Console.WriteLine("Area: {0}\n", sq1.Area());
Console.WriteLine("Second Square Characteristics");
Console.WriteLine("Side: {0}", sq2._side);
Console.WriteLine("Perimeter: {0}", sq2.Perimeter());
Console.WriteLine("Area: {0}", sq2.Area());
return 0;
}
}This would produce:
First Square Characteristics
Side: -12.48
Perimeter: -49.92
Area: 155.7504
Second Square Characteristics
Side: 25.55
Perimeter: 102.2
Area: 652.8025
Press any key to continue...Because of this, and since it is through the writer that the external objects would change the value of the member variable, you can use the write property, rather than the reader, to validate or reject a new value assigned to the field. Remember that the client objects of the class can only read the value of the field through the read property. Therefore, there may be only little concern on that side.
Read/Write Properties#
If you create a property that has only a set section, the property is referred to as write-only because the other classes can only assign it a value (or write a value to it). If you create a property that has both a get and a set sections, its corresponding member variable can receive new values from outside the class and the member variable can provide its values to clients of the class. Such a property is referred to as read/write. To create a read/write property:
- Create a get and a set accessors in the body of the property
- Right-click the section where you want to create the property and click Insert Snippet... Double-click Visual C#. In the list, double-click propfull
- Consider the following code: public class Exercise { int nbr; } If you have already declared a variable in a class, you can transform it into a property. To do that, right-click the field, position the mouse on Refactor, and double-click Encapsulate Field... This would display the Encapsulate Field dialog box. In the Property Name text box, enter the desired name for the property you want to create Once you are ready, click OK. This would open the Preview Reference Changes dialog box. Use it to check how the property will be created: If everything looks alright, click Apply: public class Exercise { int nbr; public int Number { get { return nbr; } set { nbr = value; } } }
Application: Creating Property Writers
- To create property writers and complete the program, change the content of the ShoppingItem.cs file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { class ShoppingItem { private long itemNo; private string nm; private string sz; private decimal price; // A property for the stock number of an item public long ItemNumber { get { return itemNo; } set { if (itemNo <= 0) itemNo = 0; else itemNo = value; } } // A property for the name of an item public string Name { get { return nm; } set { if (nm == "") nm = "Item no Description"; else nm = value; } } // A property for size of a merchandise public string Size { get { if( sz == "0" ) return "Unknown Size or Fits All"; else return sz; } set { sz = value; } } // A property for the marked price of an item public decimal UnitPrice { get { return price; } set { if (price < 0) price = 0.00M; else price = value; } } public static ShoppingItem Read() { ShoppingItem shop = new ShoppingItem(); Console.Write("Item #: "); shop.itemNo = long.Parse(Console.ReadLine()); Console.Write("Item Name: "); shop.Name = Console.ReadLine(); Console.Write("Item Size (Enter 0 if unknown): "); shop.Size = Console.ReadLine(); Console.Write("Unit Price: "); shop.UnitPrice = decimal.Parse(Console.ReadLine()); return shop; } public static void Write(ShoppingItem item) { Console.WriteLine("Item #: {0}", item.ItemNumber); Console.WriteLine("Description: {0}", item.Name); Console.WriteLine("Item Size: {0}", item.Size); Console.WriteLine("Unit Price: {0:C}", item.UnitPrice); } } }
- Access the DepartmentStore.cs file and change it as follows: using System; using DepartmentStore9; public class DepartmentStore { public static int Main() { Console.WriteLine("/-/Fun Department Store/-/"); Console.Write("Enter the following pieces of "); Console.WriteLine("information about the sale item"); ShoppingItem saleItem = ShoppingItem.Read(); Console.Clear(); Console.WriteLine("================================"); Console.WriteLine("/-/Fun Department Store/-/"); Console.WriteLine("--------------------------------"); ShoppingItem.Write(saleItem); System.Console.ReadKey(); return 0; } }
- To execute the program, on the main menu, click Debug -> Start Debugging
- Enter the values as follows: /-/Fun Department Store/-/ Enter the following pieces of information about the sale item Item #: 114888 Item Name: North Hook Alpine Jacket Item Size (Enter 0 if unknown): Large Unit Price: 275.95 ================================ /-/Fun Department Store/-/ -------------------------------- Item #: 114888 Description: North Hook Alpine Jacket Item Size: Large Unit Price: $275.95
- Close the DOS window
A Boolean Property#
You can create a property as a Boolean type. To do this, first specify its data type as bool. When treating the property, make sure its get accessory returns a Boolean type. A Boolean property is not like the other regular properties. It must specify its value as true or false. As done for Boolean methods, a Boolean property must produce only a true or false value.
Properties of External Classes#
Properties and Enumerations#
An enumeration is a technique of creating a data type that mimics an integer. After creating it, you can treat it as a pseudo data type. You can declare a variable from it, you can pass it as an argument, and you can return it from a method. Based on these characteristics, you can create a property that is based on an enumeration. To create an enumeration property, you use the same formula as one of the primitive data types we have used previously. Keep it mind that the property is of the type of an enumeration. This means that you cannot request its value like that of a primitive type and, when manipulating it, you must process it appropriately. Application: Creating an Enumeration Property
- Access the ShoppingStore.cs file and change it as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { public enum ItemCategory { Unspecified, Women, Men, Girls, Boys, Babies } class ShoppingItem { private long itemNo; private ItemCategory cat; private string nm; private string sz; private decimal price; // A property for the stock number of an item public long ItemNumber { get { return itemNo; } set { if (itemNo <= 0) itemNo = 0; else itemNo = value; } } // A property for the category of item public ItemCategory Category { get { return cat; } set { cat = value; } } // A property for the name of an item public string Name { get { return nm; } set { if (nm == "") nm = "Item no Description"; else nm = value; } } // A property for size of a merchandise public string Size { get { if( sz == "0" ) return "Unknown Size or Fits All"; else return sz; } set { sz = value; } } // A property for the marked price of an item public decimal UnitPrice { get { return price; } set { if (price < 0) price = 0.00M; else price = value; } } public static ShoppingItem Read() { int category = 0; ShoppingItem shop = new ShoppingItem(); Console.Write("Item #: "); shop.itemNo = long.Parse(Console.ReadLine()); Console.WriteLine("Store Items Categories"); Console.WriteLine("\t1. Women"); Console.WriteLine("\t2. Men"); Console.WriteLine("\t3. Girls"); Console.WriteLine("\t4. Boys"); Console.WriteLine("\t5. Babies"); Console.Write("Enter the Category: "); category = int.Parse(Console.ReadLine()); if (category == 1) shop.Category = ItemCategory.Women; else if (category == 2) shop.Category = ItemCategory.Men; else if (category == 3) shop.Category = ItemCategory.Girls; else if (category == 4) shop.Category = ItemCategory.Boys; else if (category == 5) shop.Category = ItemCategory.Babies; else shop.Category = ItemCategory.Unspecified; Console.Write("Item Name: "); shop.Name = Console.ReadLine(); Console.Write("Item Size (Enter 0 if unknown): "); shop.Size = Console.ReadLine(); Console.Write("Unit Price: "); shop.UnitPrice = decimal.Parse(Console.ReadLine()); return shop; } public static void Write(ShoppingItem item) { Console.WriteLine("Item #: {0}", item.ItemNumber); Console.WriteLine("Category: {0}", item.Category); Console.WriteLine("Description: {0}", item.Name); Console.WriteLine("Item Size: {0}", item.Size); Console.WriteLine("Unit Price: {0:C}", item.UnitPrice); } } }
- Execute the application and test it. Here is an example: /-/Fun Department Store/-/ Enter the following pieces of information about the sale item Item #: 624008 Store Items Categories 1. Women 2. Men 3. Girls 4. Boys 5. Babies Enter the Category: 3 Item Name: Scotta Miniskirt Item Size (Enter 0 if unknown): 11 Unit Price: 35.95 ================================ /-/Fun Department Store/-/ -------------------------------- Item #: 624008 Category: Girls Description: Scotta Miniskirt Item Size: 11 Unit Price: $35.95
- Close the DOS window
A Class as a Property#
Remember that, after creating a class, it becomes a data type in its own right. We have seen that you could declare a variable from it, you could pass it as argument, and you could return it from a method. As a normal data type, a class can be validated. This means that its value can be evaluated, rejected, or retrieved. Based on these characteristics of a class, you can create a property from it. To create a property that is based on a class, primarily follow the same formulas we have applied to the other properties. The most important aspect to remember is that the class is composite. That is, it is (likely) made of fields of various types. Application: Creating a Property of a Class Type
- Access the ShoppingItem.cs file and change it as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { public enum ItemCategory { Unspecified, Women, Men, Girls, Boys, Babies } public class ShoppingItem { private long itemNo; private ItemCategory cat; private string nm; private string sz; private decimal price; // A property for the stock number of an item public long ItemNumber { get { return itemNo; } set { itemNo = value; } } // A property for the category of item public ItemCategory Category { get { return cat; } set { cat = value; } } // A property for the name of an item public string Name { get { return nm; } set { if (nm == "") nm = "Item no Description"; else nm = value; } } // A property for size of a merchandise public string Size { get { if (sz == "0") return "Unknown Size or Fits All"; else return sz; } set { sz = value; } } // A property for the marked price of an item public decimal UnitPrice { get { return price; } set { if (price < 0) price = 0.00M; else price = value; } } } }
- To create a new class, in the Class View, right-click DepartmentStore9 -> Add -> Class...
- Set the Name to OrderProcessing and click Add
- Change the file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DepartmentStore9 { public class OrderProcessing { private int qty; private ShoppingItem itm; public int Quantity { get { return qty; } set { if (qty <= 0) qty = 0; else qty = value; } } public ShoppingItem SaleItem { get { return itm; } set { if (itm == null) { itm.ItemNumber = 0; itm.Category = ItemCategory.Unspecified; itm.Name = "Unknown"; itm.Size = "0"; itm.UnitPrice = 0.00M; } else itm = value; } } public OrderProcessing() { itm = new ShoppingItem(); } public void ProcessOrder() { int category; Console.WriteLine("/-/Fun Department Store/-/"); Console.WriteLine("Enter the following pieces of"); Console.WriteLine("information about the sale item"); Console.Write("Item #: "); itm.ItemNumber = long.Parse(Console.ReadLine()); Console.WriteLine("Store Items Categories"); Console.WriteLine("\t1. Women"); Console.WriteLine("\t2. Men"); Console.WriteLine("\t3. Girls"); Console.WriteLine("\t4. Boys"); Console.WriteLine("\t5. Babies"); Console.Write("Enter the Category: "); category = int.Parse(Console.ReadLine()); if (category == 1) itm.Category = ItemCategory.Women; else if (category == 2) itm.Category = ItemCategory.Men; else if (category == 3) itm.Category = ItemCategory.Girls; else if (category == 4) itm.Category = ItemCategory.Boys; else if (category == 5) itm.Category = ItemCategory.Babies; else itm.Category = ItemCategory.Unspecified; Console.Write("Item Name: "); itm.Name = Console.ReadLine(); Console.Write("Item Size (Enter 0 if unknown): "); itm.Size = Console.ReadLine(); Console.Write("Unit Price: "); itm.UnitPrice = decimal.Parse(Console.ReadLine()); Console.Write("How many samples of "); Console.Write(itm.Name); Console.Write(": "); qty = int.Parse(Console.ReadLine()); } public void DisplayReceipt() { decimal totalPrice = itm.UnitPrice * Quantity; Console.WriteLine("\n================================"); Console.WriteLine("/-/Fun Department Store/-/"); Console.WriteLine("--------------------------------"); Console.WriteLine("Item #: {0}", itm.ItemNumber); Console.WriteLine("Category: {0}", itm.Category); Console.WriteLine("Description: {0}", itm.Name); Console.WriteLine("Item Size: {0}", itm.Size); Console.WriteLine("Unit Price: {0:C}", itm.UnitPrice); Console.WriteLine("Quantity: {0}", Quantity); Console.WriteLine("Total Price: {0:C}\n", totalPrice); Console.WriteLine("\n================================"); } } }
- Access the DepartmentStore.cs file and change it as follows: using System; using DepartmentStore9; public class DepartmentStore { public static int Main() { OrderProcessing op = new OrderProcessing(); op.ProcessOrder(); Console.Clear(); op.DisplayReceipt(); Console.ReadKey(); return 0; } }
- To execute the program, press F5
- When requested to enter values in the DOS window, use the following: /-/Fun Department Store/-/ Enter the following pieces of information about the sale item Item #: 444412 Store Items Categories 1. Women 2. Men 3. Girls 4. Boys 5. Babies Enter the Category: 1 Item Name: Stretch Cotton Shirt Item Size (Enter 0 if unknown): 14 Unit Price: 55.95 How many samples of Stretch Cotton Shirt: 2
- Press Enter ================================ /-/Fun Department Store/-/ -------------------------------- Item #: 444412 Category: Women Description: Stretch Cotton Shirt Item Size: 14 Unit Price: $55.95 Quantity: 2 Total Price: $111.90 ================================
- Close the DOS window and return to your programming environment
- Close Microsoft Visual Studio
- When asked whether you want to save, click Discard
Home Copyright © 2010-2011 C# Key

