E
E
embiid2020-12-22 12:19:26
C++ / C#
embiid, 2020-12-22 12:19:26

How to iterate an array of delegates through an array of class objects?

It is necessary for an array of delegates to go through each student.

public delegate void DelegateAction();
  struct Student {
    int _Energy;
    double _AverageMark;
    string _Name, _Surname; 

    public string Name {
      get => _Name;
      set => _Name = value;
    }

    public string Surname {
      get => _Surname;
      set => _Surname = value;
    }

    public int Energy {
      get => _Energy;
      set => _Energy = value;
    }

    public double AverageMark {
      get => _AverageMark;
      set => _AverageMark = value;
    }

    public Student(string Name, string Surname, int Energy, double AverageMark) : this() {
      this.Name = Name;
      this.Surname = Surname;
      this.Energy = Energy;
      this.AverageMark = AverageMark;
    }

    public void Wash() { }
    public void Feed() { ++this.Energy; }
    public void PutSleep() { ++this.Energy; }
    public void Grant() {
      if (this.AverageMark > 4.5)
        Console.WriteLine("{0}, you've just got a grant", this.Name + " " + this.Surname);
    }
  }


class Program {
    static void Main() {
      Student[] university = new Student[]{
        new Student("Kyle", "Kyzma", 90, 4.5),
        new Student("Kelly", "Oubre", 100, 4.7),
        new Student("Andrew", "Wiggins", 95, 3.2),
      };

      //DelegateAction[] studentAction = new DelegateAction[] {
      //	university[0].Wash,
      //	university[0].Feed,
      //	university[0].PutSleep,
      //	university[0].Grant,

      //	university[1].Wash,
      //	university[1].Feed,
      //	university[1].PutSleep,
      //	university[1].Grant,

      //	university[2].Wash,
      //	university[2].Feed,
      //	university[2].PutSleep,
      //	university[2].Grant,
      //};

      DelegateAction[] studentAction = new DelegateAction[4];

      for (int counterDelegates = 0; counterDelegates < studentAction.Length; counterDelegates++) {
        for (int counterInstances = 0; counterInstances < university.Length; counterInstances++) {
          studentAction[counterDelegates] = new DelegateAction(university[counterInstances].Feed).Invoke;
          studentAction[counterDelegates] = new DelegateAction(university[counterInstances].Wash).Invoke;
          studentAction[counterDelegates] = new DelegateAction(university[counterInstances].Grant).Invoke;
          studentAction[counterDelegates] = new DelegateAction(university[counterInstances].PutSleep).Invoke;
        }
      }
    }
  }


Tell me, how correct is this? I have doubts about this. I don't like the cycle. Delegates are not invoked. Is it possible, in general, to somehow shorten the method call loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-12-22
@embiid

Cycle yes - game. I would write something like

var studentActions = students.Select(student =>
    () =>
    {
      student.Feed();
      student.Wash();
      student.Grant();
      student.PutSleep();
    })
  .ToArray();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question