Good afternoon! Tell me how to solve the puzzle:

there is a collection class

public class SortBoard { public double A { get; set; } public double B { get; set; } public double C { get; set; } } 

There is an announced collection:

 public ObservableCollection<SortBoard> collection; 

And there is a main class of the program where calculations are performed.

 class Program { public ObservableCollection<SortBoard> collection; public double k; static void Main(string[] args) { collection = new ObservableCollection<SortBoard>(); collection.Add(new SortBoard() {A=10,B=20,C=30}); collection.Add(new SortBoard() {A=12,B=18,C=33}); for (int i =0; i<500;i++) { if (i % 2 ) { k=i*0.35; } } } } 

How can you implement an event in which when "k" is changed so that the data in the collection is updated according to the following logic:

A = C * 2 * k B = C * 3 * k

I can not understand how to delegates and events here to tie

    1 answer 1

    Why do you want an event?

    Why shouldn't K be put in the inside of some class that is responsible for storing the collection? (For example, make inheritance from ObservableCollection

    by adding your constant to the extended class.)

    You bring in K as a property, you hang a trigger on the setter, which will cause the collection to be updated.

    Upon request, I post my implementation:

      namespace TestConsoleProg { class Program { public class SortBoard { public double A { get; set; } public double B { get; set; } public double C { get; set; } } public class ObservableCollectionEx<SortBoard> : ObservableCollection<SortBoard> { double _k; public double k { get { return _k; } set { _k = value; foreach (dynamic t in Items)//НавСрноС ΠΊΠ°ΠΊ-Ρ‚ΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠ±ΠΎΠΉΡ‚ΠΈΡΡŒ Π±Π΅Π· динамичСского Ρ‚ΠΈΠΏΠ°... //Если ΠΊΡ‚ΠΎ подскаТСт, ΡΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚ΠΈΡ€ΡƒΡŽ { tA = tC*2*_k; tB = tC*3*_k; } } } } static void Main(string[] args) { ObservableCollectionEx<SortBoard> collection; collection = new ObservableCollectionEx<SortBoard>(); collection.Add(new SortBoard() { A = 10, B = 20, C = 30 }); collection.Add(new SortBoard() { A = 12, B = 18, C = 33 }); foreach (var val in collection) { Console.WriteLine(val.A); } collection.k = 3; Console.ReadKey(); } } } 

    True, I am not sure about one point that I marked with a comment, since I did not practice this before. If someone tells you how to get away from the speaker, then I will correct the answer.

    • You can show how it is implemented - Spvik
    • @Spvik, laid out the implementation, but there is 1 point at the expense of which I'm not sure ... - iluxa1810
    • About events, data comes from a remote server, when changing this coefficient, which is used not only in the collection, the data in the collection itself and me in the table should be recalculated and, accordingly, immediately reflect the changes in recalculation - Spvik