This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / ByteType.cs
1     //
2     // ByteType.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     /**
35      * Class that converts objects to Byte value.
36      */
37 using System;
38 namespace Microsoft.VisualBasic.CompilerServices 
39 {
40         [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] 
41         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] 
42         sealed public class ByteType {
43     
44                 /**
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
52                  */
53                 public static System.Byte FromObject (object Value) { 
54                         if ((object)Value==null)
55                                 return 0;
56
57                         if      (Value is string)
58                                 return FromString((string) Value);
59
60                         return Convert.ToByte(Value);//Throws correct execption. Execption not converted from .java code.
61                 }
62     
63                 // Methods
64                 /**
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
72                          */ 
73                 public static System.Byte FromString (string Value) {
74                         if(Value == null)return 0;
75     
76                         int Base = 10;
77                         int start = 0;
78                         if(Value.Substring(0,1) == "&"){
79                                 //is diff base
80                                 if(Value.Substring(1,1).ToUpper() == "H")
81                                         Base = 16;
82                                 else if(Value.Substring(1,1).ToUpper() == "O")
83                                         Base = 8;
84                                 else {
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#
88                                 }
89                                 start = 2;
90                         }
91                         return Convert.ToByte(Value.Substring(start,Value.Length - start), Base);
92   
93                         // Mainsoft java implmentation.
94                         // isNumber checks for leading &H or &O
95                         // leave for documentation.
96                         //
97                         //            if (VBUtils.isNumber(str, lRes))
98                         //            {
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"));
103                         //                return (int) val;
104                         //            }
105                         //        }
106                         //        catch (java.lang.Exception e)
107                         //        {
108                         //            throw new InvalidCastException(
109                         //                Utils.GetResourceString("InvalidCast_FromStringTo", 
110                         //                    str, "Byte"), e);
111                         //        }
112                 }
113         };
114 }