comparison MetroWpf/MetroWpf.Framework/Extensions/StringConverter.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System;
2
3 namespace MetroWpf
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 }