This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / BooleanType.cs
1 //
2 // BooleanType.cs
3 //
4 //      Author:
5 //      Chris J Breisch (cjbreisch@altavista.net) 
6 //      Dennis Hayes        (dennish@raytek.com)
7 //
8 //      (C) 2002 Chris J Breisch
9 //              (C) 2004 Joerg Rosenkranz <JoergR@voelcker.com>
10 //
11  /*
12   * Copyright (c) 2002-2003 Mainsoft Corporation.
13   * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14   *
15   * Permission is hereby granted, free of charge, to any person obtaining a
16   * copy of this software and associated documentation files (the "Software"),
17   * to deal in the Software without restriction, including without limitation
18   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19   * and/or sell copies of the Software, and to permit persons to whom the
20   * Software is furnished to do so, subject to the following conditions:
21   * 
22   * The above copyright notice and this permission notice shall be included in
23   * all copies or substantial portions of the Software.
24   * 
25   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31   * DEALINGS IN THE SOFTWARE.
32   */
33 using System;
34 using System.Globalization;
35
36 namespace Microsoft.VisualBasic.CompilerServices
37 {
38         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] 
39         [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] 
40         sealed public class BooleanType {
41                 /**
42                                  * The method converts given object to boolean by the following logic:
43                                  * 1. If input object is null - return null
44                                  * 2. If input object is String - run FromString method
45                                  * 3. Otherwise run .NET default conversion - Convert.ToBoolean
46                                  * @param value - The object that going to be converted
47                                  * @return boolean The boolean value that converted from the source object
48                                  */
49                 public static System.Boolean FromObject (object Value) {
50                         if (Value == null)
51                                 return false;
52
53                         if (Value is string)
54                                 return FromString((string)Value);
55
56                         //This throws the correct execption, Mainsoft java code has to catch and rethrow to map java to .net
57                         return Convert.ToBoolean(Value);
58                 }
59
60                 /**
61                                  * The method try to convert given string to boolean in a following way:
62                                  * 1. If input value is True or False string - return corresponding value
63                                  * 2. If input string represents number: return true if this number is not 
64                                  *    equals to zero, otherwise return false;
65                                  * @exception InvalidCastException - in case if number translation failed 
66                                  *  due to NumberFormatException.
67                                  * @exception All other thrown exceptions from ClrDobule.Parse 
68                                  * @param str - The string that converted to boolean
69                                  * @return boolean The value that extracted from the input string. 
70                                  */
71                 public static Boolean FromString (string Value) {
72                         if(Value == null)
73                                 return false;
74
75                         if (string.Compare(Value, bool.TrueString, true) == 0)
76                                 return true;
77                         if (string.Compare(Value, bool.FalseString, true) == 0)
78                                 return false;
79                         
80                         double conv;
81                         if (double.TryParse(Value, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out conv))
82                                 return (conv != 0);
83                          
84                         throw new InvalidCastException (
85                                 string.Format (
86                                 "Cast from string \"{0}\" to type 'Boolean' is not valid.",
87                                 Value));
88                 }        
89         }
90 }