5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Francesco Delfino (pluto@tipic.com)
7 // Dennis Hayes (dennish@raytek.com)
9 // (C) copyright 2002 Chris J Breisch
10 // 2002 Tipic, Inc (http://www.tipic.com)
13 * Copyright (c) 2002-2003 Mainsoft Corporation.
14 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
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.
35 using System
.ComponentModel
;
36 using Microsoft
.VisualBasic
;
38 namespace Microsoft
.VisualBasic
.CompilerServices
40 [StandardModule
, EditorBrowsable(EditorBrowsableState
.Never
)]
41 sealed public class IntegerType
{
42 private IntegerType () {}
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
54 public static System
.Int32
FromString (string Value
) {
55 if(Value
== null)return 0;
57 double[] lRes
= new double[1];
58 //the following handles &H &O and other stuff. int.parse does not.
59 if (VBUtils
.isNumber(Value
, lRes
)) {
60 long val
= (long)Math
.Round(lRes
[0]);
61 if (val
> Int32
.MaxValue
|| val
< Int32
.MinValue
)
62 throw new OverflowException(
63 /*Environment.GetResourceString(*/"Overflow_Int32")/*)*/;
69 public static System
.Int32
FromObject (object Value
) {
70 if ((object)Value
==null)return 0;
72 if(Value
is string)return FromString((string) Value
);
74 if(Value
is int)return ((int)Value
);
76 return System
.Convert
.ToInt32(Value
);