Wednesday, July 20, 2011

Wednesday, 7/20/2011


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

namespace InOutRef
{
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 SetTime(int hr, out int min, ref int sec)
{
//if the passed in time is >= 30
//increment the minute and set second to 0
//otherwise leave both alone
if (sec >= 0)
{
Minute++;
Second = 0;
}
Hour = hr; //set to value passed in
//pas the minute and second back out
min = Minute;
sec = 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 = 3;
int theMinute;
int theSecond = 20;

t.SetTime(theHour, out theMinute, ref theSecond);
System.Console.WriteLine("the Minute is now: {0} and {1} seconds", theMinute, theSecond);

theSecond = 40;
t.SetTime(theHour, out theMinute, ref theSecond);
System.Console.WriteLine("the Minute is now: {0} and {1} seconds", theMinute, theSecond);
}
}
}




//the signature of a method is defined by its name and its parameter list.
//Two methods differ in their signatures if they have different names or different parameter lists.

//A class can have any number of methods, as long as each one's signature differs from that of all the others.

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


namespace OverloadedConstructor
{
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);
}

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

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

public class tester
{
static void Main()
{
System.DateTime currentTime = System.DateTime.Now;

Time t1 = new Time(currentTime);
t1.DisplayCurrentTime();

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




//by decoupling the class state from the method that accesses that state, the designer is free to change the internal state of the object as needed
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion

namespace UsingAProperty
{
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("Time\t: {0}/{1}/{2} {3}:{4}:{5}", month, date, year, hour, minute, second);
}

public Time(System.DateTime dt)
{
year = dt.Year;
month = dt.Month;
date = dt.Day;
hour = dt.Hour;
minute = dt.Minute;
second = dt.Second;
}

//create a property
//this creates two accessors: get and set
public int Hour
{
get
{
return hour;
}

set
{
hour = value;
}
}
}

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

int theHour = t.Hour;
System.Console.WriteLine("\nRetrieved the hour: {0}\n", theHour);
theHour++;
t.Hour = theHour;
System.Console.WriteLine("Updated the hour: {0}\n", theHour);
}
}
}




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

namespace StaticPublicConstants
{
public class RightNow
{
//public member variables
public static readonly int Year;
public static readonly int Month;
public static readonly int Date;
public static readonly int Hour;
public static readonly int Minute;
public static readonly int Second;


static RightNow()
{
System.DateTime dt = System.DateTime.Now;
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.Console.WriteLine("This year:{0}", RightNow.Year.ToString());
}
}
}

No comments: