public enum Element { Hydrogen = 1, Helium, Lithium, Beryllium, Boron, Carbon, Nitrogen, Oxygen, Fluorine, Neon } // End of enumeration of elements. |
private void btn11_Click(object sender, EventArgs e) { Element thing; thing = Element.Fluorine; if (thing == Element.Fluorine) { MessageBox.Show("You found Fluorine!"); } // End of if block for checking fluorine. } // End of btn 11's click. |
public void AtomicInformation(out int AtomicNumber, out float AtomicWeight, out string ChemSymbol, Element el) { AtomicNumber = ( int )el; switch (el) { case Element.Hydrogen: AtomicWeight = 1.00794f; ChemSymbol = "H"; break; case Element.Fluorine: AtomicWeight = 18.9984032f; // This number is too big to be stored as a float. ChemSymbol = "F"; break; case Element.Helium: AtomicWeight = 4.002602f; ChemSymbol = "He"; break; default: AtomicWeight = 0f; ChemSymbol = "?"; break; } // End of switch (el) return; } // End of atomic information function. |
private void btn11_Click(object sender, EventArgs e) { Element elem; int protons; float atomweight; string symb; //Note: You cannot pass a property such as btn2.Text as an out parameter. //Thus, the string symb elem = Element.Helium; AtomicInformation(out protons, out atomweight, out symb, elem); MessageBox.Show(string.Format("{0}: {1} - Atomic Number {2}, Atomic Weight {3}.", elem, symb, protons, atomweight)); } // End of btn 11's click. |
public class TestParallelogram { private Point[] vertices; // Upper left, upper right, lower right, lower left. public TestParallelogram(int left, int top, int basewidth, int baseheight, int offset, bool ishorizontal) { vertices = new Point[4]; if (ishorizontal) { // The top and bottom sides of the parallelogram are flat, left and right sides are sloped if (offset > 0) { // Upper left corner to the right of lower left corner. vertices = new Point[] {new Point(left+offset, top), new Point(left+basewidth+offset, top), new Point(left+basewidth, top+baseheight), new Point(left, top+baseheight)}; // Make the vertices. } else { // Upper left corner farther left than lower left corner. vertices = new Point[] {new Point(left, top), new Point(left+basewidth, top), new Point(left-offset+basewidth, top+baseheight), new Point(left-offset, top+baseheight)}; } // End of offset's if block. } else { // The left and right sides of the parallelogram are flat, top and bottom sides are sloped. if (offset > 0) { // Upper left corner is below the upper right corner. vertices = new Point[] {new Point(left, top+offset), new Point(left+basewidth, top), new Point(left+basewidth,top+baseheight), new Point(left, top+baseheight+offset)}; } else { // Upper left corner is above the upper right corner. vertices = new Point[] {new Point(left, top+offset), new Point(left+basewidth, top), new Point(left+basewidth,top+baseheight), new Point(left, top+baseheight+offset)}; } // End of offset's if block. } // End of horizontal check if block. } // End of TestParallelogram constructor. public void Inflate(int addwidth, int addheight) { vertices[1].Offset(addwidth, 0); // Offset is a member of the Point structure which basically moves a point. vertices[2].Offset(addwidth, addheight); vertices[3].Offset(0, addheight); } // End of inflate subroutine. public float Area() { return (vertices[1].X - vertices[0].X) * (vertices[3].Y - vertices[0].Y); } // End of Area function. } // End of Area class. |
private void btn12_Click(object sender, EventArgs e) { TestParallelogram parry = new TestParallelogram(0, 0, 30, 20, 10, true); TestParallelogram isometric = parry; // isometric and parry 'refer' to the same parallelogram. MessageBox.Show(parry.Area().ToString()); // Area of first parallelogram should be 600. parry.Inflate(10, 10); // If I inflate parry's parallelogram... to 1200 area... MessageBox.Show(isometric.Area().ToString()); // The effects are reflected in isometric's parallelogram... // Because they are the same parallelogram. // The only parallelogram we actually have is the one instantiated. at the first line. parry = null; isometric = null; // Empty object references - always good practice. } |
public int Width { get { return (vertices[1].X - vertices[0].X); } // Get the width. set { vertices[1].X = vertices[0].X + value; vertices[2].X = vertices[3].X + value; } // Set the width. } // End of width property. public int Height { get { return (vertices[3].Y - vertices[0].Y); } // Get the height. set { vertices[3].Y = vertices[0].Y + value; vertices[2].Y = vertices[1].Y + value; } // Set the Height. } // End of height property. public bool IsHorizontal { get { return (vertices[1].Y == vertices[0].Y); } // Tells if parallelogram is horizontal. } // End of readonly IsHorizontal? property. |
private void btn12_Click(object sender, EventArgs e) { TestParallelogram parry = new TestParallelogram(0, 0, 30, 20, 10, true); MessageBox.Show(parry.Area().ToString()); // Area of first parallelogram should be 600. parry.Inflate(10, 10); // If I inflate parry's parallelogram... to 1200 area... // Because they are the same parallelogram. // The only parallelogram we actually have is the one instantiated. at the first line. parry.Height = 20; parry.Width = 20; if (parry.IsHorizontal) { MessageBox.Show("Working with a \"horizontal\" parallelogram!"); } MessageBox.Show(parry.Area().ToString()); // The effects are reflected in isometric's parallelogram... parry = null; // Protip: Annul object references. } |
private TestParallelogram() { vertices = new Point[4]; } // Private constructor which creates an empty TestParallelogram public static TestParallelogram FromLTRB(int left, int top, int right, int bottom, int offset, bool horizontal) { TestParallelogram ret = new TestParallelogram(); if (horizontal) { if (offset > 0) { ret.vertices = new Point[] {new Point(left+offset, top), new Point(right+offset, top), new Point(right, bottom ), new Point(left, bottom )}; } else { ret.vertices = new Point[] {new Point(left, top), new Point(right, top), new Point(right-offset, bottom), new Point(left-offset, bottom )}; } // End of offset if block. } else { if (offset > 0) { ret.vertices = new Point[] {new Point(left, top+offset), new Point(right, top), new Point(right,bottom), new Point(left, bottom+offset)}; } else { ret.vertices = new Point[] {new Point(left, top), new Point(right, top-offset), new Point(right,bottom-offset), new Point(left, bottom)}; } // End of second offset if block. } return ret; } // End of static LTRB Testparallelogram creator. |
private void btn13_Click(object sender, EventArgs e) { TestParallelogram melee = TestParallelogram.FromLTRB(50, 50, 100, 100, 20, false); // Static method. melee.Inflate(15, 20); // Make parallelogram bigger MessageBox.Show(melee.Area().ToString()); // Show area. melee = null; //Protip: Annul object references. } // End of btn 13's click procedure. |
/// <summary> /// IGeometric provides assurance that the class will return its Area. /// </summary> public interface IGeometric { int Area(); } // End of IGeometric interface. |
private void btn13_Click(object sender, EventArgs e) { TestParallelogram melee = TestParallelogram.FromLTRB(50, 50, 100, 100, 20, false); // Static method. IGeometric areaprobe = melee; // Areaprobe is installed in melee parallelogram to return its area. melee.Inflate(15, 20); // Make parallelogram bigger MessageBox.Show(areaprobe.Area().ToString()); // Show area. areaprobe = null; //Protip: Annul interface references. melee = null; //Protip: Annul object references. } // End of btn 13's click procedure. |
public int Width { get { return (vertices[1].X - vertices[0].X); } // Get the width. set { if (value < 0) { throw new SystemException("Width cannot be less than zero."); } else { vertices[1].X = vertices[0].X + value; vertices[2].X = vertices[3].X + value; } // End of validation block } // Set the width. } // End of width property. public int Height { get { return (vertices[3].Y - vertices[0].Y); } // Get the height. set { if (value < 0) { throw new SystemException("Height cannot be less than zero."); } else { vertices[3].Y = vertices[0].Y + value; vertices[2].Y = vertices[1].Y + value; } } // Set the Height. } // End of height property. |
private void btn13_Click(object sender, EventArgs e) { TestParallelogram melee = TestParallelogram.FromLTRB(50, 50, 100, 100, 20, false); // Static method. melee.Width = ~0; // Not of 0 is -1. MessageBox.Show(melee.Area().ToString()); // Show area. melee = null; //Protip: Annul object references. } // End of btn 13's click procedure. |
private void btn13_Click(object sender, EventArgs e) { TestParallelogram melee = TestParallelogram.FromLTRB(50, 50, 100, 100, 20, false); // Static method. try { melee.Width = ~0; // Not of 0 is -1. MessageBox.Show(melee.Area().ToString()); // Show area. } catch { // Catches anything thrown out of the try block. MessageBox.Show("Your parallelogram width is too small."); } finally { melee = null; //Protip: Annul object references. } // End of the exception handler. } // End of btn 13's click procedure. |