/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 18 - PropertiesStatic Properties
// copyright 2000 Eric Gunnerson
class Color
{
public Color(int red, int green, int blue)
{
this.red = red;
this.green = green;
this.blue = blue;
}
int red;
int green;
int blue;
public static Color Red
{
get
{
return(new Color(255, 0, 0));
}
}
public static Color Green
{
get
{
return(new Color(0, 255, 0));
}
}
public static Color Blue
{
get
{
return(new Color(0, 0, 255));
}
}
}
public class StaticProperties
{
static void Main()
{
Color background = Color.Red;
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Delegates as Static Properties
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 22 – DelegatesDelegates as Static Properties
// copyright 2000 Eric Gunnerson
using System;
class Container
{
public delegate int CompareItemsCallback(object obj1, object obj2);
public void SortItems(CompareItemsCallback compare)
{
// not a real sort, just shows what the
// inner loop code might do
int x = 0;
int y = 1;
object item1 = arr[x];
object item2 = arr[y];
int order = compare(item1, item2);
}
object[] arr; // items in the collection
}
class Employee
{
Employee(string name, int id)
{
this.name = name;
this.id = id;
}
public static Container.CompareItemsCallback SortByName
{
get
{
return(new Container.CompareItemsCallback(CompareName));
}
}
public static Container.CompareItemsCallback SortById
{
get
{
return(new Container.CompareItemsCallback(CompareId));
}
}
static int CompareName(object obj1, object obj2)
{
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;
return(String.Compare(emp1.name, emp2.name));
}
static int CompareId(object obj1, object obj2)
{
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;
if (emp1.id > emp2.id)
return(1);
if (emp1.id < emp2.id)
return(-1);
else
return(0);
}
string name;
int id;
}
public class DelegatesasStaticProperties
{
public static void Main()
{
Container employees = new Container();
// create and add some employees here
employees.SortItems(Employee.SortByName);
// employees is now sorted by name
}
}
[/csharp]
Properties Accessors
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 18 - PropertiesAccessors
// copyright 2000 Eric Gunnerson
public class PropertiesAccessors
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
Properties: Side Effects When Setting Values
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 18 - PropertiesSide Effects When Setting Values
// copyright 2000 Eric Gunnerson
using System;
using System.Collections;
class Basket
{
internal void UpdateTotal()
{
total = 0;
foreach (BasketItem item in items)
{
total += item.Total;
}
}
ArrayList items = new ArrayList();
Decimal total;
}
public class BasketItem
{
BasketItem(Basket basket)
{
this.basket = basket;
}
public int Quantity
{
get
{
return(quantity);
}
set
{
quantity = value;
basket.UpdateTotal();
}
}
public Decimal Price
{
get
{
return(price);
}
set
{
price = value;
basket.UpdateTotal();
}
}
public Decimal Total
{
get
{
// volume discount; 10% if 10 or more are purchased
if (quantity >= 10)
return(quantity * price * 0.90m);
else
return(quantity * price);
}
}
int quantity; // count of the item
Decimal price; // price of the item
Basket basket; // reference back to the basket
}
Properties: Use of Properties
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 18 - PropertiesUse of Properties
// copyright 2000 Eric Gunnerson
using System;
public class Auto
{
public Auto(int id, string name)
{
this.id = id;
this.name = name;
}
// query to find # produced
public int ProductionCount
{
get
{
if (productionCount == -1)
{
// fetch count from database here.
}
return(productionCount);
}
}
public int SalesCount
{
get
{
if (salesCount == -1)
{
// query each dealership for data
}
return(salesCount);
}
}
string name;
int id;
int productionCount = -1;
int salesCount = -1;
}
access to a private field through a property
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Property.cs — Demonstrates access to a private field through a property.
// Compile this program with the following command line:
// C:>csc Property.cs
//
namespace nsProperty
{
using System;
public class Property
{
const double radian = 57.29578;
const double pi = 3.14159;
int Angle
{
get
{
int angle = (int) (fAngle * radian + 0.5);
angle = angle == 360 ? 0 : angle;
return (angle);
}
set
{
double angle = (double) value / radian;
if (angle < (2 * pi))
{
fAngle = angle;
Console.WriteLine ("fAngle set to {0,0:F5}", fAngle);
}
else
{
Console.WriteLine ("fAngle not modified");
}
}
}
double fAngle = 0.0; // Angle in radians
static public int Main (string [] args)
{
int angle;
try
{
angle = int.Parse (args[0]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine ("usage: circle [angle in degrees]");
return (-1);
}
catch (FormatException)
{
Console.WriteLine ("Please use a number value for the angle in degrees");
return (-1);
}
Property main = new Property();
main.Angle = angle;
Console.WriteLine ("The angle is {0} degrees", main.Angle);
return (0);
}
}
}
[/csharp]
The use of an abstract property
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Abstract.cs -- Demonsrates the use of an abstract property.
//
// Compile this program with the following command line:
// C:>csc Abstract.cs
//
namespace nsAbstract
{
using System;
using System.Runtime.InteropServices;
public class AbstractPro
{
static public void Main ()
{
Console.WriteLine (clsAbstract.StaticMethod());
}
}
//
// To use the abstract modifier on a method, the class also must
// be declared as abastract
abstract class clsAbstract
{
//
// To declare an abstract method, end the declaration with a semicolon.
// Do not provide a body for the method.
abstract public int AbstractMethod();
//
// An abstract class may contain a static method. You do not have
// to declare an instance of the class to access a static method
static public double StaticMethod()
{
return (3.14159 * 3.14159);
}
abstract public long Prop
{
get;
set;
}
}
//
// Inherit from the abstract class. The following class implements
// the AbstractMethod().
// The access level of the derived class method must be the same
// as the access level of the base class abstract method.
class clsDerivedFromAbstract : clsAbstract
{
override public int AbstractMethod()
{
return (0);
}
override public long Prop
{
get
{
return (val);
}
set
{
val = value;
}
}
private long val;
}
}