/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example16_3b.cs uses a class from Example16_3a.cs
*/
using System;
using StringSwitch; // name space define in Example16_3c.cs
public class Example16_3b
{
public static void Main()
{
string localString;
MySwitch s = new MySwitch();
s.inString="abcdef";
s.upper(out localString);
Console.WriteLine(localString);
}
}
//===========================================================
/*
Example16_3c.cs provides manifest information for Example 16_3
*/
using System.Reflection;
[assembly: AssemblyTitle("Example 16.3")]
[assembly: AssemblyVersion("1.0.0.0")]
//===========================================================
/*
Example16_3a.cs creates a namespace with a single class
*/
using System;
namespace StringSwitch
{
class MySwitch
{
string privateString;
public string inString
{
get
{
return privateString;
}
set
{
privateString = value;
}
}
public void upper(out string upperString)
{
upperString = privateString.ToUpper();
}
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
Return an object
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Return an object.
using System;
class Rect {
int width;
int height;
public Rect(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
public void show() {
Console.WriteLine(width + " " + height);
}
/* Return a rectangle that is a specified
factor larger than the invoking rectangle. */
public Rect enlarge(int factor) {
return new Rect(width * factor, height * factor);
}
}
public class RetObj {
public static void Main() {
Rect r1 = new Rect(4, 5);
Console.Write("Dimensions of r1: ");
r1.show();
Console.WriteLine("Area of r1: " + r1.area());
Console.WriteLine();
// create a rectange that is twice as big as r1
Rect r2 = r1.enlarge(2);
Console.Write("Dimensions of r2: ");
r2.show();
Console.WriteLine("Area of r2 " + r2.area());
}
}
This program creates two Building objects
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// This program creates two Building objects.
using System;
class Building {
public int floors; // number of floors
public int area; // total square footage of building
public int occupants; // number of occupants
}
// This class declares two objects of type Building.
public class BuildingDemo1 {
public static void Main() {
Building house = new Building();
Building office = new Building();
int areaPP; // area per person
// assign values to fields in house
house.occupants = 4;
house.area = 2500;
house.floors = 2;
// assign values to fields in office
office.occupants = 25;
office.area = 4200;
office.floors = 3;
// compute the area per person in house
areaPP = house.area / house.occupants;
Console.WriteLine("house has:
" +
house.floors + " floors
" +
house.occupants + " occupants
" +
house.area + " total area
" +
areaPP + " area per person");
Console.WriteLine();
// compute the area per person in office
areaPP = office.area / office.occupants;
Console.WriteLine("office has:
" +
office.floors + " floors
" +
office.occupants + " occupants
" +
office.area + " total area
" +
areaPP + " area per person");
}
}
A program that uses the Building class
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A program that uses the Building class.
using System;
class Building {
public int floors; // number of floors
public int area; // total square footage of building
public int occupants; // number of occupants
}
// This class declares an object of type Building.
public class BuildingDemo {
public static void Main() {
Building house = new Building(); // create a Building object
int areaPP; // area per person
// assign values to fields in house
house.occupants = 4;
house.area = 2500;
house.floors = 2;
// compute the area per person
areaPP = house.area / house.occupants;
Console.WriteLine("house has:
" +
house.floors + " floors
" +
house.occupants + " occupants
" +
house.area + " total area
" +
areaPP + " area per person");
}
}
Create class
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class MyTime
{
// private variables
private int year;
private int month;
private int date;
private int hour;
private int minute;
private int second;
// public methods
public void DisplayCurrentMyTime()
{
Console.WriteLine(
"stub for DisplayCurrentMyTime");
}
}
public class Tester
{
static void Main()
{
MyTime timeObject = new MyTime();
timeObject.DisplayCurrentMyTime();
}
}
Variable in and out a class
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace heap
{
public class Dog
{
public int weight;
}
public class TesterClass
{
public void Run()
{
// create an integer
int firstInt = 5;
// create a second integer
int secondInt = firstInt;
// display the two integers
Console.WriteLine("firstInt: {0} secondInt: {1}",
firstInt, secondInt);
// modify the second integer
secondInt = 7;
// display the two integers
Console.WriteLine("firstInt: {0} secondInt: {1}",
firstInt, secondInt);
// create a dog
Dog milo = new Dog();
// assign a value to weight
milo.weight = 5;
// create a second reference to the dog
Dog fido = milo;
// display their values
Console.WriteLine("Milo: {0}, fido: {1}",
milo.weight, fido.weight);
// assign a new weight to the second reference
fido.weight = 7;
// display the two values
Console.WriteLine("Milo: {0}, fido: {1}",
milo.weight, fido.weight);
}
static void Main()
{
TesterClass t = new TesterClass();
t.Run();
}
}
}
Illustrates hiding
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_4.cs illustrates hiding
*/
// declare the Car class
class Car
{
public int yearBuilt;
public double maximumSpeed;
public int Age(int currentYear)
{
int maximumSpeed = 100; // hides the field
System.Console.WriteLine("In Age(): maximumSpeed = " +
maximumSpeed);
int age = currentYear - yearBuilt;
return age;
}
public double Distance(double initialSpeed, double time)
{
System.Console.WriteLine("In Distance(): maximumSpeed = " +
maximumSpeed);
return (initialSpeed + maximumSpeed) / 2 * time;
}
}
public class Example5_4
{
public static void Main()
{
// create a Car object
Car redPorsche = new Car();
redPorsche.yearBuilt = 2000;
redPorsche.maximumSpeed = 150;
int age = redPorsche.Age(2001);
System.Console.WriteLine("redPorsche is " + age + " year old.");
System.Console.WriteLine("redPorsche travels " +
redPorsche.Distance(31, .25) + " miles.");
}
}