Monday, July 18, 2011

Monday 7/18/2011


#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace ContinueBreak
{
class ContinueBreak
{
static void Main(string[] args)
{
string signal = "0"; //initialize to neutral
while(signal!="X") // X indicates stop
{
Console.Write("Enter a signal: ");
signal = Console.ReadLine();

//do some work here, no matter what signal you receive
Console.WriteLine("Received: {0}", signal);

if(signal=="A")
{
//faulty - abort signal processing
//log the problem and abort
Console.WriteLine("Fault! Abort\n");
break;
}
if(signal=="O")
{
//normal traffic condition
//log and continue on
Console.WriteLine("All is well.\n");
continue;
}
//problem. Take action and then log the problem
//and then continue on
Console.WriteLine("{0} -- raise alarm!\n", signal);
}
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace DivisionModulus
{
class DivisionModulus
{
static void Main(string[] args)
{
int i1, i2;
float f1, f2;
double d1, d2;
decimal dec1, dec2;

i1 = 17;
i2 = 4;
f1 = 17f;
f2 = 4f;
d1 = 17;
d2 = 4;
dec1 = 17;
dec2 = 4;

Console.WriteLine("Integer:\t{0}\n float:\t\t{1}", i1 / i2, f1 / f2);
Console.WriteLine("Double:\t\t{0}\n decimal:\t{1}", d1 / d2, dec1 / dec2);
Console.WriteLine("\n Modulus:\t{0}", i1 % i2);
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace PrefixPostfix
{
class PrefixPostfix
{
static void Main(string[] args)
{
int valueOne = 10;
int valueTwo;
valueTwo = valueOne++;
Console.WriteLine("After postfix: {0}, {1}", valueOne, valueTwo);
valueOne = 20;
valueTwo = ++valueOne;
Console.WriteLine("After prefix: {0}, {1}", valueOne, valueTwo);
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion


namespace TimeClass
{
public class Time
{
//private variables
int Year;
int Month;
int Date;
int Hour;
int Minute;
int Second;

//public methods
public void DisplayCurrentTime()
{
Console.WriteLine("stub for DisplayCurrentTime");
}
}
public class Tester
{
static void Main()
{
Time t = new Time();
t.DisplayCurrentTime();
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace PassingValues
{
public class MyClass
{
public void SomeMethod(int firstParam, float secondParam)
{
Console.WriteLine("Here are the parameters received: {0}, {1}", firstParam, secondParam);
}
}

public class Tester
{
static void Main()
{
int howManyPeople = 5;
float pi = 3.14f;
MyClass mc = new MyClass();
mc.SomeMethod(howManyPeople, pi);
}
}
}

In this example, the constructor takes a DateTime object and initializes all the member variables based on values in that object.


#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace DeclaringConstructor
{
public class Time
{
//private member variables
int Year;
int Month;
int Date;
int Hour;
int Minute;
int Second;
//public accessor methods
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second);
}

//constructor
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
}

public class Tester
{
static void Main()
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace Initializer
{
public class Time
{
//private member variable
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second = 30; //initialzier

//public accessor methods
public void DisplayCurrentTime()
{
System.DateTime now = System.DateTime.Now;
System.Console.WriteLine("\nDebug\t:{0}/{1}/{2} {3}:{4}:{5}", now.Month, now.Day, now.Year, now.Hour, now.Minute, now.Second);

System.Console.WriteLine("Time\t: {0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second);
}

//constructors
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second; //explicit assignment
}


public Time(int Year, int Month, int Date, int Hour, int Minute)
{
this.Year = Year;
this.Month = Month;
this.Date = Date;
this.Hour = Hour;
this.Minute = Minute;
}
}

public class Tester
{
static void Main()
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();

Time t2 = new Time(2007, 11, 18, 11, 45);
t2.DisplayCurrentTime();
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace StaticFields
{
public class Cat
{
//if you want to initialize a static member
//you must provide an explicit initializer
private static int instances = 0;

public Cat()
{
instances++;
}

public static void HowManyCats()
{
Console.WriteLine("{0} cats adopted", instances);
}
}

public class tester
{
static void Main()
{
Cat.HowManyCats();
Cat frisky = new Cat();
Cat.HowManyCats();
Cat whiskers = new Cat();
Cat.HowManyCats();
}
}
}




namespace usingStatement
{
class Tester
{
public static void Main()
{
using (Font theFont = new Font("Arial", 10.0f))
{
//use theFont
Console.WriteLine("using Arial");
} //compiler will call Dispose on theFont

Font anotherFont = new Font("Courier", 12.0f);
using (anotherFont)
{
//use another font
Console.WriteLine("using Courier");
}
}
}
}




#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace ReturningValuesInParams
{
public class Time
{
//private member variables
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;

//public accessor methods
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second);
}

public int GetHour()
{
return Hour;
}

public void GetTime(ref int h, ref int m, ref int s)
{
h = Hour;
m = Minute;
s = Second;
}

//constructor
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
}

public class Tester
{
static void Main()
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();

int theHour = 0;
int theMinute = 0;
int theSecond = 0;
t.GetTime(ref theHour, ref theMinute, ref theSecond);
System.Console.WriteLine("Current time: {0}:{1}:{2}", theHour, theMinute, theSecond);
}
}
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class program
{
static void Main(string[] args)
{
Console.WriteLine("{0} command line arguments were specified. ", args.Length);
foreach (string arg in args)
{
Console.WriteLine(arg);
}
}
}

No comments: