Tuesday, November 15, 2011

Tuesday 11.15.11

Module Module1

Sub Main()
Console.WriteLine("***** Simple Exception Example *****")
Console.WriteLine("=> Creating a car and stepping on it!")
Dim myCar As New Car("Zippy", 20)
myCar.CrankTunes(True)

Try
For i As Integer = 0 To 10
myCar.Accelerate(10)
Next
Catch ex As Exception
Console.WriteLine("*** Error! ***")
Console.WriteLine("Method: {0}", ex.TargetSite)
Console.WriteLine("Class defining member: {0}", ex.TargetSite.DeclaringType)
Console.WriteLine("Help Link: {0}", ex.HelpLink)
Console.WriteLine("Member type: {0}", ex.TargetSite.MemberType)
Console.WriteLine("Message: {0}", ex.Message)
Console.WriteLine("Source: {0}", ex.Source)
Console.WriteLine("-> Custom Data:")
If (ex.Data IsNot Nothing) Then
For Each de As DictionaryEntry In ex.Data
Console.WriteLine("-> {0} : {1}", de.Key, de.Value)
Next
End If
End Try

Console.ReadLine()
End Sub

End Module



Sub Main()

'the class type System.GC allows you to programatically interact with the
'garbage collector

Console.WriteLine("***** Fun with System.GC *****")
'print out estimated number of bytes on heap
Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(False))

'MaxGeneration is zero based, so add 1 for display purposes
Console.WriteLine("This OS has {0} object generations.", (GC.MaxGeneration + 1))

Dim refToMyCar As New Car("Zippy", 100)
Console.WriteLine(refToMyCar.ToString())

'print out generation of refToMyCar object
Console.WriteLine("Generation of refToMyCar is: {0}", GC.GetGeneration(refToMyCar))
Console.ReadLine()

'make a ton of objects for testing purposes
Dim tonsOfObjects(5000) As Object
For i As Integer = 0 To UBound(tonsOfObjects)
tonsOfObjects(i) = New Object()
Next

'collect only gen 0 objects
GC.Collect(0)
GC.WaitForPendingFinalizers()

'print out generation of refToMyCar
Console.WriteLine("Generation of refToMyCar is: {0}", GC.GetGeneration(refToMyCar))

'see if tonsOfObjects(4000) is still alive
If (tonsOfObjects(4000) IsNot Nothing) Then
Console.WriteLine("Generation of tonsOfObjects(4000) is: {0}", GC.GetGeneration(tonsOfObjects(4000)))
Else
Console.WriteLine("tonsOfObjects(4000) is no longer alive.")
End If

'print out how many times a generation has been swept
Console.WriteLine("Gen 0 has been swept {0} times", GC.CollectionCount(0))
Console.WriteLine("Gen 2 has been swept {0} times", GC.CollectionCount(1))
Console.WriteLine("Gen 3 has been swept {0} times", GC.CollectionCount(2))
Console.ReadLine()

End Sub



Sub Main()
ArrayListTest()

End Sub

Sub ArrayListTest()
'make ArrayList and add a range of cars
Dim carArList As New ArrayList()
carArList.AddRange(New Car() {New Car("Fred", 90), New Car("Mary", 100), New Car("MB", 190)})
Console.WriteLine("items in carArList: {0}", carArList.Count)

'iterate over contents using For/Each
For Each c As Car In carArList
Console.WriteLine("Car pet name: {0}", c.name)
Next

'insert new car
Console.WriteLine("-> Inserting new Car.")
carArList.Insert(2, New Car("TheNewCar", 0))
Console.WriteLine("Items in carArlist: {0}", carArList.Count)

'get the subobjects as an array
Dim arrayOfCars As Object() = carArList.ToArray()
Dim i As Integer = 0

'now iterate over array using While loop/Length property
While i < arrayOfCars.Length
Console.WriteLine("Car pet name: {0}", CType(arrayOfCars(i), Car).name)
i = i + 1
End While
Console.ReadLine()

End Sub


Public Sub WashCar(ByVal c As Car)
Console.WriteLine("Cleaning {0}", c.name)
End Sub

Sub QueueTest()
'make a Q with three items
Dim carWashQ As New Queue()
carWashQ.Enqueue(New Car("FirstCar", 0))
carWashQ.Enqueue(New Car("SecondCar", 1))
carWashQ.Enqueue(New Car("ThirdCar", 3))

'peek at first car in queue
Console.WriteLine("First in Q is {0}", CType(carWashQ.Peek(), Car).name)
'remove each item from Q
WashCar(CType(carWashQ.Dequeue(), Car))
WashCar(CType(carWashQ.Dequeue(), Car))
WashCar(CType(carWashQ.Dequeue(), Car))

'try to d-Q again?
Try
WashCar(CType(carWashQ.Dequeue(), Car))
Catch ex As Exception
Console.WriteLine("Error!! {0}", ex.Message)
End Try
End Sub


Sub Main()
Dim myPeople As New PeopleCollection()
myPeople.AddPerson(New Person("Homer", "Simpson", 40))
myPeople.AddPerson(New Person("Marge", "Simpson", 38))
myPeople.AddPerson(New Person("Lisa", "Simpson", 7))
myPeople.AddPerson(New Person("Bart", "Simpson", 9))
myPeople.AddPerson(New Person("Maggie", "Simpson", 2))

For Each p As Person In myPeople
Console.WriteLine(p)
Next

myPeople(5) = New Person("Waylon", "Smithers", 47)
Console.WriteLine("Person #5 is {0}", myPeople(5))

Console.ReadLine()
End Sub

Public Class PeopleCollection
Implements IEnumerable

Private arPeople As New ArrayList()

Public Function GetPerson(ByVal pos As Integer) As Person
Return CType(arPeople(pos), Person)
End Function

Public Sub AddPerson(ByVal p As Person)
arPeople.Add(p)
End Sub

Public Sub ClearPeople()
arPeople.Clear()
End Sub

Public ReadOnly Property Count() As Integer
Get
Return arPeople.Count
End Get
End Property

'the 'indexer' of this custom collection
Default Public Property Item(ByVal p As Integer) As Person
Get
Return CType(arPeople(p), Person)
End Get
Set(ByVal value As Person)
arPeople.Insert(p, value)
End Set
End Property

Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return arPeople.GetEnumerator
End Function
End Class

Public Class Person
'Made public for simplicity
Public currAge As Integer
Public fName As String
Public lName As String

'constructors
Public Sub New()

End Sub

Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal age As String)
currAge = age
fName = firstName
lName = lastName
End Sub

Public Overrides Function ToString() As String
Return String.Format("{0}, {1} is {2} years old.", lName, fName, currAge)
End Function
End Class


Sub Main()
Console.WriteLine("***** Fun with Nullable Data ******" & vbLf)
Dim dr As New DatabaseReader()

'get integer from 'database'
Dim i As Nullable(Of Integer) = dr.GetIntFromDatabase()
If (i.HasValue) Then
Console.WriteLine("Value of 'i' is: {0}", i.Value)
Else
Console.WriteLine("Value of 'i' is undefined.")
End If

'get Boolean from "database"
Dim b As Nullable(Of Boolean) = dr.GetBoolFromDatabase()
If (b.HasValue) Then
Console.WriteLine("Value of 'b' is: {0}", b.Value)
Else
Console.WriteLine("Value of 'b' is undefined.")
End If
Console.ReadLine()
End Sub

Public Class DatabaseReader
'nullable data fields
Public numericValue As Nullable(Of Integer) = Nothing
Public boolValue As Nullable(Of Boolean) = True

'note the nullable return type
Public Function GetIntFromDatabase() As Nullable(Of Integer)
Return numericValue
End Function

'note the nullable return type

Public Function GetBoolFromDatabase() As Nullable(Of Boolean)
Return boolValue
End Function
End Class

No comments: