2007-05-23 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System / Int16.cs
index 2214d60facf8eca4df13f2d61d09873f07e51898..44104e51fb4aef81135bb1b090f40146901d3b6a 100644 (file)
@@ -32,6 +32,9 @@ using System.Globalization;
 namespace System {
        
        [Serializable]
+#if NET_2_0
+       [System.Runtime.InteropServices.ComVisible (true)]
+#endif
        public struct Int16 : IFormattable, IConvertible, IComparable
 #if NET_2_0
                , IComparable<Int16>, IEquatable <Int16>
@@ -90,7 +93,7 @@ namespace System {
                }
 #endif
 
-               internal static bool Parse (string s, bool tryParse, out short result)
+               internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
                {
                        short val = 0;
                        int len;
@@ -98,12 +101,13 @@ namespace System {
                        bool digits_seen = false;
 
                        result = 0;
+                       exc = null;
 
-                       if (s == null)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new ArgumentNullException ("s");
+                       if (s == null) {
+                               if (!tryParse)
+                                       exc = new ArgumentNullException ("s");
+                               return false;
+                       }
 
                        len = s.Length;
 
@@ -114,11 +118,11 @@ namespace System {
                                        break;
                        }
                        
-                       if (i == len)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new FormatException ();
+                       if (i == len) {
+                               if (!tryParse)
+                                       exc = Int32.GetFormatException ();
+                               return false;
+                       }
 
                        c = s [i];
                        if (c == '+')
@@ -137,25 +141,25 @@ namespace System {
                                } else {
                                        if (Char.IsWhiteSpace (c)){
                                                for (i++; i < len; i++){
-                                                       if (!Char.IsWhiteSpace (s [i]))
-                                                               if (tryParse)
-                                                                       return false;
-                                                               else
-                                                                       throw new FormatException ();
+                                                       if (!Char.IsWhiteSpace (s [i])) {
+                                                               if (!tryParse)
+                                                                       exc = Int32.GetFormatException ();
+                                                               return false;
+                                                       }
                                                }
                                                break;
-                                       } else
-                                               if (tryParse)
-                                                       return false;
-                                               else
-                                                       throw new FormatException ();
+                                       } else {
+                                               if (!tryParse)
+                                                       exc = Int32.GetFormatException ();
+                                               return false;
+                                       }
                                }
                        }
-                       if (!digits_seen)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new FormatException ();
+                       if (!digits_seen) {
+                               if (!tryParse)
+                                       exc = Int32.GetFormatException ();
+                               return false;
+                       }
                        
                        result = val;
                        return true;
@@ -180,41 +184,42 @@ namespace System {
                        return (short) tmpResult;
                }
 
-               public static short Parse (string s) {
+               public static short Parse (string s) 
+               {
+                       Exception exc;
                        short res;
 
-                       Parse (s, false, out res);
+                       if (!Parse (s, false, out res, out exc))
+                               throw exc;
 
                        return res;
                }
 
 #if NET_2_0
-               public static bool TryParse (string s, out short result) {
-                       try {
-                               return Parse (s, true, out result);
-                       }
-                       catch (Exception) {
+               public static bool TryParse (string s, out short result) 
+               {
+                       Exception exc;
+                       if (!Parse (s, true, out result, out exc)) {
                                result = 0;
                                return false;
                        }
-               }
 
-               public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) {
-                       try {
-                               int tmpResult;
+                       return true;
+               }
 
-                               result = 0;
-                               if (!Int32.TryParse (s, style, provider, out tmpResult))
-                                       return false;
-                               if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
-                                       return false;
-                               result = (short)tmpResult;
-                               return true;
-                       }
-                       catch (Exception) {
-                               result = 0;
+               public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) 
+               {
+                       int tmpResult;
+                       result = 0;
+                               
+                       if (!Int32.TryParse (s, style, provider, out tmpResult))
                                return false;
-                       }
+                       
+                       if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
+                               return false;
+                               
+                       result = (short)tmpResult;
+                       return true;
                }
 #endif