/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// This.cs -- Demonstrates using the this intrinsic variable, which
// allows a class instance to identify itself
//
// Compile this program with the following command line:
// C:>csc this.cs
//
namespace nsThis
{
using System;
public class ThisclsMain
{
static public void Main ()
{
// Declare an array of classes
clsThis [] arr = new clsThis[]
{
new clsThis(), new clsThis(), new clsThis(),
new clsThis(), new clsThis(), new clsThis()
};
Console.WriteLine ("{0} instances were created", arr[0].m_Instance);
// Ask each instance in the array to identify itself
foreach (clsThis inst in arr)
{
Console.WriteLine ("This is instance Number " +
inst.Identify().Instance);
}
}
}
internal class clsThis
{
public clsThis ()
{
m_Instance = ++Count;
}
private static int Count = 0;
public int Instance
{
get {return (m_Instance);}
}
internal int m_Instance;
public clsThis Identify ()
{
// Return this instance of the class
return (this);
}
}
}
Class Interface
Illustrates the use of the this object reference
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_5.cs illustrates the use of the this
object reference
*/
// declare the Car class
class Car
{
public int yearBuilt;
public void SetYearBuilt(int yearBuilt)
{
// the yearBuilt parameter hides the
// the yearBuilt field
this.yearBuilt = yearBuilt;
}
}
public class Example5_5
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
myCar.SetYearBuilt(2000);
System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt);
}
}
Using the this Object Reference
public class Product {
public int yearBuilt;
public void SetYearBuilt(int yearBuilt) {
this.yearBuilt = yearBuilt;
}
}
class MainClass{
public static void Main() {
Product myProduct = new Product();
myProduct.SetYearBuilt(2000);
System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);
}
}
Conversions Between Structs 2
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 24 - User-Defined ConversionsConversions Between Structs
// copyright 2000 Eric Gunnerson
using System;
using System.Text;
struct RomanNumeral
{
public RomanNumeral(short value)
{
if (value > 5000)
throw(new ArgumentOutOfRangeException());
this.value = value;
}
public static explicit operator RomanNumeral(
short value)
{
RomanNumeral retval;
retval = new RomanNumeral(value);
return(retval);
}
public static implicit operator short(
RomanNumeral roman)
{
return(roman.value);
}
static string NumberString(
ref int value, int magnitude, char letter)
{
StringBuilder numberString = new StringBuilder();
while (value >= magnitude)
{
value -= magnitude;
numberString.Append(letter);
}
return(numberString.ToString());
}
public static implicit operator string(
RomanNumeral roman)
{
int temp = roman.value;
StringBuilder retval = new StringBuilder();
retval.Append(RomanNumeral.NumberString(ref temp, 1000, 'M'));
retval.Append(RomanNumeral.NumberString(ref temp, 500, 'D'));
retval.Append(RomanNumeral.NumberString(ref temp, 100, 'C'));
retval.Append(RomanNumeral.NumberString(ref temp, 50, 'L'));
retval.Append(RomanNumeral.NumberString(ref temp, 10, 'X'));
retval.Append(RomanNumeral.NumberString(ref temp, 5, 'V'));
retval.Append(RomanNumeral.NumberString(ref temp, 1, 'I'));
return(retval.ToString());
}
private short value;
}
struct BinaryNumeral
{
public BinaryNumeral(int value)
{
this.value = value;
}
public static implicit operator BinaryNumeral(
int value)
{
BinaryNumeral retval = new BinaryNumeral(value);
return(retval);
}
public static implicit operator int(
BinaryNumeral binary)
{
return(binary.value);
}
public static implicit operator string(
BinaryNumeral binary)
{
StringBuilder retval = new StringBuilder();
return(retval.ToString());
}
private int value;
}
public class ConversionsBetweenStructs1
{
public static void Main()
{
RomanNumeral roman = new RomanNumeral(12);
BinaryNumeral binary;
binary = (BinaryNumeral)(int)roman;
}
}
Conversions Between Structs 1
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 24 - User-Defined ConversionsConversions Between Structs
// copyright 2000 Eric Gunnerson
using System;
using System.Text;
struct RomanNumeral
{
public RomanNumeral(short value)
{
if (value > 5000)
throw(new ArgumentOutOfRangeException());
this.value = value;
}
public static explicit operator RomanNumeral(
short value)
{
RomanNumeral retval;
retval = new RomanNumeral(value);
return(retval);
}
public static implicit operator short(
RomanNumeral roman)
{
return(roman.value);
}
static string NumberString(
ref int value, int magnitude, char letter)
{
StringBuilder numberString = new StringBuilder();
while (value >= magnitude)
{
value -= magnitude;
numberString.Append(letter);
}
return(numberString.ToString());
}
public static implicit operator string(
RomanNumeral roman)
{
int temp = roman.value;
StringBuilder retval = new StringBuilder();
retval.Append(RomanNumeral.NumberString(ref temp, 1000, 'M'));
retval.Append(RomanNumeral.NumberString(ref temp, 500, 'D'));
retval.Append(RomanNumeral.NumberString(ref temp, 100, 'C'));
retval.Append(RomanNumeral.NumberString(ref temp, 50, 'L'));
retval.Append(RomanNumeral.NumberString(ref temp, 10, 'X'));
retval.Append(RomanNumeral.NumberString(ref temp, 5, 'V'));
retval.Append(RomanNumeral.NumberString(ref temp, 1, 'I'));
return(retval.ToString());
}
public static implicit operator BinaryNumeral(RomanNumeral roman)
{
return(new BinaryNumeral((short) roman));
}
public static explicit operator RomanNumeral(
BinaryNumeral binary)
{
return(new RomanNumeral((short) binary));
}
private short value;
}
struct BinaryNumeral
{
public BinaryNumeral(int value)
{
this.value = value;
}
public static implicit operator BinaryNumeral(
int value)
{
BinaryNumeral retval = new BinaryNumeral(value);
return(retval);
}
public static implicit operator int(
BinaryNumeral binary)
{
return(binary.value);
}
public static implicit operator string(
BinaryNumeral binary)
{
StringBuilder retval = new StringBuilder();
return(retval.ToString());
}
private int value;
}
public class ConversionsConversionsBetweenStructs2
{
public static void Main()
{
RomanNumeral roman = new RomanNumeral(122);
BinaryNumeral binary;
binary = roman;
roman = (RomanNumeral) binary;
}
}
Structs (Value Types):Structs and Constructors
using System;
struct Point
{
int x;
int y;
Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return(String.Format("({0}, {1})", x, y));
}
}
public class StructsandConstructors
{
public static void Main()
{
Point[] points = new Point[5];
Console.WriteLine("[2] = {0}", points[2]);
}
}
Structs (Value Types):A Point Struct
using System;
struct Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return(String.Format("({0}, {1})", x, y));
}
public int x;
public int y;
}
public class APointStruct
{
public static void Main()
{
Point start = new Point(5, 5);
Console.WriteLine("Start: {0}", start);
}
}