Changeset 2653 for trunk/sources/HeuristicLab.Core/3.3/Variable.cs
- Timestamp:
- 01/20/10 05:00:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/Variable.cs
r2526 r2653 29 29 namespace HeuristicLab.Core { 30 30 /// <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. 32 32 /// </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; 56 37 /// <inheritdoc/> 57 38 /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks> 39 [Storable] 58 40 public IItem Value { 59 get { return myValue; }41 get { return value; } 60 42 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); 63 47 OnValueChanged(); 64 48 } … … 70 54 /// and value <c>null</c>. 71 55 /// </summary> 72 public Variable() {73 myName = "Anonymous";74 myValue = null;56 public Variable() 57 : base("Anonymous") { 58 Value = null; 75 59 } 76 60 /// <summary> … … 80 64 /// <param name="name">The name of the current instance.</param> 81 65 /// <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; 85 69 } 86 70 … … 95 79 /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param> 96 80 /// <returns>The cloned object as <see cref="Variable"/>.</returns> 97 public override I Item Clone(ICloner cloner) {81 public override IDeepCloneable Clone(Cloner cloner) { 98 82 Variable clone = new Variable(); 99 83 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); 103 87 return clone; 104 88 } … … 109 93 /// <returns>The current instance as a string.</returns> 110 94 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); 112 99 } 113 100 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 }135 101 /// <inheritdoc/> 136 102 public event EventHandler ValueChanged; … … 138 104 /// Fires a new <c>ValueChanged</c> even. 139 105 /// </summary> 140 pr otected virtualvoid OnValueChanged() {106 private void OnValueChanged() { 141 107 if (ValueChanged != null) 142 108 ValueChanged(this, new EventArgs()); 143 109 OnChanged(); 144 110 } 111 112 private void Value_Changed(object sender, ChangedEventArgs e) { 113 OnChanged(e); 114 } 145 115 } 146 116 }
Note: See TracChangeset
for help on using the changeset viewer.