Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/25/13 10:43:27 (11 years ago)
Author:
mkommend
Message:

#2075: Added element names for the ValueType- and StringArray classes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data/3.3/StringArray.cs

    r9456 r9657  
    4343    protected string[] array;
    4444
     45    [Storable]
     46    protected List<string> elementNames;
     47    public virtual IEnumerable<string> ElementNames {
     48      get { return this.elementNames; }
     49      set {
     50        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
     51        if (value == null || !value.Any())
     52          elementNames = new List<string>();
     53        else if (value.Count() != Length)
     54          throw new ArgumentException("An element name must be specified for each element.");
     55        else
     56          elementNames = new List<string>(value);
     57        OnElementNamesChanged();
     58      }
     59    }
     60
    4561    public virtual int Length {
    4662      get { return array.Length; }
     
    4965        if (value != Length) {
    5066          Array.Resize<string>(ref array, value);
     67          while (elementNames.Count > value)
     68            elementNames.RemoveAt(elementNames.Count - 1);
     69          while (elementNames.Count < value)
     70            elementNames.Add("Element " + elementNames.Count);
     71          OnElementNamesChanged();
    5172          OnReset();
    5273        }
     
    7293    }
    7394
     95    [StorableHook(HookType.AfterDeserialization)]
     96    private void AfterDeserialization() {
     97      if (elementNames == null) { elementNames = new List<string>(); }
     98    }
     99
    74100    [StorableConstructor]
    75101    protected StringArray(bool deserializing) : base(deserializing) { }
     
    78104      this.array = (string[])original.array.Clone();
    79105      this.readOnly = original.readOnly;
     106      this.elementNames = new List<string>(original.elementNames);
    80107    }
    81108    public StringArray() {
    82109      array = new string[0];
    83110      readOnly = false;
     111      elementNames = new List<string>();
    84112    }
    85113    public StringArray(int length) {
     
    88116        array[i] = string.Empty;
    89117      readOnly = false;
     118      elementNames = new List<string>();
    90119    }
    91120    public StringArray(string[] elements) {
     
    95124        array[i] = elements[i] == null ? string.Empty : elements[i];
    96125      readOnly = false;
     126      elementNames = new List<string>();
    97127    }
    98128
     
    153183    }
    154184
     185    public event EventHandler ElementNamesChanged;
     186    protected virtual void OnElementNamesChanged() {
     187      EventHandler handler = ElementNamesChanged;
     188      if (handler != null)
     189        handler(this, EventArgs.Empty);
     190    }
     191
    155192    public event EventHandler<EventArgs<int>> ItemChanged;
    156193    protected virtual void OnItemChanged(int index) {
Note: See TracChangeset for help on using the changeset viewer.