home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_ValueEnum_sln________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-05-01  |  9.7 KB  |  241 lines

  1. '/*=====================================================================
  2. '  File:      ValueEnum.vb
  3. '  Summary:   Demonstrates things you can do with ValueType/Enum types.
  4. '---------------------------------------------------------------------
  5. '  This file is part of the Microsoft .NET Framework SDK Code Samples.
  6. '  Copyright (C) Microsoft Corporation.  All rights reserved.
  7. 'This source code is intended only as a supplement to Microsoft
  8. 'Development Tools and/or on-line documentation.  See these other
  9. 'materials for detailed information regarding Microsoft code samples.
  10. 'THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  11. 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  12. 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  13. 'PARTICULAR PURPOSE.
  14. '=====================================================================*/
  15.  
  16. Option Explicit On 
  17. Option Strict On
  18.  
  19.  
  20. ' Add the classes in the following namespaces to our namespace
  21. Imports System
  22. Imports System.ValueType
  23. Imports System.Enum
  24. Imports System.Object
  25. Imports Microsoft.VisualBasic.Strings
  26.  
  27. Public Module App
  28.     ' "Main" is application's entry point
  29.     Sub Main()
  30.         DemoValueTypes()
  31.         DemoReferenceTypes()
  32.         DemoEnums()
  33.         DemoFlags()        
  34.     End Sub
  35.  
  36.     'This is a value type because of "structure"
  37.     Structure Point
  38.         Dim x, y As Integer
  39.                 
  40.         Sub New(ByVal x As Integer, ByVal y As Integer)
  41.             Me.x = x
  42.             Me.y = y
  43.         End Sub
  44.  
  45.  
  46.         Overrides Public Function ToString() As String  
  47.           ToString = "(" & x & "," & y & ")"
  48.         End Function
  49.         
  50.     End Structure
  51.  
  52.     Private Sub DemoValueTypes()
  53.         Console.WriteLine("Demo start: Demo of value types.")
  54.  
  55.         Dim p1 As Point = New Point(5, 10)
  56.         Dim p2 As Point = New Point(5, 10)
  57.         Dim p3 As Point = New Point(3, 4)
  58.  
  59.         ' What type is this valuetype & what is it derived from
  60.         Console.WriteLine("   The " & (p1.GetType().ToString) & " type is derived from " & (p1.GetType().BaseType.ToString))
  61.  
  62.         ' Value types compare for equality by comparing the fields
  63.         Console.WriteLine("   Does p1 equal p1: " & p1.Equals(p1)) ' True
  64.         Console.WriteLine("   Does p1 equal p2: " & p1.Equals(p2)) ' True
  65.         Console.WriteLine("   Does p1 equal p3: " & p1.Equals(p3)) ' False
  66.         Console.WriteLine("   p1=" & p1.ToString & ", p3=" & p3.ToString())        
  67.  
  68.         Console.WriteLine("Demo stop: Demo of value types.")
  69.  
  70.     End Sub
  71.  
  72.     Class Rectangle
  73.         Dim x, y, width, height As Integer
  74.  
  75.         Sub New(ByRef x As Integer, ByRef y As Integer, ByRef width As Integer, ByRef height As Integer)
  76.             Me.x = x
  77.             Me.y = y
  78.             Me.width = width
  79.             Me.height = height
  80.         End Sub
  81.  
  82.         Overrides Public Function ToString() As String
  83.             ToString = "(" & x & "," & y & ")x(" & width & "," & height & ")"
  84.         End Function
  85.  
  86.         Public Overloads Overrides Function Equals(ByVal o As Object) As Boolean
  87.             ' Change the symantics of this reference type so that it is
  88.             ' equal to the same type of object if the fields are equal.
  89.             Console.WriteLine("   In Rectangle.Equals method")
  90.             Dim r As Rectangle = CType(o, Rectangle)
  91.  
  92.             If (r.x = x And r.y = y And r.width = width And r.height = height) Then
  93.                 Equals = True
  94.             Else
  95.                 Equals = False
  96.             End If
  97.  
  98.         End Function
  99.  
  100.     End Class
  101.  
  102.     Private Sub DemoReferenceTypes()
  103.         
  104.         console.WriteLine()
  105.         console.WriteLine()
  106.         Console.WriteLine("Demo start: Demo of reference types.")
  107.         Dim r As Rectangle = New Rectangle(1, 2, 3, 4)
  108.   
  109.         ' What type is this reference type & what is it derived from
  110.         Console.WriteLine("   The " & r.GetType().ToString & " type is derived from " & r.GetType().BaseType.ToString)
  111.         Console.WriteLine("   " & r.ToString)
  112.         
  113.         ' Reference types are equal if they refer to the same object
  114.         Console.WriteLine("   Is r equivalent to (1, 2, 3, 4): " & (r Is New Rectangle(1, 2, 3, 4))) ' False
  115.         Console.WriteLine("   Is r equal to (1, 2, 3, 4): " & (r.Equals(New Rectangle(1, 2, 3, 4)))) ' True
  116.         Console.WriteLine("   Is r equivalent to (1, 1, 1, 1): " & (r Is New Rectangle(1, 1, 1, 1))) ' False
  117.         Console.WriteLine("   Is r equal to (1, 1, 1, 1): " & (r.Equals(New Rectangle(1, 1, 1, 1)))) ' False
  118.  
  119.         Console.WriteLine("Demo stop: Demo of reference types.")
  120.     End Sub
  121.  
  122.     ' This is an enumerated type because of 'enum'
  123.     Enum Color
  124.         Red = 111
  125.         Green = 222
  126.         Blue = 333
  127.     End Enum
  128.  
  129.     Private Sub DemoEnums()
  130.         
  131.         Console.WriteLine()
  132.         Console.Writeline()
  133.         Console.WriteLine("Demo start: Demo of enumerated types.")
  134.         Dim c As Color = CType(Color.Red, Color)
  135.  
  136.         ' What type is this enum & what is it derived from
  137.         Console.WriteLine("   The " + c.GetType().ToString + " type is derived from " + c.GetType().BaseType.ToString)
  138.  
  139.         ' What is the underlying type used for the Enum's value
  140.         Console.WriteLine("   Underlying type: " + GetUnderlyingType(c.GetType()).ToString)
  141.  
  142.         ' Display the set of legal enum values
  143.         Dim o() As Color = CType(GetValues(c.GetType()), Color())
  144.         Console.WriteLine()
  145.         Console.WriteLine("   Number of valid enum values: " & o.length)
  146.         
  147.         Dim x As Integer
  148.         For x = 0 To (o.Length - 1)
  149.             Dim cc As Color = CType(o(x), Color)
  150.             Console.WriteLine("   " & x & ": Name=" & chr(9) & cc.ToString() & Chr(9) & "     Number=" & System.Enum.Format(GetType(Color), cc, "d"))
  151.         Next
  152.        
  153.         ' Check if a value is legal for this enum
  154.         Console.WriteLine()
  155.         Console.WriteLine("   111 is a valid enum value: " & IsDefined(c.GetType(), 111)) ' True
  156.         Console.WriteLine("   112 is a valid enum value: " & IsDefined(c.GetType(), 112)) ' False
  157.  
  158.         ' Check if two enums are equal
  159.         console.WriteLine()
  160.         Console.WriteLine("   Is c equal to Red: " & (c.Equals(Color.Red))) ' True
  161.         Console.WriteLine("   Is c equal to Blue: " & (c.Equals(Color.Blue))) ' False
  162.  
  163.         ' Display the enum's value as a string
  164.         console.WriteLine()
  165.         Console.WriteLine("   c's value as a string: " & c.ToString()) ' Red
  166.         Console.WriteLine("   c's value as a number: " & System.Enum.Format(GetType(Color), c, "d")) ' 111
  167.  
  168.         ' Convert a string to an enum's value
  169.         c = CType(Parse(GetType(Color), "Blue"), color)
  170.         Try
  171.             c = CType(Parse(GetType(Color), "NotAColor"), Color) ' Not valid, raises exception        
  172.         Catch 
  173.             Console.WriteLine("   'NotAColor' is not a valid value for this enum.")
  174.         End Try
  175.  
  176.         ' Display the enum's value as a string
  177.         console.WriteLine()
  178.         Console.WriteLine("   c's value as a string: " & c.ToString()) ' Blue
  179.         Console.WriteLine("   c's value as a number: " & System.Enum.Format(GetType(Color), c, "d")) ' 333
  180.  
  181.         Console.WriteLine("Demo stop: Demo of enumerated types.")
  182.     End Sub
  183.  
  184.     
  185.     <Flags()> _
  186.     Enum ActionAttributes
  187.         Read = 1
  188.         Write = 2
  189.         Delete = 4
  190.         Query = 8
  191.         Sync = 16
  192.     End Enum
  193.  
  194.     Private Sub DemoFlags()
  195.         Console.WriteLine()
  196.         Console.WriteLine()
  197.         Console.WriteLine("Demo start: Demo of enumerated flags types.")
  198.  
  199.         Dim aa As ActionAttributes = CType((ActionAttributes.Read Or ActionAttributes.Write Or ActionAttributes.Query), ActionAttributes)
  200.  
  201.         ' What type is this enum & what is it derived from
  202.         Console.WriteLine("   The " & aa.GetType.ToString & " type is derived from " & aa.GetType.BaseType.ToString)
  203.  
  204.         ' What is the underlying type used for the Enum's value
  205.         Console.WriteLine("   Underlying type: " & GetUnderlyingType(aa.GetType()).ToString)
  206.  
  207.         ' Display the set of legal enum values
  208.         Dim o() As ActionAttributes = CType(GetValues(aa.GetType()), ActionAttributes())
  209.         Console.WriteLine()
  210.         Console.WriteLine("   Number of valid enum values: " & o.Length)
  211.  
  212.         Dim x As Integer
  213.         For x = 0 To (o.Length - 1)
  214.             Dim aax As ActionAttributes = CType(o(x), ActionAttributes)
  215.             Console.WriteLine("   " & x & ": Name=" & Chr(9) & aax.ToString() & Chr(9) & Chr(9) & "Number=" & System.Enum.Format(GetType(ActionAttributes), aax, "d"))
  216.         Next
  217.  
  218.         ' Check if a value is legal for this enum
  219.         Console.WriteLine()
  220.         Console.WriteLine("   8 is a valid enum value: " & IsDefined(aa.GetType(), 8)) ' True
  221.         Console.WriteLine("   6 is a valid enum value: " & IsDefined(aa.GetType(), 6)) ' False
  222.  
  223.         ' Display the enum's value as a string
  224.         Console.WriteLine()
  225.         Console.WriteLine("   aa's value as a string: " & aa.ToString()) ' Read|Write|Query
  226.         Console.WriteLine("   aa's value as a number: " & System.Enum.Format(GetType(ActionAttributes), aa, "d")) ' 11
  227.  
  228.         ' Convert a string to an enum's value
  229.         aa = CType(Parse(GetType(ActionAttributes), "Write"), ActionAttributes)
  230.         aa = aa Or CType(Parse(GetType(ActionAttributes), "Sync"), ActionAttributes)
  231.         Console.WriteLine()
  232.         Console.WriteLine("   aa's value as a string: " & aa.ToString()) ' Write|Sync
  233.         Console.WriteLine("   aa's value as a number: " & System.Enum.Format(GetType(ActionAttributes), aa, "d")) ' 18
  234.  
  235.         Console.WriteLine("Demo stop: Demo of enumerated flags types.")
  236.     End Sub
  237.  
  238. End Module
  239.  
  240. 'End of File
  241.