Another Random.cs compile fix.
[mono.git] / mcs / class / corlib / System / Boolean.cs
1 //
2 // System.Boolean.cs
3 //
4 // Author:
5 //   Derek Holden (dholden@draper.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // I guess this is the Boolean class. This was written word for word
12 // off the Library specification for System.Boolean in the ECMA
13 // TC39 TG2 and TG3 working documents.
14 //
15 // The XML style documentation isn't that elegant, but it seems to 
16 // be the standard way according to the poorly documented C# 
17 // Programmer's Reference section on XML Documentation.
18 //
19 // This header and the one above it can be formatted however, just trying
20 // to keep it consistent w/ the existing mcs headers.
21 //
22 // Even though it's not in the ECMA docs, the .NET Framework Class Library
23 // says this implements IConvertible, but if it does it has some other
24 // member functions to implement. 
25 //
26
27 namespace System {
28
29         /// <summary>
30         /// Represents the boolean values of logical true and false.
31         /// </summary>
32         // The .NET Framework SDK lists this as implementing IConvertible,
33         // though it's not done in the ECMA spec.
34         public struct Boolean : IComparable { //, IConvertible {
35                 
36                 /// <value>
37                 /// The String representation of Boolean False
38                 /// </value>    
39                 public static readonly string FalseString;
40
41                 /// <value>
42                 /// The String representation of Boolean True
43                 /// </value>    
44                 public static readonly string TrueString;
45       
46                 /// <value>
47                 /// Internal bool value for for this instance
48                 /// </value>
49                 //
50                 // HACK: we tag it as public, so the source will compile.               
51                 public bool value;
52         
53                 static Boolean () 
54                 {
55                         FalseString = "False";
56                         TrueString = "True";
57                 }
58
59                 /// <summary>
60                 /// Compares the current Boolean instance against another object.
61                 /// </summary>
62                 /// <remarks>
63                 /// Throws an ArgumentException if <c>obj</c> isn't null or 
64                 /// a Boolean.
65                 /// </remarks>
66                 /// <param name="obj">
67                 /// The object to compare against
68                 /// </param>
69                 /// <returns>
70                 /// An int reflecting the sort order of this instance as 
71                 /// compared to <c>obj</c>
72                 /// -1 if this instance is false and <c>obj</c> is true
73                 ///  0 if this instance is equal to <c>obj</c>
74                 ///  1 if this instance is true and <c>obj</c> is false, 
75                 ///    or <c>obj</c> is null
76                 /// </returns>
77                 public int CompareTo (object obj) 
78                 {
79                         if(obj != null && !(obj is System.Boolean))
80                                 throw new ArgumentException
81                                 ("Object is not a Boolean and is not a null reference");
82                         
83                         // for case #3
84                         if (obj == null || (value == true && (bool)obj == false))
85                                 return 1;
86             
87                         // for case #2, else it's #1
88                         return (value == (bool)obj) ? 0 : -1;
89                 }
90         
91                 /// <summary>
92                 /// Determines whether this instance and another object represent the
93                 /// same type and value.
94                 /// </summary>
95                 /// <param name="obj">
96                 /// The object to check against
97                 /// </param>
98                 /// <returns>
99                 /// true if this instnace and <c>obj</c> are same value, 
100                 /// otherwise false if it is not or null
101                 /// </returns>
102                 public override bool Equals (Object obj) 
103                 {
104                         if (obj == null || !(obj is System.Boolean))
105                                 return false;
106
107                         return ((bool)obj) == value;
108                 }
109         
110                 /// <summary>
111                 /// Generates a hashcode for this object.
112                 /// </summary>
113                 /// <returns>
114                 /// An Int32 value holding the hash code
115                 /// </returns>
116                 public override int GetHashCode () 
117                 {
118                         // Guess there's not too many ways to hash a Boolean
119                         return value ? 1 : 0;
120                 }
121
122                 /// <summary>
123                 /// Returns a given string as a boolean value. The string must be 
124                 /// equivalent to either TrueString or FalseString, with leading and/or
125                 /// trailing spaces, and is parsed case-insensitively.
126                 /// </summary>
127                 /// <remarks>
128                 /// Throws an ArgumentNullException if <c>val</c> is null, or a 
129                 /// FormatException if <c>val</c> doesn't match <c>TrueString</c> 
130                 /// or <c>FalseString</c>
131                 /// </remarks>
132                 /// <param name="val">
133                 /// The string value to parse
134                 /// </param>
135                 /// <returns>
136                 /// true if <c>val</c> is equivalent to TrueString, 
137                 /// otherwise false
138                 /// </returns>
139                 public static bool Parse (string val) 
140                 {
141                         if (val == null)
142                                 throw new ArgumentNullException ("Value is a null reference");
143             
144                         val = val.Trim ();
145             
146                         if (String.Compare (val, TrueString, true) == 0)
147                                 return true;
148             
149                         if (String.Compare (val, FalseString, true) == 0)
150                                 return false;
151             
152                         throw new FormatException
153                         ("Value is not equivalent to either TrueString or FalseString");
154                 }
155
156                 /// <summary>
157                 /// Returns a string representation of this Boolean object.
158                 /// </summary>
159                 /// <returns>
160                 /// <c>FalseString</c> if the instance value is false, otherwise 
161                 /// <c>TrueString</c>
162                 /// </returns>
163                 public override string ToString () 
164                 {
165                         return value ? TrueString : FalseString;
166                 }
167                 
168                 // =========== IConvertible Methods =========== //
169
170                 public TypeCode GetTypeCode () 
171                 { 
172                         return TypeCode.Boolean;
173                 }
174
175         } // System.Boolean
176
177 } // Namespace System