Tuesday, December 20, 2011

Tuesday 12.20.11

class Program
{
static void Main()
{
int employeeID = 303;
object boxedID = employeeID;

employeeID = 404;
int unboxedID = (int)boxedID;

System.Console.WriteLine(employeeID.ToString());
System.Console.WriteLine(unboxedID.ToString());
}
}


class Program
{
static void Main()
{
String derivedObj = "Dummy";
Object baseObj1 = new Object();
Object baseObj2 = derivedObj;

Console.WriteLine("baseObj2 {0} String", baseObj2 is String ? "is" : "isnot");
Console.WriteLine("baseObj1 {0} String", baseObj1 is String ? "is" : "isnot");
Console.WriteLine("derivedObj {0} Object", derivedObj is Object ? "is" : "isnot");

int j = 123;
object boxed = j;
object obj = new Object();

Console.WriteLine("boxed {0} int", boxed is int ? "is" : "isnot");
Console.WriteLine("obj {0} int", obj is int ? "is" : "isnot");
Console.WriteLine("boxed {0} System.ValueType", boxed is ValueType ? "is" : "isnot");
}
}
}


static void Main()
{
DerivedType derivedObj = new DerivedType();
BaseType baseObj1 = new BaseType();
BaseType baseObj2 = derivedObj;

DerivedType derivedObj2 = baseObj2 as DerivedType;
if (derivedObj2 != null)
{
Console.WriteLine("Conversion Succeeded");
} else
{
Console.WriteLine("Conversion Failed");
}

//fails
derivedObj2 = baseObj1 as DerivedType;
if (derivedObj2 != null)
{
Console.WriteLine("Conversion Succeeded");
}
else
{
Console.WriteLine("Conversion Failed");
}

BaseType baseObj3 = derivedObj as BaseType;
if (baseObj3 != null)
{
Console.WriteLine("Conversion Succeeded");
}
else
{
Console.WriteLine("Conversion Failed");
}
}



class Program
{
static void Main()
{
Collection<int> numbers = new Collection();
numbers.Add(42);
numbers.Add(409);

Collection<string> strings = new Collection();
strings.Add("Joe");
strings.Add("Bob");

Collection<Collection> colNumbers = new Collection<Collection>();
colNumbers.Add(numbers);
IList<int> theNumbers = numbers;
foreach (int i in theNumbers)
{
Console.WriteLine(i);
}
}
}


public class A
{
public A()
{
this.y = 456;
SetField(ref this.y);

}

private void SetField(ref int val)
{
val = 888;
}

public readonly int x = 123;
public readonly int y;
public const int z = 555;

}
class Program
{
static void Main()
{
A obj = new A();
System.Console.WriteLine("x = {0}, y = {1}, z = {2}", obj.x, obj.y, A.z);
}
}


namespace IComparer
{
public class Employee : IComparable
{
private int empID;
private int yearsOfSvc = 1;

public Employee(int empID)
{
this.empID = empID;
}

public Employee(int empID, int yearsOfSvc)
{
this.empID = empID;
this.yearsOfSvc = yearsOfSvc;
}

public override string ToString()
{
return "ID: " + empID.ToString() + ". Years of Svc: " + yearsOfSvc.ToString();
}

public bool Equals(Employee other)
{
if (this.empID == other.empID)
{
return true;
}
else
{
return false;
}
}

//static method to get a comparer object
public static EmployeeComparer GetComparer()
{
return new Employee.EmployeeComparer();
}

//comparer delegates back to Employee
//Employee uses the integer's default
//compareto method

public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(rhs.empID);
}

//special implementation to be called by custom comparer
public int CompareTo(Employee rhs, Employee.EmployeeComparer.ComparisonType which)
{
switch (which)
{
case Employee.EmployeeComparer.ComparisonType.EmpID:
return this.empID.CompareTo(rhs.empID);
case Employee.EmployeeComparer.ComparisonType.Yrs:
return this.yearsOfSvc.CompareTo(rhs.yearsOfSvc);
}
return 0;
}

//nested class which implements IComparer
public class EmployeeComparer : IComparer
{
//enumeration of comparison types
public enum ComparisonType
{
EmpID,
Yrs
};
public bool Equals(Employee lhs, Employee rhs)
{
return this.Compare(lhs, rhs) == 0;
}

public int GetHashCode(Employee e)
{
return e.GetHashCode();
}

//tell the employee objects to compare themselves
public int Compare(Employee lhs, Employee rhs)
{
return lhs.CompareTo(rhs, WhichComparison);
}
public Employee.EmployeeComparer.ComparisonType
WhichComparison { get; set; }
}

class Program
{
static void Main()
{
List empArray = new List();
//generate random numbers for both the integers and the employee IDs
Random r = new Random();

//populate the array
for (int i = 0; i < 5; i++)
{
//add a random employee id
empArray.Add(new Employee(r.Next(10) + 100, r.Next(20)));
}

//diplsay all the contents of the emploee array
for (int i = 0; i < empArray.Count; i++)
{
Console.Write("\n{0} ", empArray[i].ToString());
}
Console.WriteLine("\n");

//sort and display the employee array
Employee.EmployeeComparer c = Employee.GetComparer();
c.WhichComparison = Employee.EmployeeComparer.ComparisonType.EmpID;
empArray.Sort(c);

//display all the contents of the Employee array
for (int i = 0; i < empArray.Count; i++)
{
Console.Write("\n{0}", empArray[i].ToString());
}
Console.WriteLine("\n");

c.WhichComparison = Employee.EmployeeComparer.ComparisonType.Yrs;
empArray.Sort(c);

for (int i = 0; i < empArray.Count; i++)
{
Console.WriteLine("\n{0} ", empArray[i].ToString());
}
Console.WriteLine("\n");
}
}
}
}


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

