1

I wanna (want to) capture the event when the user adds a new state to the country.

I will apply some business rules to this new State before inserting into the database.

namespace ClassLibrary1
{
    public class Country
    {
        public Country()
        {
            States = new HashSet<State>();
        }
        public virtual ICollection<State> States { get; set; }
    }
    public class State {
        public string Code { get; set; }
        public string Name { get; set; }
    }
    public class test {
        public test()
        {
            Country c = new Country();
            //I wanna (want to) capture this action inside of the class "Country"
            c.States.Add(new State { Code = "US", Name = "Unated States" });
        }
    }

}

Thank you everyone

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • It seems that you need an observable hashset (similiar to [`ObservableCollection`](https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx)), but I'm not aware of one in the framework (other than as part of EntityFramework). [The answers to this question might help](https://stackoverflow.com/questions/526986/how-can-i-make-an-observable-hashset-in-c) – ProgrammingLlama Jul 26 '18 at 02:08
  • 1
    I'm actually still confused as to what question you're asking. –  Jul 26 '18 at 02:10
  • @Francisco Alternatively, are you open to exposing `States` as read only, and then having an `AddState` method? – ProgrammingLlama Jul 26 '18 at 02:13

2 Answers2

0

Probably the easiest way is to create AddState() and RemoveState() methods on the Country class. Then use those methods instead of accessing the HashSet directly. Like so:

namespace ClassLibrary1
{
    public class Country
    {
        private readonly HashSet<State> states;
        public Country()
        {
            states = new HashSet<State>();
        }
        public void AddState(State value)
        {
            states.Add(value);
            // Fire event
        }
        public void RemoveState(State value)
        {
            states.Remove(value);
            // Fire event
        }
    }
    public class State {
        public string Code { get; set; }
        public string Name { get; set; }
    }
    public class test {
        public test()
        {
            Country c = new Country();
            // Captured!
            c.AddState(new State { Code = "US", Name = "United States" });
        }
    }

}
w4g3n3r
  • 967
  • 5
  • 8
0

Thank you everyone, I resolve my problem using BindingList object instead HashSet. This is my code and it is working as I spect.

public class Country
    {
        public Country()
        {
            States = new BindingList<State>();
            this._States.ListChanged += this.EditState;
        }
        private BindingList<State> _States = new BindingList<State>();
        public BindingList<State> States
        {
            get { return _States; }
            set
            {
                _States = value;
            }
        }
        public void EditState(object sender, ListChangedEventArgs args)
        {
            if (args.ListChangedType == ListChangedType.ItemAdded) {
                _States[args.NewIndex].Code = "NY";
                _States[args.NewIndex].Name = "New York";
            }       
        }
    }
    public class State
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }
    public class test
    {
        public test()
        {
            Country c = new Country();
            //I wanna (want to) capture this action inside of the class "Country"
            c.States.Add(new State { Code = "US", Name = "Unated States" });
        }
    }