Fixed API: removed public constructors
[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                 private ByteType () {}
44     
45                 /**
46                  * The method converts given object to byte by the following logic:
47                  * 1. If input object is null - return 0
48                  * 2. If input object is String - run FromString method
49                  * 3. Otherwise run .NET default conversion - Convert.ToByte
50                  * @param value - The object that going to be converted
51                  * @return byte The byte value that converted from the source object
52                  * @see system.Convert#ToByte
53                  */
54                 public static System.Byte FromObject (object Value) { 
55                         if ((object)Value==null)
56                                 return 0;
57
58                         if      (Value is string)
59                                 return FromString((string) Value);
60
61                         return Convert.ToByte(Value);//Throws correct execption. Execption not converted from .java code.
62                 }
63     
64                 // Methods
65                 /**
66                          * The method try to convert given string to byte in a following way:
67                          * 1. If input string is null return 0.
68                          * 2. If input string represents number: return value of this number, 
69                          * @exception InvalidCastException - in case if number translation failed 
70                          * @param str - The string that converted to int
71                          * @return int The value that extracted from the input string.
72                          * @see Microsoft.VisualBasic.VBUtils#isNumber
73                          */ 
74                 public static System.Byte FromString (string Value) {
75                         if(Value == null)return 0;
76     
77                         int Base = 10;
78                         int start = 0;
79                         if(Value.Substring(0,1) == "&"){
80                                 //is diff base
81                                 if(Value.Substring(1,1).ToUpper() == "H")
82                                         Base = 16;
83                                 else if(Value.Substring(1,1).ToUpper() == "O")
84                                         Base = 8;
85                                 else {
86                                         // I think we should just let convert take care of the execption.
87                                         // Should we throw a special execption instead?
88                                         // I think the Mainsoft java code below just converts execptions from java to C#
89                                 }
90                                 start = 2;
91                         }
92                         return Convert.ToByte(Value.Substring(start,Value.Length - start), Base);
93   
94                         // Mainsoft java implmentation.
95                         // isNumber checks for leading &H or &O
96                         // leave for documentation.
97                         //
98                         //            if (VBUtils.isNumber(str, lRes))
99                         //            {
100                         //                long val = (long)java.lang.Math.rint(lRes[0]);
101                         //                if (val > ClrByte.MaxValue || val < ClrByte.MinValue)
102                         //                    throw new OverflowException(
103                         //                        Environment.GetResourceString("Overflow_Byte"));
104                         //                return (int) val;
105                         //            }
106                         //        }
107                         //        catch (java.lang.Exception e)
108                         //        {
109                         //            throw new InvalidCastException(
110                         //                Utils.GetResourceString("InvalidCast_FromStringTo", 
111                         //                    str, "Byte"), e);
112                         //        }
113                 }
114         };
115 }