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:<script runat="server"> public class Square { private double _side; // This is a new property public double Side { set { } } } </script> |
|
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: <script runat="server"> public class Square { private double _side; // This is a new property public double Side { set { _side = value; } } } </script> Clients of a class can change the corresponding field of a member variable through the property writer. Consider the following code: <%@ Page Language="C#" %> <html> <head> <script runat="server"> 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; } } </script> <title>Exercise</title> </head> <body> <% Square sq1 = new Square(); Square sq2 = new Square(); sq1._side = -12.48; sq2._side = 25.55; Response.Write("<pre>First Square Characteristics<br />"); Response.Write("Side: " + sq1._side.ToString() + "<br />"); Response.Write("Perimeter: " + sq1.Perimeter().ToString() + "<br />"); Response.Write("Area: " + sq1.Area().ToString() + "</pre>"); Response.Write("<pre>Second Square Characteristics<br />"); Response.Write("Side: " + sq2._side.ToString() + "<br />"); Response.Write("Perimeter: " + sq2.Perimeter().ToString() + "<br />"); Response.Write("Area: " + sq2.Area().ToString() + "</pre>"); %> </body> </html> Here is an example of testing the code: 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.
If you create a property that has only a set section, the property is referred to as write-only. 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.
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. To create an enumeration property, you use the same formula as one of the primitive data types we have used previously. Here is an example: <script runat="server"> 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; } } } } </script>
Remember that, after creating a class, it becomes a data type in its own right. Here is an example of such a class: <script runat="server"> 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; } } } </script> To create a property that is based on a class, primarily follow the same formulas we have applied to the other properties. Here is an example : <script runat="server"> public class DepartmentStore { 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 DepartmentStore() { itm = new ShoppingItem(); } } </script> |
|