/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace Test_Console_App_3 { // declare a Cat class // stripped down class Cat { // a private static member to keep // track of how many Cat objects have // been created private static int instances = 0; private int weight; private String name; // cat constructor // increments the count of Cats public Cat(String name, int weight) { instances++; this.name = name; this.weight = weight; } // Static method to retrieve // the current number of Cats public static void HowManyCats() { Console.WriteLine("{0} cats adopted", instances); } public void TellWeight() { Console.WriteLine("{0} is {1} pounds", name, weight); } } public class StaticInClassTester { public void Run() { Cat.HowManyCats(); Cat frisky = new Cat("Frisky", 5); frisky.TellWeight(); Cat.HowManyCats(); Cat whiskers = new Cat("Whisky", 7); whiskers.TellWeight(); Cat.HowManyCats(); } static void Main() { StaticInClassTester t = new StaticInClassTester(); t.Run(); } } }
Month: February 2011
Class variables with default value
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class MyTimeWithDefaultValue { // private member variables int year; int month; int date; int hour; int minute; int second = 30; // public method public void DisplayCurrentMyTimeWithDefaultValue() { System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", month, date, year, hour, minute, second); } // constructor public MyTimeWithDefaultValue(int theYear, int theMonth, int theDate, int theHour, int theMinute) { year = theYear; month = theMonth; date = theDate; hour = theHour; minute = theMinute; } } public class Tester { static void Main() { MyTimeWithDefaultValue timeObject = new MyTimeWithDefaultValue(2005,3,25,9,35); timeObject.DisplayCurrentMyTimeWithDefaultValue(); } }
A Simple Class and Objects
public class Product { public string make; public string model; public string color; public int yearBuilt; public void Start() { System.Console.WriteLine(model + " started"); } public void Stop() { System.Console.WriteLine(model + " stopped"); } } class MainClass{ public static void Main() { Product myProduct; myProduct = new Product(); myProduct.make = "Toyota"; myProduct.model = "MR2"; myProduct.color = "black"; myProduct.yearBuilt = 1995; System.Console.WriteLine("myProduct details:"); System.Console.WriteLine("myProduct.make = " + myProduct.make); System.Console.WriteLine("myProduct.model = " + myProduct.model); System.Console.WriteLine("myProduct.color = " + myProduct.color); System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt); myProduct.Start(); myProduct.Stop(); Product redPorsche = new Product(); redPorsche.make = "Porsche"; redPorsche.model = "Boxster"; redPorsche.color = "red"; redPorsche.yearBuilt = 2000; System.Console.WriteLine( "redPorsche is a " + redPorsche.model); System.Console.WriteLine("Assigning redPorsche to myProduct"); myProduct = redPorsche; System.Console.WriteLine("myProduct details:"); System.Console.WriteLine("myProduct.make = " + myProduct.make); System.Console.WriteLine("myProduct.model = " + myProduct.model); System.Console.WriteLine("myProduct.color = " + myProduct.color); System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt); } }
Field Attributes
using System; using System.Reflection; public enum RegHives { HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG } public class RegKeyAttribute : Attribute { public RegKeyAttribute(RegHives Hive, String ValueName) { this.Hive = Hive; this.ValueName = ValueName; } protected RegHives hive; public RegHives Hive { get { return hive; } set { hive = value; } } protected String valueName; public String ValueName { get { return valueName; } set { valueName = value; } } } class SomeClass { [RegKey(RegHives.HKEY_CURRENT_USER, "Foo")] public int Foo; public int Bar; } class Test { [STAThread] static void Main(string[] args) { Type type = Type.GetType("FieldAttribs.SomeClass"); foreach (FieldInfo field in type.GetFields()) { foreach (Attribute attr in field.GetCustomAttributes(true)) { RegKeyAttribute rka = attr as RegKeyAttribute; if (null != rka) { Console.WriteLine("{0} will be saved in {1}{2}", field.Name, rka.Hive, rka.ValueName); } } } } }
The super-string class.
using System;
public class MyString {
private string fString;
public MyString() {
fString = “”;
}
public MyString(string inStr) {
fString = inStr;
}
public string ToStr() {
return fString;
}
public string Right(int nChars) {
if (nChars > fString.Length)
return fString;
string s = “”;
for (int i = fString.Length – nChars; i < fString.Length; ++i)
s += fString[i];
return s;
}
public string Left(int nChars) {
if (nChars > fString.Length)
return fString;
string s = “”;
for (int i = 0; i < nChars; ++i)
s += fString[i];
return s;
}
public string Mid(int nStart, int nEnd) {
if (nStart < 0 || nEnd > fString.Length)
return fString;
if (nStart > nEnd)
return “”;
string s = “”;
for (int i = nStart; i < nEnd; ++i)
s += fString[i];
return s;
}
}
class Class1 {
static void Main(string[] args) {
MyString s = new MyString("Hello world");
System.Console.WriteLine("s = {0}", s.ToStr());
System.Console.WriteLine("Right 3 = [{0}]", s.Right(3));
System.Console.WriteLine("Left 6 = [{0}]", s.Left(6));
System.Console.WriteLine("Mid 2,4 = [{0}]", s.Mid(2, 4));
}
}
[/csharp]
change field value in a method
using System; public class Foo { public int i; } class RefTest2App { public static void ChangeValue(Foo f) { f.i = 42; } static void Main(string[] args) { Foo test = new Foo(); test.i = 6; Console.WriteLine("BEFORE METHOD CALL"); Console.WriteLine("test.i={0}", test.i); Console.WriteLine(); ChangeValue(test); Console.WriteLine("AFTER METHOD CALL"); Console.WriteLine("test.i={0}", test.i); } }
C# Classes Member Functions
using System; public class MemberFunctions { public static void Main() { Point myPoint = new Point(10, 15); Console.WriteLine("myPoint.X {0}", myPoint.GetX()); Console.WriteLine("myPoint.Y {0}", myPoint.GetY()); } } class Point { public Point(int x, int y) { this.x = x; this.y = y; } // accessor functions public int GetX() {return(x);} public int GetY() {return(y);} // variables now private int x; int y; }