2005-05-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / CharType.cs
1 //
2 // CharType.cs
3 //
4 // Author:
5 //      Chris J Breisch (cjbreisch@altavista.net)
6 //      Dennis Hayes (dennish@raytek.com)
7 //
8 //      (C) 2002 Chris J Breisch
9 //
10   /* Copyright (c) 2002-2003 Mainsoft Corporation.
11   *  Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12   * 
13   * Permission is hereby granted, free of charge, to any person obtaining a
14   * copy of this software and associated documentation files (the "Software"),
15   * to deal in the Software without restriction, including without limitation
16   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17   * and/or sell copies of the Software, and to permit persons to whom the
18   * Software is furnished to do so, subject to the following conditions:
19   * 
20   * The above copyright notice and this permission notice shall be included in
21   * all copies or substantial portions of the Software.
22   * 
23   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29   * DEALINGS IN THE SOFTWARE.
30   */
31
32 using System;
33 using System.ComponentModel;
34
35 namespace Microsoft.VisualBasic.CompilerServices
36 {
37         [StandardModule, EditorBrowsable(EditorBrowsableState.Never)] 
38         sealed public class CharType {
39                 private CharType () {}
40
41                 /**
42                    * The method converts given object to char by the following logic:
43                    * 1. If input object is null - return '\0'
44                    * 2. If input object is String - run FromString method 
45                    * 3. Otherwise run .NET default conversion - Convert.ToChar
46                    * @param value - The object that going to be converted
47                    * @return char The char value that converted from the source object
48                    * @see system.Convert#ToChar
49                    */
50                 public static char FromObject(object Value) {
51                         if (Value == null)
52                                 return '\0';
53
54                         if (Value is string)
55                                 return FromString((string)Value);
56
57                         //Mainsoft code creates a new execption, but that is just to convert from java execption to .net execption
58                         return Convert.ToChar(Value);
59                 }
60
61                 /**
62                    * The method returns first character of the input string. If input string 
63                    * is null or empty return it returns '\0'
64                    * @param str - The string that converted to char
65                    * @return char The value that extracted from the input string.
66                    */
67                 public static char FromString(string Value) {
68                         if (Value == null || Value.Length == 0)
69                                 return '\0';
70
71                         return Value.ToCharArray()[0];
72                 }
73         }
74 }
75