home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / ValueEnum / ValueEnum.cs < prev   
Encoding:
Text File  |  2000-06-23  |  8.6 KB  |  203 lines

  1. /*=====================================================================
  2.   File:      ValueEnum.cs
  3.  
  4.   Summary:   Demonstrates things you can do with ValueType/Enum types.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft COM+ 2.0 SDK Code Samples.
  8.  
  9.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20.  
  21. // Add the classes in the following namespaces to our namespace
  22. using System;
  23.  
  24. class Application {
  25.     // This is a value type because of 'struct'
  26.     struct Point {
  27.         Int32 x, y;
  28.         public Point(Int32 x, Int32 y) {
  29.             this.x = x;
  30.             this.y = y;
  31.         }
  32.         public override String ToString() {
  33.             return("(" + x + "," + y + ")");
  34.         }
  35.     }
  36.     private static void DemoValueTypes() {
  37.         Console.WriteLine("Demo start: Demo of value types.");
  38.  
  39.         Point p1 = new Point(5, 10);
  40.         Point p2 = new Point(5, 10);
  41.         Point p3 = new Point(3, 4);
  42.         // What type is this valuetype & what is it derived from
  43.         Console.WriteLine("   The " + p1.GetType() + " type is derived from " + p1.GetType().BaseType);
  44.  
  45.         // Value types compare for equality by comparing the fields
  46.         Console.WriteLine("   Does p1 equal p1: " + p1.Equals(p1));   // True
  47.         Console.WriteLine("   Does p1 equal p2: " + p1.Equals(p2));   // True
  48.         Console.WriteLine("   Does p1 equal p3: " + p1.Equals(p3));   // False
  49.         Console.WriteLine("   p1={0}, p3={1}", p1.ToString(), p3.ToString());
  50.  
  51.         Console.WriteLine("Demo stop: Demo of value types.");
  52.     }
  53.  
  54.  
  55.     // This is an enumerated type because of 'enum'
  56.     enum Color { 
  57.         Red   = 111,
  58.         Green = 222,
  59.         Blue  = 333
  60.     }
  61.     private static void DemoEnums() {
  62.         Console.WriteLine("\n\nDemo start: Demo of enumerated types.");
  63.         Color c = Color.Red;
  64.  
  65.         // What type is this enum & what is it derived from
  66.         Console.WriteLine("   The " + c.GetType() + " type is derived from " + c.GetType().BaseType);
  67.  
  68.         // What is the underlying type used for the Enum's value
  69.         Console.WriteLine("   Underlying type: " + Enum.GetUnderlyingType(c.GetType()));
  70.  
  71.         // Display the set of legal enum values
  72.         Object[] o = Enum.GetValues(c.GetType());
  73.         Console.WriteLine("\n   Number of valid enum values: " + o.Length);
  74.         for (int x = 0; x < o.Length; x++) {
  75.             Color cc = ((Color)(o[x]));
  76.             Console.WriteLine("   {0}: Name={1,7}\t\tNumber={2}", x, 
  77.                 cc.Format(), cc.ToString());
  78.         }
  79.  
  80.         // Check if a value is legal for this enum
  81.         Console.WriteLine("\n   111 is a valid enum value: " + Enum.IsDefined(c.GetType(), 111));   // True
  82.         Console.WriteLine("   112 is a valid enum value: " + Enum.IsDefined(c.GetType(), 112));   // False
  83.  
  84.         // Check if two enums are equal
  85.         Console.WriteLine("\n   Is c equal to Red: " + (Color.Red == c));  // True
  86.         Console.WriteLine("   Is c equal to Blue: "  + (Color.Blue == c)); // False
  87.  
  88.         // Display the enum's value as a string
  89.         Console.WriteLine("\n   c's value as a string: " + c.Format());   // Red
  90.         Console.WriteLine("   c's value as a number: " + c.ToString());   // 111
  91.  
  92.         // Convert a string to an enum's value
  93.         c = (Color) (Enum.FromString(typeof(Color), "Blue"));
  94.         try {
  95.             c = (Color) (Enum.FromString(typeof(Color), "NotAColor")); // Not valid, raises exception
  96.         }
  97.         catch (ArgumentException) {
  98.             Console.WriteLine("   'NotAColor' is not a valid value for this enum.");
  99.         }
  100.  
  101.         // Display the enum's value as a string
  102.         Console.WriteLine("\n   c's value as a string: " + c.Format());   // Blue
  103.         Console.WriteLine("   c's value as a number: " + c.ToString());   // 333
  104.  
  105.         Console.WriteLine("Demo stop: Demo of enumerated types.");
  106.     }
  107.  
  108.  
  109.     // This is an enumerated type of flags
  110.     [Flags] 
  111.     enum ActionAttributes { 
  112.         Read   =  1,
  113.         Write  =  2,
  114.         Delete =  4,
  115.         Query  =  8,
  116.         Sync   = 16 
  117.     }
  118.     private static void DemoFlags() {
  119.         Console.WriteLine("\n\nDemo start: Demo of enumerated flags types.");
  120.         ActionAttributes aa = ActionAttributes.Read 
  121.             | ActionAttributes.Write | ActionAttributes.Query;
  122.  
  123.         // What type is this enum & what is it derived from
  124.         Console.WriteLine("   The " + aa.GetType() + " type is derived from " + aa.GetType().BaseType);
  125.  
  126.         // What is the underlying type used for the Enum's value
  127.         Console.WriteLine("   Underlying type: " + Enum.GetUnderlyingType(aa.GetType()));
  128.  
  129.         // Display the set of legal enum values
  130.         Object[] o = Enum.GetValues(aa.GetType());
  131.         Console.WriteLine("\n   Number of valid enum values: " + o.Length);
  132.         for (int x = 0; x < o.Length; x++) {
  133.             ActionAttributes aax = ((ActionAttributes)(o[x]));
  134.             Console.WriteLine("   {0}: Name={1,10}\tNumber={2}", x, 
  135.                 aax.Format(), ((ActionAttributes) aax).ToString());
  136.         }
  137.  
  138.         // Check if a value is legal for this enum
  139.         Console.WriteLine("\n   8 is a valid enum value: " + Enum.IsDefined(aa.GetType(), 8));   // True
  140.         Console.WriteLine("   6 is a valid enum value: "   + Enum.IsDefined(aa.GetType(), 6));   // False
  141.  
  142.         // Display the enum's value as a string
  143.         Console.WriteLine("\n   aa's value as a string: " + aa.Format());   // Read|Write|Query
  144.         Console.WriteLine("   aa's value as a number: "   + aa.ToString());         // 11
  145.  
  146.         // Convert a string to an enum's value
  147.         aa  = (ActionAttributes) (Enum.FromString(typeof(ActionAttributes), "Write"));
  148.         aa |= (ActionAttributes) (Enum.FromString(typeof(ActionAttributes), "Sync"));
  149.         Console.WriteLine("\n   aa's value as a string: " + aa.Format());   // Write|Sync
  150.         Console.WriteLine("   aa's value as a number: "   + aa.ToString());         // 18
  151.  
  152.         Console.WriteLine("Demo stop: Demo of enumerated flags types.");
  153.     }
  154.  
  155.     // This is a reference type because of 'class'
  156.     class Rectangle {
  157.         Int32 x, y, width, height;
  158.         public Rectangle(Int32 x, Int32 y, Int32 width, Int32 height) {
  159.             this.x = x;
  160.             this.y = y;
  161.             this.width = width;
  162.             this.height = height;
  163.         }
  164.         public override String ToString() {
  165.             return("(" + x + "," + y + ")x("+width+","+height+")");
  166.         }
  167.         public override bool Equals(Object o) {
  168.             // Change the symantics of this reference type so that it is
  169.             // equal to the same type of object if the fields are equal.
  170.             Console.WriteLine("   In Rectangle.Equals method");
  171.             Rectangle r = (Rectangle) o;
  172.             return(r.x == x && r.y == y && r.width == width && r.height == height);
  173.         }
  174.     }
  175.  
  176.     private static void DemoReferenceTypes() {
  177.         Console.WriteLine("\n\nDemo start: Demo of reference types.");
  178.         Rectangle r = new Rectangle(1, 2, 3, 4);
  179.         // What type is this reference type & what is it derived from
  180.         Console.WriteLine("   The " + r.GetType() + " type is derived from " + r.GetType().BaseType);
  181.         Console.WriteLine("   " + r);
  182.  
  183.         // Reference types are equal if they refer to the same object
  184.         Console.WriteLine("   Is r equal to (1, 2, 3, 4): " + (r == new Rectangle(1, 2, 3, 4)));       // False
  185.         Console.WriteLine("   Is r equal to (1, 2, 3, 4): " + (r.Equals(new Rectangle(1, 2, 3, 4))));  // True
  186.         Console.WriteLine("   Is r equal to (1, 1, 1, 1): " + (r == new Rectangle(1, 1, 1, 1)));       // False
  187.         Console.WriteLine("   Is r equal to (1, 1, 1, 1): " + (r.Equals(new Rectangle(1, 1, 1, 1))));  // False
  188.  
  189.         Console.WriteLine("Demo stop: Demo of reference types.");
  190.     }
  191.  
  192.     public static void Main () {
  193.         DemoValueTypes();
  194.         DemoEnums();
  195.         DemoFlags();
  196.         DemoReferenceTypes();
  197.     }
  198. }
  199.  
  200.  
  201.  
  202. ///////////////////////////////// End of File /////////////////////////////////
  203.