Should I use a constructor or an event, or something entirely different?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    Should I use a constructor or an event, or something entirely different?

    I'm wondering what is better in terms of speed and memory usage.

    The way the example is used.
    Code:
            public static void Main()
            {
                ExampleClass CLASS = new ExampleClass();
                CLASS.AddItem();
                CLASS[0].SetValue("TEST");
                if (CLASS.HasChanged)
                {
                    System.Console.WriteLine("Some data has changed");
                    //Do stuff
                    CLASS.FinishedUpdate();
                }
            }
    ExampleClass used in the constructor of ExampleItem
    Code:
        public sealed class ExampleClass
        {
            public ExampleClass()
            {
                this.ExampleList = new System.Collections.Generic.List<ExampleItem>();
            }
    
            public ExampleItem this[int Index]
            {
                get { return this.ExampleList[Index]; }
            }
    
            public void FinishedUpdate()
            {
                this.HasChanged = false;
            }
    
            public void AddItem()
            {
                this.ExampleList.Add(new ExampleItem(this));
            }
    
            private System.Collections.Generic.List<ExampleItem> ExampleList;
            public bool HasChanged { get; internal set; }
        }
    Code:
        public class ExampleItem
        {
            internal ExampleItem(ExampleClass CLASS)
            {
                this.StoredCLASS = CLASS;
            }
    
            public void SetValue(string Value)
            {
                this.StoredValue = Value;
                this.StoredCLASS.HasChanged = true;
            }
    
            private ExampleClass StoredCLASS;
            private string StoredValue;
        }
    ---------------------------------------------------------
    ExampleItem with an event in ExampleClass
    Code:
            public sealed class ExampleClass
        {
            public ExampleClass()
            {
                this.ExampleList = new System.Collections.Generic.List<ExampleItem>();
            }
    
            public ExampleItem this[int Index]
            {
                get { return this.ExampleList[Index]; }
            }
    
            public void FinishedUpdate()
            {
                this.HasChanged = false;
            }
    
            public void AddItem()
            {
                this.ExampleList.Add(new ExampleItem());
                this.ExampleList[this.ExampleList.Count - 1].ChangeOccurred += () => this.HasChanged = true;
            }
    
            private System.Collections.Generic.List<ExampleItem> ExampleList;
            public bool HasChanged { get; private set; }
        }
    Code:
        public class ExampleItem
        {
            public void SetValue(string Value)
            {
                this.StoredValue = Value;
                if (this.ChangeOccurred != null) this.ChangeOccurred();
            }
    
            public delegate void Changed();
            public event Changed ChangeOccurred;
    
            private string StoredValue;
        }
    NOTE: StoredValue will most likely always be null, but might change. Also, just in case it affects anything, the final outcome of ExampleClass most likely have 2 constructors, about 4 booleans properties, and about 6 functions.

    I want to know which method (Those two, or any you know of) is faster, and requires the least memory, or any other way to find out if the StoredValue has changed.
Working...