namespace Queue
{
class Program
{
static void Main()
{
Queue intQueue = new Queue();

//populate the array
for (int i = 0; i < 5; i++)
{
intQueue.Enqueue(i * 5);
}

//display the Queue
Console.Write("intQueue values:\t");
PrintValues(intQueue);


//remove an element from the queue
Console.WriteLine("\n(Dequeue)\t{0}", intQueue.Dequeue());

//display the queue
Console.WriteLine("intQueue values:\t");
PrintValues(intQueue);

//remove another element from the queue
Console.WriteLine("\n(Dequeue)\t{0}", intQueue.Dequeue());

//display the queue
Console.WriteLine("intQueue values:\t");
PrintValues(intQueue);

//view the first element in the Queue but do not remove
Console.WriteLine("\n(Peek) \t{0}", intQueue.Peek());

//display the queue
Console.WriteLine("intQueue values:\t");
PrintValues(intQueue);
}

public static void PrintValues(IEnumerable myCollection)
{
IEnumerator myEnumerator = myCollection.GetEnumerator();
while (myEnumerator.MoveNext())
Console.WriteLine("{0} ", myEnumerator.Current);
Console.WriteLine();
}
}
}


namespace Stack
{
class Program
{
static void Main()
{
Stack intStack = new Stack();

//populate the array
for (int i = 0; i < 8; i++)
{
intStack.Push(i * 5);
}

//display the stack
Console.Write("intStack values:\t");
PrintValues(intStack);

//remove an element from the stack
Console.WriteLine("\n(Pop)\t{0}", intStack.Pop());

//display the stack
Console.Write("intStack values:\t");
PrintValues(intStack);

//Remove another element from the stack
Console.WriteLine("\n(Pop)\t{0}", intStack.Pop());

//display the stack
Console.Write("intStack values:\t");
PrintValues(intStack);

//view the first element in the stack
//but do not remove
Console.WriteLine("\n(Peek) \t{0}", intStack.Peek());

//display the stack
Console.Write("intStack values:\t");
PrintValues(intStack);

//declare an array object which will hold 12 integers
int[] targetArray = new int[12];

for (int i = 0; i < targetArray.Length; i++)
{
targetArray[i] = i * 100 + 100;
}

//display the values of the target array instance
Console.WriteLine("\nTarget Array: ");
PrintValues(targetArray);

//copy the entire source Stack to the
//target array instance, starting at index 6
intStack.CopyTo(targetArray, 6);

//display the values fo the target Array instance
Console.WriteLine("\nTarget array after copy: ");
PrintValues(targetArray);
}

public static void PrintValues(IEnumerable myCollection)
{
IEnumerator enumerator = myCollection.GetEnumerator();
while (enumerator.MoveNext())
Console.Write("{0} ", enumerator.Current);
Console.WriteLine();
}
}
}


Module Program


Sub Main()
'create a TimeSpan representing 2.5 days
Dim timespan1 As New TimeSpan(2, 12, 0, 0)

'create a Timespan representing 4.5 days
Dim timespan2 As New TimeSpan(4, 12, 0, 0)

'create a timepsan representing 1 week
Dim oneweek As TimeSpan = timespan1 + timespan2

'create a datetime with the current date and time
Dim now As DateTime = DateTime.Now

'create a DateTime representing one week ago
Dim past As DateTime = now - oneweek

'create a DateTime reprsenting 1 week in the future
Dim future As DateTime = now + oneweek

'create a datetime representing the next day using the addDays method
Dim tomorrow As DateTime = now.AddDays(1)

'display the DateTime instances
Console.WriteLine("Now : {0}", now)
Console.WriteLine("Past : {0}", past)
Console.WriteLine("Future : {0}", future)
Console.WriteLine("Tomorrow : {0}", tomorrow)
Console.WriteLine(Environment.NewLine)

