/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace Properties { class Time { // private member variables private int year; private int month; private int date; private int hour; private int minute; private int second; // create a property public int Hour { get { return hour; } set { hour = value; } } // public accessor methods public void DisplayCurrentTime() { System.Console.WriteLine( "Time: {0}/{1}/{2} {3}:{4}:{5}", month, date, year, hour, minute, second); } // constructors public Time(System.DateTime dt) { year = dt.Year; month = dt.Month; date = dt.Day; hour = dt.Hour; minute = dt.Minute; second = dt.Second; } } public class PropertiesTester { public void Run() { System.DateTime currentTime = System.DateTime.Now; Time t = new Time(currentTime); t.DisplayCurrentTime(); // access the hour to a local variable int theHour = t.Hour; // display it System.Console.WriteLine("Retrieved the hour: {0}", theHour); // increment it theHour++; // reassign the incremented value back through // the property t.Hour = theHour; // display the property System.Console.WriteLine("Updated the hour: {0}", t.Hour); } [STAThread] static void Main() { PropertiesTester t = new PropertiesTester(); t.Run(); } } }
Month: February 2011
Use properties to set and get private members
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use properties to set and get private members. using System; // A class for two-dimensional objects. class TwoDShape { double pri_width; // now private double pri_height; // now private // Properties for width and height. public double width { get { return pri_width; } set { pri_width = value; } } public double height { get { return pri_height; } set { pri_height = value; } } public void showDim() { Console.WriteLine("Width and height are " + width + " and " + height); } } // A derived class of TwoDShape for triangles. class Triangle : TwoDShape { public string style; // style of triangle // Return area of triangle. public double area() { return width * height / 2; } // Display a triangle's style. public void showStyle() { Console.WriteLine("Triangle is " + style); } } public class Shapes2 { public static void Main() { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(); t1.width = 4.0; t1.height = 4.0; t1.style = "isosceles"; t2.width = 8.0; t2.height = 12.0; t2.style = "right"; Console.WriteLine("Info for t1: "); t1.showStyle(); t1.showDim(); Console.WriteLine("Area is " + t1.area()); Console.WriteLine(); Console.WriteLine("Info for t2: "); t2.showStyle(); t2.showDim(); Console.WriteLine("Area is " + t2.area()); } }
Error handling in property setter validating
using System; public class Employee { private int prop_age; public int age { set { if (value < 0 || value > 120) { throw new ApplicationException("Not valid age!"); } prop_age = value; } get { return prop_age; } } }
Convert errflag into a property
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Convert errflag into a property.
using System;
class FailSoftArray {
int[] a; // reference to underlying array
int len; // length of array
bool errflag; // now private
// Construct array given its size.
public FailSoftArray(int size) {
a = new int[size];
len = size;
}
// Read-only Length property.
public int Length {
get {
return len;
}
}
// Read-only Error property.
public bool Error {
get {
return errflag;
}
}
// This is the indexer for FailSoftArray.
public int this[int index] {
// This is the get accessor.
get {
if(ok(index)) {
errflag = false;
return a[index];
} else {
errflag = true;
return 0;
}
}
// This is the set accessor
set {
if(ok(index)) {
a[index] = value;
errflag = false;
}
else errflag = true;
}
}
// Return true if index is within bounds.
private bool ok(int index) {
if(index >= 0 & index < Length) return true;
return false;
}
}
// Demonstrate the improved fail-soft array.
public class FinalFSDemo {
public static void Main() {
FailSoftArray fs = new FailSoftArray(5);
// use Error property
for(int i=0; i < fs.Length + 1; i++) {
fs[i] = i*10;
if(fs.Error)
Console.WriteLine("Error with index " + i);
}
}
}
[/csharp]
Add Length property to FailSoftArray
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add Length property to FailSoftArray.
using System;
class FailSoftArray {
int[] a; // reference to underlying array
int len; // length of array — underlies Length property
public bool errflag; // indicates outcome of last operation
// Construct array given its size.
public FailSoftArray(int size) {
a = new int[size];
len = size;
}
// Read-only Length property.
public int Length {
get {
return len;
}
}
// This is the indexer for FailSoftArray.
public int this[int index] {
// This is the get accessor.
get {
if(ok(index)) {
errflag = false;
return a[index];
} else {
errflag = true;
return 0;
}
}
// This is the set accessor
set {
if(ok(index)) {
a[index] = value;
errflag = false;
}
else errflag = true;
}
}
// Return true if index is within bounds.
private bool ok(int index) {
if(index >= 0 & index < Length) return true;
return false;
}
}
// Demonstrate the improved fail-soft array.
public class ImprovedFSDemo {
public static void Main() {
FailSoftArray fs = new FailSoftArray(5);
int x;
// can read Length
for(int i=0; i < fs.Length; i++)
fs[i] = i*10;
for(int i=0; i < fs.Length; i++) {
x = fs[i];
if(x != -1) Console.Write(x + " ");
}
Console.WriteLine();
// fs.Length = 10; // Error, illegal!
}
}
[/csharp]
A simple property example
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A simple property example. using System; class SimpProp { int prop; // field being managed by myprop public SimpProp() { prop = 0; } /* This is the property that supports access to the private instance variable prop. It allows only positive values. */ public int myprop { get { return prop; } set { if(value >= 0) prop = value; } } } // Demonstrate a property. public class PropertyDemo { public static void Main() { SimpProp ob = new SimpProp(); Console.WriteLine("Original value of ob.myprop: " + ob.myprop); ob.myprop = 100; // assign value Console.WriteLine("Value of ob.myprop: " + ob.myprop); // Can't assign negative value to prop Console.WriteLine("Attempting to -10 assign to ob.myprop"); ob.myprop = -10; Console.WriteLine("Value of ob.myprop: " + ob.myprop); } }
enum based attribute
using System; public enum RemoteServers { A, B, C } public class RemoteObjectAttribute : Attribute { public RemoteObjectAttribute(RemoteServers Server) { this.server = Server; } protected RemoteServers server; public string Server { get { return RemoteServers.GetName( typeof(RemoteServers), this.server); } } } [RemoteObject(RemoteServers.C)] class MyRemotableClass { } class Test { [STAThread] static void Main(string[] args) { Type type = typeof(MyRemotableClass); foreach (Attribute attr in type.GetCustomAttributes(true)) { RemoteObjectAttribute remoteAttr = attr as RemoteObjectAttribute; if (null != remoteAttr) { Console.WriteLine(remoteAttr.Server); } } } }