/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Hide.cs -- Show name hiding in a derived class // // Compile this program with the following command line: // C:>csc Hide.cs // namespace nsHide { using System; using System.Reflection; public class Hide { static public void Main () { clsBase Base = new clsBase(); clsDerived Derived = new clsDerived (); Base.x = 42; Derived.x = 42; Console.WriteLine ("For the base class:"); Console.WriteLine (" The type stored in clsBase is " + Base.TypeOf()); Console.WriteLine (" MathOp () returns {0,0:F3} for {1}", Base.MathOp(42), 42); Console.WriteLine (" For the derived class:"); Console.WriteLine (" The type stored in clsDerived is " + Derived.TypeOf()); Console.WriteLine (" MathOp () returns {0,0:F3} for {1}", Derived.MathOp(42), 42); } } class clsBase { protected int m_x; public int x { get {return (x);} set {m_x = value;} } public double MathOp (int val) { return (Math.Sqrt ((double) val)); } public string TypeOf () { return ("integer"); } } class clsDerived : clsBase { new protected double m_x; new public double x { get {return (x);} set {m_x = value;} } new public double MathOp (int val) { return ((double) (val * val)); } new public string TypeOf () { return ("long"); } } }
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
Demonstrate the use of a nested class to contain data
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Nested.cs -- demonstrate the use of a nested class to contain data // // Compile this program with the following command line // C:>csc Nested.cs // namespace nsReadOnly { using System; public class Nested { static double DegreeFactor = 1; static double MilFactor = 0.05625; static double RadianFactor = 57.29578; static public void Main () { double angle = 90; double radius = 50; // Declare an instance of the nested class clsArea.clsData data = new clsArea.clsData (angle, radius, DegreeFactor); clsArea InDegrees = new clsArea (data); // Change the values to mils data.Factor = MilFactor; data.Angle = angle * 17.77778; clsArea InMils = new clsArea (data); // Change the values to radians data.Angle = angle / 57.29578; data.Factor = RadianFactor; clsArea InRadians = new clsArea (data); Console.WriteLine ("Area of pie of {0,0:F3} degrees is {1,0:F1}", InDegrees.Data.Angle, InDegrees.Area); Console.WriteLine ("Area of pie of {0,0:F3} radians is {1,0:F1}", InRadians.Data.Angle, InRadians.Area); Console.WriteLine ("Area of pie of {0,0:F3} mils is {1,0:F1}", InMils.Data.Angle, InMils.Area); } } class clsArea { public class clsData : ICloneable { public clsData (double angle, double radius, double factor) { m_Angle = angle; m_Radius = radius; m_Factor = factor / 57.29578; } public double Angle { get {return(m_Angle);} set {m_Angle = value;} } public double Radius { get {return(m_Radius);} set {m_Radius = value;} } public double Factor { get {return(m_Factor);} set {m_Factor = value / 57.29578;} } private double m_Angle = 0; private double m_Radius = 0; private double m_Factor = 1; public object Clone () { clsData clone = new clsData (m_Angle, m_Radius, m_Factor * 57.29578); return (clone); } } public clsArea (clsData data) { // Clone the data object to get a copy for ourselves m_Data = (clsData) data.Clone(); } public clsData Data { get {return (m_Data);} } private clsData m_Data; private const double pi = 3.14159; private const double radian = 57.29578; public double Area { get { return (m_Data.Radius * m_Data.Radius * pi * m_Data.Angle * m_Data.Factor / (2 * pi)); } } } }
Multiple constructors in a class definition
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Constrct.cs - Demonstrates the use of multiple constructors // in a class definition. // // Compile this program with the following command line: // C:>csc Constrct.cs // namespace nsConstructor { using System; struct POINT { public POINT (int cx, int cy) { this.cx = cx; this.cy = cy; } public int cx; public int cy; } public class Constrct { static public void Main () { clsRect rc1 = new clsRect(); clsRect rc2 = new clsRect (10, 12, 84, 96); POINT pt1 = new POINT (10, 12); POINT pt2 = new POINT (84, 96); clsRect rc3 = new clsRect (pt1, pt2); } } class clsRect { // The following constructor replaces the default constructor public clsRect () { Console.WriteLine ("Default constructor called"); m_Left = m_Top = m_Right = m_Bottom = 0; } public clsRect (int cx1, int cy1, int cx2, int cy2) { Console.WriteLine ("Constructor 1 called"); m_Left = cx1; m_Top = cy1; m_Right = cx2; m_Bottom = cy2; } public clsRect (POINT pt1, POINT pt2) { Console.WriteLine ("Constructor 2 called"); m_Left = pt1.cx; m_Top = pt1.cy; m_Right = pt2.cx; m_Bottom = pt2.cy; } public POINT UpperLeft { get {return(new POINT(m_Left, m_Top));} set {m_Left = value.cx; m_Top = value.cy;} } public POINT LowerRight { get {return(new POINT(m_Right, m_Bottom));} set {m_Right = value.cx; m_Bottom = value.cy;} } private int m_Left; private int m_Top; private int m_Right; private int m_Bottom; } }
Illustrates nested classes
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example6_6.cs illustrates nested classes */ // declare the Car class class Car { // declare the Engine class public class Engine { // declare the Engine fields public int cylinders; public int horsepower; // define the Engine method public void Start() { System.Console.WriteLine("Engine started"); } } // declare the Car fields public string make; public Engine engine; // Car has an Engine // define the Car method public void Start() { engine.Start(); } } public class Example6_6 { public static void Main() { // declare a Car object reference named myCar System.Console.WriteLine("Creating a Car object"); Car myCar = new Car(); myCar.make = "Toyota"; // Car objects have an Engine object System.Console.WriteLine("Creating an Engine object"); myCar.engine = new Car.Engine(); myCar.engine.cylinders = 4; myCar.engine.horsepower = 180; // display the values for the Car and Engine object fields System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.engine.cylinders = " + myCar.engine.cylinders); System.Console.WriteLine("myCar.engine.horsepower = " + myCar.engine.horsepower); // call the Car object's Start() method myCar.Start(); } }
illustrates how to use a 'has a' relationship
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example6_5.cs illustrates how to use a "has a" relationship */ // declare the Engine class class Engine { // declare the fields public int cylinders; public int horsepower; // define the method public void Start() { System.Console.WriteLine("Engine started"); } } // declare the Car class class Car { // declare the fields public string make; public Engine engine; // Car has an Engine // define the method public void Start() { engine.Start(); } } public class Example6_5 { public static void Main() { // declare a Car object reference named myCar System.Console.WriteLine("Creating a Car object"); Car myCar = new Car(); myCar.make = "Toyota"; // Car objects have an Engine object System.Console.WriteLine("Creating an Engine object"); myCar.engine = new Engine(); myCar.engine.cylinders = 4; myCar.engine.horsepower = 180; // display the values for the Car and Engine object fields System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.engine.cylinders = " + myCar.engine.cylinders); System.Console.WriteLine("myCar.engine.horsepower = " + myCar.engine.horsepower); // call the Car object's Start() method myCar.Start(); } }
Illustrates how to assign default values to fields using initializers
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_2.cs illustrates how to assign default values to fields using initializers */ // declare the Car class class Car { // declare the fields public string make = "Ford"; public string model = "T"; public string color; // default value of null public int yearBuilt = 1910; // define the methods public void Start() { System.Console.WriteLine(model + " started"); } public void Stop() { System.Console.WriteLine(model + " stopped"); } } public class Example5_2 { public static void Main() { // create a Car object Car myCar = new Car(); // display the default values for the Car object fields System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.model = " + myCar.model); if (myCar.color == null) { System.Console.WriteLine("myCar.color is null"); } System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); } }
Illustrates how to declare classes, object references, and create objects
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_1.cs illustrates how to declare classes, object references, and create objects */ // declare the Car class class Car { // declare the fields public string make; public string model; public string color; public int yearBuilt; // define the methods public void Start() { System.Console.WriteLine(model + " started"); } public void Stop() { System.Console.WriteLine(model + " stopped"); } } public class Example5_1 { public static void Main() { // declare a Car object reference named myCar Car myCar; // create a Car object, and assign its address to myCar System.Console.WriteLine("Creating a Car object and assigning " + "its memory location to myCar"); myCar = new Car(); // assign values to the Car object's fields using myCar myCar.make = "Toyota"; myCar.model = "MR2"; myCar.color = "black"; myCar.yearBuilt = 1995; // display the field values using myCar System.Console.WriteLine("myCar details:"); System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.model = " + myCar.model); System.Console.WriteLine("myCar.color = " + myCar.color); System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); // call the methods using myCar myCar.Start(); myCar.Stop(); // declare another Car object reference and // create another Car object System.Console.WriteLine("Creating another Car object and " + "assigning its memory location to redPorsche"); Car redPorsche = new Car(); redPorsche.make = "Porsche"; redPorsche.model = "Boxster"; redPorsche.color = "red"; redPorsche.yearBuilt = 2000; System.Console.WriteLine("redPorsche is a " + redPorsche.model); // change the object referenced by the myCar object reference // to the object referenced by redPorshe System.Console.WriteLine("Assigning redPorsche to myCar"); myCar = redPorsche; System.Console.WriteLine("myCar details:"); System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.model = " + myCar.model); System.Console.WriteLine("myCar.color = " + myCar.color); System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); // assign null to myCar (myCar will no longer reference an object) myCar = null; } }