Demonstrate the use of readonly variables
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// ReadOnly.cs -- demonstrate the use of readonly variables
//
// Compile this program with the following command line
// C:>csc ReadOnly.cs
//
namespace nsReadOnly
{
using System;
public class ReadOnly
{
static double DegreeFactor = 1;
static double MilFactor = 0.05625;
static public void Main ()
{
double degrees = 42;
// 1 degree = 17.77778 mils
double mils = degrees * 17.77778;
// 1 degree = 0.017453 radians
double radians = degrees * 0.017453;
clsArea InDegrees = new clsArea (DegreeFactor);
InDegrees.Angle = degrees;
InDegrees.Radius = 50;
Console.WriteLine ("Area of circle is {0,0:F1}", InDegrees.Area);
// Radians are the default, so you can use the parameterless
// constructor
clsArea InRadians = new clsArea ();
InRadians.Angle = radians;
InRadians.Radius = 50;
Console.WriteLine ("Area of circle is {0,0:F1}", InRadians.Area);
clsArea InMils = new clsArea (MilFactor);
InMils.Angle = mils;
InMils.Radius = 50;
Console.WriteLine ("Area of circle is {0,0:F1}", InMils.Area);
}
}
class clsArea
{
public clsArea ()
{
}
public clsArea (double factor)
{
m_Factor = factor / 57.29578;
}
private const double pi = 3.14159;
private const double radian = 57.29578;
private readonly double m_Factor = 1;
public double Angle
{
get {return (m_Angle);}
set {m_Angle = value;}
}
public double Radius
{
get {return (m_Radius);}
set {m_Radius = value;}
}
private double m_Angle;
private double m_Radius;
public double Area
{
get
{
return (m_Radius * m_Radius * pi * m_Angle * m_Factor / (2 * pi));
}
}
}
}
Related examples in the same category