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