'create various DateTimeOffset objects using the same
'methods demonstrated above using the dateTime structure
Dim nowOffset As DateTimeOffset = DateTimeOffset.Now
Dim pastOffset As DateTimeOffset = nowOffset - oneweek
Dim futureOffset As DateTimeOffset = nowOffset + oneweek
Dim tomorrowOffset As DateTimeOffset = nowOffset.AddDays(1)

'change the offset used by nowOffset to -8 (which is pacific standard time)
Dim nowPST As DateTimeOffset = nowOffset.ToOffset(New TimeSpan(-8, 0, 0))

'display the DateTimeOffset instances
Console.WriteLine("Now (with offset) : {0}", nowOffset)
Console.WriteLine("Past (with offset) : {0}", pastOffset)
Console.WriteLine("Future (with offset) : {0}", futureOffset)
Console.WriteLine("Tomorrow (with offset) : {0}", tomorrowOffset)
Console.WriteLine(Environment.NewLine)
Console.WriteLine("now (with offset of -8) : {0}", nowPST)

'wait to continue
Console.WriteLine(vbCrLf & "Main method complete. Press Enter")
Console.ReadLine()



End Sub

End Module


Module Program


Sub Main()
'create a TimeZoneInfo object for the local time zone
Dim localTimeZone As TimeZoneInfo = TimeZoneInfo.Local
'create a TimeZoneInfo object for Coordinated Universal Time (UTC)
Dim utcTimeZone As TimeZoneInfo = TimeZoneInfo.Utc

'create a timezoneinfo object for Pacific Standard Time (PST)
Dim pstTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")

'create a DateTimeOffset that represents the current time
Dim currentTime As DateTimeOffset = DateTimeOffset.Now

'display the lcoal time and the local time zone
If localTimeZone.IsDaylightSavingTime(currentTime) Then
Console.WriteLine("Current time in the local time zone ({0}):", localTimeZone.DaylightName)
Else
Console.WriteLine("Current time in the local time zone ({0}):", localTimeZone.StandardName)
End If
Console.WriteLine("{0}", currentTime.ToString())
Console.WriteLine(Environment.NewLine)

'display the results of converting the current local time to coordinated universal time (UTC)
If utcTimeZone.IsDaylightSavingTime(currentTime) Then
Console.WriteLine("Current time in {0}:", utcTimeZone.DaylightName)
Else
Console.WriteLine("Current time in {0}:", utcTimeZone.StandardName)
End If
Console.WriteLine(" {0}", TimeZoneInfo.ConvertTime(currentTime, utcTimeZone))
Console.WriteLine(Environment.NewLine)

'create a DateTimeoffset object that represents the current local time converted
'to the PST time zone
Dim pstDTO As DateTimeOffset = TimeZoneInfo.ConvertTime(currentTime, pstTimeZone)

'display the results of the conversion
If pstTimeZone.IsDaylightSavingTime(currentTime) Then
Console.WriteLine("Current time in {0}:", pstTimeZone.DaylightName)
Else
Console.WriteLine("Current time in {0}:", pstTimeZone.StandardName)
End If
Console.WriteLine(" {0}", pstDTO.ToString())

'display the previous results converted to Coordinated Universal Time (UTC)
Console.WriteLine(" {0} (Converted to UTC)", TimeZoneInfo.ConvertTimeToUtc(pstDTO.DateTime, pstTimeZone))
Console.WriteLine(Environment.NewLine)

'create a DateTimeOffset that represent the current local time
'converted to Mountain Standard Time using the ConvertTimeBySystemTimeoneId method
'this conversion works but it is best to creat an actual TimeZoneInfo object so you have access
'to determine if it is daylight saving time or not
Dim mstDTO As DateTimeOffset = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, "Mountain Standard Time")

'display the results of the conversion
Console.WriteLine("Current time in Mountain Standard Time:")
Console.WriteLine(" {0}", mstDTO.ToString())
Console.WriteLine(Environment.NewLine)

'wait to continue
Console.WriteLine(vbCrLf & "Main method complete. Press Enter")
Console.ReadLine()
End Sub

End Module


Sub Main()
'create a new array and populate it
Dim array1 As Integer() = {4, 2, 9, 3}

'sort the array
Array.Sort(array1)

'display the contents of the sorted array
For Each i As Integer In array1
Console.WriteLine(i.ToString)
Next

'create a new ArrayList and populate it
Dim list1 As New ArrayList(4)
list1.Add("Amy")
list1.Add("Alaina")
list1.Add("Aidan")
list1.Add("Anna")

'sort the array list
list1.Sort()

'display the contents of the sorted array list
For Each s As String In list1
Console.WriteLine(s)
Next

'wait to continue
Console.ReadLine()

End Sub

No comments: