Tuesday, November 22, 2011

Tuesday 11.22.11

Module Module1

Sub Main()
ExplicitTyping()
ImplicitTypingInForEach()
Console.ReadLine()
End Sub

Public Sub ExplicitTyping()
'local variables are declared as follows
'Dim variableName as DataType = initialValue
Dim myInt As Integer = 0
Dim myBool As Boolean = True
Dim myString As String = "Time, marches on..."
End Sub

Public Sub ImplicitTypingInForEach()
Dim evenNumbers() = New Integer() {2, 4, 6, 8, 10}
'use implicit typing in a standard For Each loop
'here, 'item' is really a System.Int32
For Each item In evenNumbers
Console.WriteLine("Item value: {0}", item)
Next
End Sub

End Module


Module Module1

Sub Main()
Console.WriteLine("***** Fun with Extension Methods *****" & vbLf)

'the integer has assumed a new identity
Dim myInt As Integer = 123456789
myInt.DisplayDefiningAssembly()

'so has the DataSet!
Dim d = New System.Data.DataSet()
d.DisplayDefiningAssembly()

'and the soundplayer (show details)
Dim sp As New System.Media.SoundPlayer()
sp.DisplayDefiningAssembly(True)

'use new Integer functionality
Console.WriteLine(vbLf & "Value of myInt: {0}", myInt)
Console.WriteLine("Reversed digits of myInt: {0}", myInt.ReverseDigits())

Console.ReadLine()

End Sub

End Module

Imports System.Reflection
Imports System.Runtime.CompilerServices


Module MyExtensions

'this method allows any object to display the assembly
'it is defined in
_
Public Sub DisplayDefiningAssembly(ByVal obj As Object)
Console.WriteLine("{0} lives here: {1}", obj.GetType().Name, obj.GetType().Assembly)
End Sub

'this method allows any integer to reverse its digits
'for example, 56 would return 65
_
Public Function ReverseDigits(ByVal i As Integer) As Integer
'translate integer into a string, and then
'get all the characters
Dim digits() As Char = i.ToString().ToCharArray()

'now reverse items in teh array
Array.Reverse(digits)

'put back into string
Dim newDigits As String = New String(digits)

'finally return the modified string back as an integer
Return Integer.Parse(newDigits)

End Function

'overloading extension methods
_
Public Sub DisplayDefiningAssembly(ByVal obj As Object, ByVal showDetails As Boolean)
Console.WriteLine("Defining Assembly: {0}", obj.GetType().Assembly)
If showDetails Then
Console.WriteLine("Name of type: {0}", obj.GetType().Name)
Console.WriteLine("Parent of type: {0}", obj.GetType().BaseType)
Console.WriteLine("Is generic?: {0}", obj.GetType().IsGenericType)

End If
End Subjavascript:void(0)

End Module


Module Module1

Sub Main()
QueryOverStrings()
Console.WriteLine()
QueryOverInts()
Console.WriteLine()
ImmediateExecution()
End Sub


Sub QueryOverStrings()
'assume we have an array o fstrings
'some of whcih have more than 6 letters
Dim currentVideoGames As String() = {"Morrowind", "BioShock", "Half Life 2: Episode 1", "The Darkness", "Daxter", "System Shock 2"}

'build a LINQ query
Dim subset As IEnumerable(Of String) = From g In currentVideoGames _
Where g.Length > 6 Order By g Select g

'print out results
For Each s As String In subset
Console.WriteLine("Item: {0}", s)
Next

ReflectOverQueryResults(subset)
Console.ReadLine()
End Sub

Sub QueryOverInts()
Dim numbers() As Integer = {10, 20, 30, 40, 1, 2, 3, 8, 11}
'only print items less than 10
'note use of implicit typing
Dim subset = From i In numbers Where i < 10 Select i

'more implicit typing
For Each i In subset
Console.WriteLine("Item: {0} < 10", i)
Next

'change some data in the array
numbers(0) = 4
Console.WriteLine()

'evaluate again
For Each i In subset
Console.WriteLine("{0} < 10 ", i)
Next
ReflectOverQueryResults(subset)
Console.ReadLine()
End Sub


Sub ImmediateExecution()
Dim numbers() As Integer = {10, 20, 30, 40, 1, 2, 3, 8, 9}
'get data right now as integer()
Dim subsetAsIntArray() As Integer = (From i In numbers Where i < 10 Select i).ToArray()

'get data right now as list(of integer)
Dim subsetAsListOfInts As List(Of Integer) = (From i In numbers Where i < 10 Select i).ToList()

For Each l In subsetAsListOfInts
Console.WriteLine("[{0}]", l)
Next

Console.ReadLine()
End Sub

Sub ReflectOverQueryResults(ByVal resultSet As Object)
Console.WriteLine("*** Info about Query ***")
Console.WriteLine("resultSet is of type: {0}", resultSet.GetType().Name)
Console.WriteLine("resultset location: {0}", resultSet.GetType().Assembly)
Console.WriteLine()
End Sub
End Module


Public Class Car
Public PetName As String = String.Empty
Public Color As String = String.Empty
Public speed As Integer
Public Make As String = String.Empty
End Class

Module Module1

Sub Main()
Console.WriteLine("**** More Fun with LINQ Expressions ****")
'make a list of car objects
Dim myCars As New List(Of Car)()
myCars.Add(New Car With {.PetName = "Henry", .Color = "Silver", .speed = 100, .Make = "BMW"})
myCars.Add(New Car With {.PetName = "Daisy", .Color = "Tan", .speed = 90, .Make = "BMW"})
myCars.Add(New Car With {.PetName = "Mary", .Color = "Black", .speed = 55, .Make = "VW"})
myCars.Add(New Car With {.PetName = "Clunker", .Color = "Rust", .speed = 5, .Make = "Yugo"})
myCars.Add(New Car With {.PetName = "Melvin", .Color = "White", .speed = 43, .Make = "Ford"})
GetFastCars(myCars)
GetFastBMWs(myCars)
ExtractNumericalData()
Console.ReadLine()


End Sub

Sub ExtractNumericalData()
'extract the integers from ArrayList
Dim myStuff As New ArrayList()
myStuff.AddRange(New Object() {10, 400, 8, False, New Car(), "string data"})
Dim myInts As IEnumerable(Of Integer) = myStuff.OfType(Of Integer)()

'print out 10, 400, and 8
For Each i As Integer In myInts
Console.WriteLine("Int value: {0}", i)
Next
End Sub

Sub GetFastCars(ByVal myCars As List(Of Car))
'create a query expression
Dim fastCars = From c In myCars Where c.speed > 55 Select c

For Each Car In fastCars
Console.WriteLine("{0} is going too fast!", Car.PetName)
Next
End Sub

Sub GetFastBMWs(ByVal myCars As List(Of Car))
'find all items where speed is greater than 90
'and make is BMW
Dim fastCars = From c In myCars Where c.speed > 90 And c.Make = "BMW" Select c

For Each Car In fastCars
Console.WriteLine("{0} is going too fast!", Car.PetName)
Next
End Sub
End Module


Module Module1

Sub Main()
QueryStringArrayWithOperators()
QueryStringWithEnumerableAndLambdas()
VeryComplexQueryExpression.QueryStringsWithRawDelegates()
GetCount()
Console.ReadLine()
End Sub


Sub QueryStringArrayWithOperators()
Console.WriteLine("***** Using LINQ Query Operators *****")

Dim currentVideoGames As String() = {"Morrowind", "Bioshock", "Half Life 2: Episode 1", "The Darkness", "Daxter", "System Shock 2"}

'build a LINQ query with VB LINQ operators
Dim subset = From g In currentVideoGames Where g.Length > 6 Order By g Select g

For Each s As String In subset
Console.WriteLine("Item: {0}", s)
Next
Console.WriteLine()
End Sub

Sub QueryStringWithEnumerableAndLambdas()
Console.WriteLine("***** Using Enumerable / Lambda Expressions *****")
Dim currentVideoGames As String() = {"Morrowind", "Bioshock", "Half LIfe 2: Episode 1", "The Darkness", "Daxter", "System Shock 2"}

'build a query expression using extension methods
'granted to the array via the Enumerable type
Dim subset = currentVideoGames.Where(Function(game) game.Length > 6).OrderBy(Function(game) game).Select(Function(game) game)

'print out the results
For Each game In subset
Console.WriteLine("Item: {0}", game)
Next
Console.WriteLine()

End Sub

Sub GetCount()
Dim currentVideoGames() As String = {"Morrowind", "Bioshock", "Half Life 2: Episode 1", "The Darkness", "Daxter", "System Shock 2"}
'get count from the query
Dim num As Integer = (From g In currentVideoGames Where g.Length > 6 Order By g Select g).Count()

'numb is the value 5
Console.WriteLine("{0} items honor the LINQ query.", num)


End Sub
End Module

No comments: