home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 06 interfaces and delegates / interfacesdemo / module1.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  7.5 KB  |  219 lines

  1.  
  2. Module MainModule
  3.  
  4.     Sub Main()
  5.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  6.  
  7.         'TestInterface()
  8.         'TestInterfaceOnStructure()
  9.         'TestIComparable()
  10.         'TestIComparer()
  11.         'TestIComparer2()
  12.         'TestICloneable()
  13.         'TestICloneable2()
  14.         'TestICloneable3()
  15.         'TestICloneable4()
  16.         'TestIEnumerable()
  17.         'TestGetEnumerator()
  18.  
  19.         ' You need these statements when running inside Visual Studio, so that
  20.         ' the Console window doesn't disappear
  21.         Console.WriteLine("")
  22.         Console.WriteLine(">>> press Enter to terminate the program <<<")
  23.         Console.ReadLine()
  24.     End Sub
  25.  
  26.     ' the following procedure accesses an interface of a class
  27.  
  28.     Sub TestInterface()
  29.         ' An instance of the class
  30.         Dim addin As New MyAddin()
  31.         ' Cast to an interface variable.
  32.         Dim iplug As IPluggableAddin = addin
  33.         ' Now you can access all the methods and properties in the interface.
  34.         iplug.State = True
  35.  
  36.         ' Cast to the interface type and invoke a method in one operation.
  37.         DirectCast(addin, IPluggableAddin).OnConnection("MyHost")
  38.  
  39.         ' Create a hidden, temporary interface variable.
  40.         With DirectCast(addin, IPluggableAddin)
  41.             .OnConnection("MyHost")
  42.             .State = True
  43.         End With
  44.  
  45.         Dim addin2 As New AnotherAddin()
  46.         ' Prove that the AnotherAddin inherited class exposes the IPluggableAddin interface.
  47.         DirectCast(addin2, IPluggableAddin).State = True
  48.  
  49.         ' The Dispose method in the derived class invokes the MyAddin.Dispose method
  50.         addin2.Dispose()
  51.     End Sub
  52.  
  53.     ' Test interfaces on structures
  54.  
  55.     Sub TestInterfaceOnStructure()
  56.         ' this code proves that value types are boxed when an interface method is called
  57.         Dim struc As MyStruct
  58.  
  59.         ' Get the object reference from the structure
  60.         Dim o1 As Object = CType(struc, IGetObjectRef).GetObjectRef
  61.         ' Do the same once again
  62.         Dim o2 As Object = CType(struc, IGetObjectRef).GetObjectRef
  63.  
  64.         ' prove that the object has been boxed twice to different areas in the heap
  65.         Console.WriteLine(o1 Is o2)                    ' => False
  66.     End Sub
  67.  
  68.     ' This procedure demonstrates the IComparable interface
  69.  
  70.     Sub TestIComparable()
  71.         Dim Persons() As Person = {New Person("John", "Smith"), _
  72.             New Person("Robert", "Doe"), New Person("Joe", "Doe")}
  73.         Array.Sort(Persons)
  74.  
  75.         ' Print all the elements in sorted order.
  76.         Dim p As Person
  77.         For Each p In Persons
  78.             Console.WriteLine(p.ReverseName)
  79.         Next
  80.     End Sub
  81.  
  82.     ' this procedure demonstrates how you can use the IComparer interface
  83.  
  84.     Sub TestIComparer()
  85.         Dim Persons() As Person2 = {New Person2("John", "Smith"), _
  86.             New Person2("Robert", "Doe"), New Person2("Joe", "Doe")}
  87.  
  88.         ' Sort the array on name.
  89.         Array.Sort(Persons, New Person2.ComparerByName())
  90.         ' another way to reach the same result using a shared method
  91.         Array.Sort(Persons, Person2.CompareByName)
  92.  
  93.         ' Print all the elements in sorted order.
  94.         Console.WriteLine("Elements sorted on name:")
  95.         Dim p As Person2
  96.         For Each p In Persons
  97.             Console.WriteLine(p.CompleteName)
  98.         Next
  99.  
  100.         ' Sort the array on reversed name.
  101.         Array.Sort(Persons, New Person2.ComparerByReverseName())
  102.         ' another way to reach the same result using a shared method
  103.         Array.Sort(Persons, Person2.CompareByReverseName)
  104.  
  105.         Console.WriteLine("Elements sorted on reverse name:")
  106.         For Each p In Persons
  107.             Console.WriteLine(p.ReverseName)
  108.         Next
  109.     End Sub
  110.  
  111.     ' this procedure tests sorts in case sensitive way
  112.  
  113.     Sub TestIComparer2()
  114.         Dim arr() As String = {"aaa", "AAA", "Abc", "ABC", "abc", "aBC", "aaα", "αbc"}
  115.         Dim s As String
  116.  
  117.         Array.Sort(arr)
  118.         Console.WriteLine("--- Standard sort")
  119.         For Each s In arr
  120.             Console.WriteLine(s)
  121.         Next
  122.  
  123.         Array.Sort(arr, CaseInsensitiveComparer.Default)
  124.         Console.WriteLine("--- Sort using CaseInsensitiveComparer.Default")
  125.         For Each s In arr
  126.             Console.WriteLine(s)
  127.         Next
  128.  
  129.         Array.Sort(arr, New CaseInsensitiveComparerVB6())
  130.         Console.WriteLine("--- Sort using CaseInsensitiveComparerVB6")
  131.         For Each s In arr
  132.             Console.WriteLine(s)
  133.         Next
  134.     End Sub
  135.  
  136.     ' this procedure demonstrates the ICloneable interface
  137.  
  138.     Sub TestICloneable()
  139.         ' Define an employee and his boss.
  140.         Dim joe As New Employee("Joe", "Doe")
  141.         Dim robert As New Employee("Robert", "Smith")
  142.         joe.Boss = robert
  143.  
  144.         ' Clone it û The Clone method returns an object, 
  145.         ' thus you need DirectCast if Option Strict is On.
  146.         Dim joe2 As Employee = DirectCast(joe.Clone, Employee)
  147.  
  148.         ' prove that all properties were copied
  149.         Console.WriteLine(joe2.FirstName & " " & joe2.LastName _
  150.             & ", whose boss is " & joe2.Boss.FirstName & " " & joe2.Boss.LastName)
  151.         ' => Cloned object: Joe Doe, whose boss is Robert Smith
  152.     End Sub
  153.  
  154.     ' this procedure proves that Clone does shallow copy
  155.  
  156.     Sub TestICloneable2()
  157.         ' Define an employee and its boss
  158.         Dim joe As New Employee("Joe", "Doe")
  159.         Dim robert As New Employee("Robert", "Smith")
  160.         joe.Boss = robert
  161.         ' Clone it 
  162.         Dim joe2 As Employee = CType(joe.Clone, Employee)
  163.         ' Prove that the Employee object was cloned, but his boss wasn't.
  164.         Console.WriteLine(joe Is joe2)                   ' => False
  165.         Console.WriteLine(joe.Boss Is joe2.Boss)         ' => True
  166.     End Sub
  167.  
  168.     ' this procedure shows a deep copy clone method
  169.  
  170.     Sub TestICloneable3()
  171.         ' Define an employee and its boss
  172.         Dim joe As New Employee2("Joe", "Doe")
  173.         Dim robert As New Employee2("Robert", "Smith")
  174.         joe.Boss = robert
  175.         ' Clone it 
  176.         Dim joe2 As Employee2 = CType(joe.Clone, Employee2)
  177.         ' Prove that we clone the entire object graph.
  178.         Console.WriteLine(joe Is joe2)                   ' => False
  179.         Console.WriteLine(joe.Boss Is joe2.Boss)         ' => False
  180.     End Sub
  181.  
  182.     ' this procedure tests a strong-typed Clone method
  183.  
  184.     Sub TestICloneable4()
  185.         ' Define an employee and its boss
  186.         Dim joe As New Employee3("Joe", "Doe")
  187.         Dim robert As New Employee3("Robert", "Smith")
  188.         joe.Boss = robert
  189.         ' Clone it - note that no unboxing occurs here.
  190.         Dim joe2 As Employee3 = joe.Clone
  191.         ' Prove that we clone the entire object graph.
  192.         Console.WriteLine(joe Is joe2)                   ' => False
  193.         Console.WriteLine(joe.Boss Is joe2.Boss)         ' => False
  194.     End Sub
  195.  
  196.     ' this procedure demonstrates the IEnumerable and IEnumerator interfaces.
  197.  
  198.     Sub TestIEnumerable()
  199.         Dim msg As String = "Please, split this into individual words!"
  200.         Dim o As Object
  201.         For Each o In New WordParser(msg)
  202.             Console.WriteLine(o)
  203.         Next
  204.     End Sub
  205.  
  206.     ' this procedure tests direct interaction with the GetEnumerator function
  207.  
  208.     Sub TestGetEnumerator()
  209.         Dim f As System.IO.FileInfo
  210.  
  211.         ' enumerate all files in that directory tree
  212.         For Each f In New FileTree("C:\Docs")
  213.             Console.WriteLine(f.FullName)
  214.         Next
  215.     End Sub
  216.  
  217. End Module
  218.  
  219.