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 * Class that converts objects to Byte value.
38 namespace Microsoft.VisualBasic.CompilerServices
40 [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute]
41 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
42 sealed public class ByteType {
45 * The method converts given object to byte by the following logic:
46 * 1. If input object is null - return 0
47 * 2. If input object is String - run FromString method
48 * 3. Otherwise run .NET default conversion - Convert.ToByte
49 * @param value - The object that going to be converted
50 * @return byte The byte value that converted from the source object
51 * @see system.Convert#ToByte
53 public static System.Byte FromObject (object Value) {
54 if ((object)Value==null)
58 return FromString((string) Value);
60 return Convert.ToByte(Value);//Throws correct execption. Execption not converted from .java code.
65 * The method try to convert given string to byte in a following way:
66 * 1. If input string is null return 0.
67 * 2. If input string represents number: return value of this number,
68 * @exception InvalidCastException - in case if number translation failed
69 * @param str - The string that converted to int
70 * @return int The value that extracted from the input string.
71 * @see Microsoft.VisualBasic.VBUtils#isNumber
73 public static System.Byte FromString (string Value) {
74 if(Value == null)return 0;
78 if(Value.Substring(0,1) == "&"){
80 if(Value.Substring(1,1).ToUpper() == "H")
82 else if(Value.Substring(1,1).ToUpper() == "O")
85 // I think we should just let convert take care of the execption.
86 // Should we throw a special execption instead?
87 // I think the Mainsoft java code below just converts execptions from java to C#
91 return Convert.ToByte(Value.Substring(start,Value.Length - start), Base);
93 // Mainsoft java implmentation.
94 // isNumber checks for leading &H or &O
95 // leave for documentation.
97 // if (VBUtils.isNumber(str, lRes))
99 // long val = (long)java.lang.Math.rint(lRes[0]);
100 // if (val > ClrByte.MaxValue || val < ClrByte.MinValue)
101 // throw new OverflowException(
102 // Environment.GetResourceString("Overflow_Byte"));
106 // catch (java.lang.Exception e)
108 // throw new InvalidCastException(
109 // Utils.GetResourceString("InvalidCast_FromStringTo",