/*
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;
}
}
Month: February 2011
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);
}
}
Calling a Function with a Structure Parameter
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 31 - InteropCalling Native DLL FunctionsCalling a Function with a Structure Parameter
// copyright 2000 Eric Gunnerson
using System;
using System.Runtime.InteropServices;
struct Point
{
public int x;
public int y;
public override string ToString()
{
return(String.Format("({0}, {1})", x, y));
}
}
struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
public override string ToString()
{
return(String.Format("({0}, {1})
({2}, {3})", left, top, right, bottom));
}
}
struct WindowPlacement
{
public uint length;
public uint flags;
public uint showCmd;
public Point minPosition;
public Point maxPosition;
public Rect normalPosition;
public override string ToString()
{
return(String.Format("min, max, normal:
{0}
{1}
{2}",
minPosition, maxPosition, normalPosition));
}
}
public class CallingaFunctionwithaStructureParameterWindow
{
[DllImport("user32")]
static extern int GetForegroundWindow();
[DllImport("user32")]
static extern bool GetWindowPlacement(int handle, ref WindowPlacement wp);
public static void Main()
{
int window = GetForegroundWindow();
WindowPlacement wp = new WindowPlacement();
wp.length = (uint) Marshal.SizeOf(wp);
bool result = GetWindowPlacement(window, ref wp);
if (result)
{
Console.WriteLine(wp);
}
}
}
C# always creates a structure instance as a value-type variable even using the new operator
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// ValType.cs -- Demonstrates that C# always creates a structure instance as
// a value-type variable even using the new operator.
// Compile this program using the following command line:
// C:>csc ValType.cs
//
namespace nsValType
{
using System;
public struct POINT
{
public int cx;
public int cy;
}
public class ValType
{
static public void Main()
{
POINT point1;
point1.cx = 42;
point1.cy = 56;
ModifyPoint (point1);
Console.WriteLine ("In Main() point2 = ({0}, {1})", point1.cx, point1.cy);
POINT point2 = new POINT ();
// point2.cx = 42;
// point2.cy = 56;
Console.WriteLine ();
ModifyPoint (point2);
Console.WriteLine ("In Main() point2 = ({0}, {1})", point2.cx, point2.cy);
}
static public void ModifyPoint (POINT pt)
{
pt.cx *= 2;
pt.cy *= 2;
Console.WriteLine ("In ModifyPoint() pt = ({0}, {1})", pt.cx, pt.cy);
}
}
}
Illustrates the use of a struct
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_15.cs illustrates the use of a struct
*/
// declare the Rectangle struct
struct Rectangle
{
// declare the fields
public int Width;
public int Height;
// define a constructor
public Rectangle(int Width, int Height)
{
this.Width = Width;
this.Height = Height;
}
// define the Area() method
public int Area()
{
return Width * Height;
}
}
public class Example5_15
{
public static void Main()
{
// create an instance of a Rectangle
System.Console.WriteLine("Creating a Rectangle instance");
Rectangle myRectangle = new Rectangle(2, 3);
// display the values for the Rectangle instance
System.Console.WriteLine("myRectangle.Width = " + myRectangle.Width);
System.Console.WriteLine("myRectangle.Height = " + myRectangle.Height);
// call the Area() method of the Rectangle instance
System.Console.WriteLine("myRectangle.Area() = " + myRectangle.Area());
}
}
Issue an error message if you do not initialize all of the fields in a structure
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Struct.cs - Issue an error message if you do not initialize all of
// the fields in a structure
//
// Compile this program with the following command line:
// C:>csc Struct.cs
//
using System;
namespace nsStruct
{
struct POINT
{
public int cx;
public int cy;
public int var;
public override string ToString ()
{
return ("(" + cx + ", " + cy + ")");
}
}
public class StructDemo2
{
static public void Main ()
{
POINT pt;
pt.cx = 24;
pt.cy = 42;
Console.WriteLine (pt);
// Console.WriteLine ("(" + pt.cx + ", " + pt.cy + ")");
}
}
}

