* Interaction.cs :
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / IntegerType.cs
1 //
2 // IntegerType.cs
3 //
4 //      Author:
5 //      Chris J Breisch (cjbreisch@altavista.net) 
6 //      Francesco Delfino (pluto@tipic.com)
7 //      Dennis Hayes (dennish@raytek.com)
8 //
9 //      (C) copyright 2002 Chris J Breisch
10 //      2002 Tipic, Inc (http://www.tipic.com)
11 //
12  /*
13   * Copyright (c) 2002-2003 Mainsoft Corporation.
14   * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15   *
16   * Permission is hereby granted, free of charge, to any person obtaining a
17   * copy of this software and associated documentation files (the "Software"),
18   * to deal in the Software without restriction, including without limitation
19   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20   * and/or sell copies of the Software, and to permit persons to whom the
21   * Software is furnished to do so, subject to the following conditions:
22   * 
23   * The above copyright notice and this permission notice shall be included in
24   * all copies or substantial portions of the Software.
25   * 
26   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32   * DEALINGS IN THE SOFTWARE.
33   */
34 using System;
35 using System.ComponentModel;
36 using Microsoft.VisualBasic;
37
38 namespace Microsoft.VisualBasic.CompilerServices
39 {
40         [StandardModule, EditorBrowsable(EditorBrowsableState.Never)] 
41         sealed public class IntegerType {
42                 private IntegerType () {}
43
44                 // Methods
45                 /**
46                  * The method try to convert given string to int in a following way:
47                  * 1. If input string is null return 0.
48                  * 2. If input string represents number: return value of this number, 
49                  * @exception InvalidCastException - in case if number translation failed 
50                  * @param str - The string that converted to int
51                  * @return int The value that extracted from the input string.
52                  * @see Microsoft.VisualBasic.VBUtils#isNumber
53                  */ 
54                 public static System.Int32 FromString (string Value) {
55                         if(Value == null)return 0;
56
57                         long [] val = new long [1];
58                         try {
59                                 if (StringType.IsHexOrOctValue (Value, val))
60                                         return Convert.ToInt32 (val [0]);
61
62                                 return Convert.ToInt32 (Convert.ToDouble (Value)); 
63                         } catch (FormatException e) {
64                                 throw new InvalidCastException(
65                                         Utils.GetResourceString("InvalidCast_FromStringTo", 
66                                         Value, "Integer"));
67                         }
68                         return 0;
69                 }
70
71                 public static System.Int32 FromObject (object Value) { 
72                         if ((object)Value==null)return 0;
73
74                         if(Value is string)return FromString((string) Value);
75
76                         if(Value is int)return ((int)Value);
77                         
78                         return System.Convert.ToInt32(Value);
79                 }
80         }
81 }