comparison Chronosv2/source/Extensions/StringConverter.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2
3 namespace Chronos.Extensions
4 {
5 /// <summary>
6 /// Universal conversion and parsing methods for strings.
7 /// These methods are avaiblable throught the generic object.Convert method:
8 /// Feel free to provide additional converns for string or any other object type.
9 /// </summary>
10 /// <example><code>
11 /// var value = "123";
12 /// var numeric = value.Convert().ToInt32();
13 /// </code></example>
14 public static class StringConverter
15 {
16 #region · Extensions ·
17
18 /// <summary>
19 /// Converts a string to an Int32 value
20 /// </summary>
21 /// <param name="value">The value.</param>
22 /// <returns></returns>
23 /// <example><code>
24 /// var value = "123";
25 /// var numeric = value.Convert().ToInt32();
26 /// </code></example>
27 public static int ToInt32(this IConverter<string> value)
28 {
29 return ToInt32(value, 0, false);
30 }
31
32 /// <summary>
33 /// Converts a string to an Int32 value
34 /// </summary>
35 /// <param name="value">The value.</param>
36 /// <param name="defaultValue">The default value.</param>
37 /// <param name="ignoreException">if set to <c>true</c> any parsing exception will be ignored.</param>
38 /// <returns></returns>
39 /// <example><code>
40 /// var value = "123";
41 /// var numeric = value.Convert().ToInt32();
42 /// </code></example>
43 public static int ToInt32(this IConverter<string> value, int defaultValue, bool ignoreException)
44 {
45 if (ignoreException)
46 {
47 try
48 {
49 return ToInt32(value, defaultValue, false);
50 }
51 catch
52 {
53 }
54
55 return defaultValue;
56 }
57
58 return int.Parse(value.Value);
59 }
60
61 #endregion
62 }
63 }