Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/20/10 05:00:28 (14 years ago)
Author:
swagner
Message:

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/Variable.cs

    r2526 r2653  
    2929namespace HeuristicLab.Core {
    3030  /// <summary>
    31   /// Represents a variable of an operator having a name and a value.
     31  /// Represents a variable which has a name and holds an IItem.
    3232  /// </summary>
    33   public class Variable : ItemBase, IVariable {
    34 
    35     [Storable]
    36     private string myName;
    37     /// <inheritdoc/>
    38     /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
    39     /// eventually in the setter.</remarks>
    40     public string Name {
    41       get { return myName; }
    42       set {
    43         if (!myName.Equals(value)) {
    44           CancelEventArgs<string> e = new CancelEventArgs<string>(value);
    45           OnNameChanging(e);
    46           if (!e.Cancel) {
    47             myName = value;
    48             OnNameChanged();
    49           }
    50         }
    51       }
    52     }
    53 
    54     [Storable]
    55     private IItem myValue;
     33  [Item("Variable", "A variable which has a name and holds an IItem.")]
     34  [Creatable("Test")]
     35  public sealed class Variable : NamedItemBase {
     36    private IItem value;
    5637    /// <inheritdoc/>
    5738    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
     39    [Storable]
    5840    public IItem Value {
    59       get { return myValue; }
     41      get { return value; }
    6042      set {
    61         if (myValue != value) {
    62           myValue = value;
     43        if (this.value != value) {
     44          if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed);
     45          this.value = value;
     46          if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
    6347          OnValueChanged();
    6448        }
     
    7054    /// and value <c>null</c>.
    7155    /// </summary>
    72     public Variable() {
    73       myName = "Anonymous";
    74       myValue = null;
     56    public Variable()
     57      : base("Anonymous") {
     58      Value = null;
    7559    }
    7660    /// <summary>
     
    8064    /// <param name="name">The name of the current instance.</param>
    8165    /// <param name="value">The value of the current instance.</param>
    82     public Variable(string name, IItem value) {
    83       myName = name;
    84       myValue = value;
     66    public Variable(string name, IItem value)
     67      : base(name) {
     68      Value = value;
    8569    }
    8670
     
    9579    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    9680    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
    97     public override IItem Clone(ICloner cloner) {
     81    public override IDeepCloneable Clone(Cloner cloner) {
    9882      Variable clone = new Variable();
    9983      cloner.RegisterClonedObject(this, clone);
    100       clone.myName = Name;
    101       if (Value != null)
    102         clone.myValue = (IItem)cloner.Clone(Value);
     84      clone.name = name;
     85      clone.description = description;
     86      clone.Value = (IItem)cloner.Clone(value);
    10387      return clone;
    10488    }
     
    10993    /// <returns>The current instance as a string.</returns>
    11094    public override string ToString() {
    111       return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
     95      if (Value == null)
     96        return string.Format("{0}: null", Name);
     97      else
     98        return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().Name);
    11299    }
    113100
    114     /// <inheritdoc/>
    115     public event EventHandler<CancelEventArgs<string>> NameChanging;
    116     /// <summary>
    117     /// Fires a new <c>NameChanging</c> event.
    118     /// </summary>
    119     /// <param name="e">The event arguments of the changing.</param>
    120     protected virtual void OnNameChanging(CancelEventArgs<string> e) {
    121       if (NameChanging != null)
    122         NameChanging(this, e);
    123     }
    124     /// <inheritdoc/>
    125     public event EventHandler NameChanged;
    126     /// <summary>
    127     /// Fires a new <c>NameChanged</c> event.
    128     /// </summary>
    129     /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
    130     protected virtual void OnNameChanged() {
    131       if (NameChanged != null)
    132         NameChanged(this, new EventArgs());
    133       OnChanged();
    134     }
    135101    /// <inheritdoc/>
    136102    public event EventHandler ValueChanged;
     
    138104    /// Fires a new <c>ValueChanged</c> even.
    139105    /// </summary>
    140     protected virtual void OnValueChanged() {
     106    private void OnValueChanged() {
    141107      if (ValueChanged != null)
    142108        ValueChanged(this, new EventArgs());
    143109      OnChanged();
    144110    }
     111
     112    private void Value_Changed(object sender, ChangedEventArgs e) {
     113      OnChanged(e);
     114    }
    145115  }
    146116}
Note: See TracChangeset for help on using the changeset viewer.