Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.Cockpit/MainWindow/StringParseValidationRule.cs @ 4401

Last change on this file since 4401 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Controls;
6using System.Windows;
7
8namespace HeuristicLab.OKB.Cockpit {
9  public class StringParseValidationRule : ValidationRule {
10
11    private Type type;
12    public Type Type {
13      get {
14        return type;
15      }
16      set {
17        if (type == value)
18          return;
19        if (type == null)
20          return;
21        switch (Type.GetTypeCode(value)) {
22          // TODO parse nullables
23          case TypeCode.Int32: break;
24          case TypeCode.Double: break;
25          case TypeCode.String: break;
26          case TypeCode.DateTime: break;
27          default:
28            if (value.GetType() == typeof(Guid))
29              break;
30            //throw new ArgumentException(String.Format(
31            //"Parsing validation of type {0} is not supported", value));
32            break;
33        }
34        type = value;
35      }
36    }
37
38    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) {
39      if (type == null)
40        return new ValidationResult(true, null);
41      try {
42        string s = (string)value;
43        switch (Type.GetTypeCode(type)) {
44          case TypeCode.Int32: int.Parse(s); break;
45          case TypeCode.Double: double.Parse(s); break;
46          case TypeCode.String: break;
47          case TypeCode.DateTime: DateTime.Parse(s); break;
48          default:
49            if (value.GetType() == typeof(Guid))
50              new Guid(s);
51            else
52              throw new InvalidOperationException();
53            break;
54        }
55        return new ValidationResult(true, null);
56      } catch {
57        return new ValidationResult(false, String.Format("'{0}' is not a valid '{1}'", value, Type.Name));
58      }
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.