How to set counter in main flow from called delegate function

Suraj 1 Reputation point
2021-04-02T16:01:17.01+00:00

Hi,

Am having some trouble getting this going - here's the code. Need the delegate function call triggered in StartLockDown() to be able to set the lockDownStartedCount in the code below. Not sure if this is possible. Thought to reach out.
[Test]
public void LockDownMethods_RaisingTheEvents()
{
int lockDownStartedCount = 0, lockDownEndedCount = 0;

        _lockDownManager.LockDownEnded += (sender, args) => lockDownEndedCount++;
        _lockDownManager.LockDownStarted += (sender, args) => lockDownStartedCount++;


        Assert.AreEqual(0, lockDownStartedCount);
        Assert.AreEqual(0, lockDownEndedCount);



        _lockDownManager.StartLockDown();
        Assert.AreEqual(1, lockDownStartedCount);
        Assert.AreEqual(0, lockDownEndedCount);


        _lockDownManager.StartLockDown();
        Assert.AreEqual(1, lockDownStartedCount);
        Assert.AreEqual(0, lockDownEndedCount);


        _lockDownManager.EndLockDown();
        Assert.AreEqual(1, lockDownStartedCount);
        Assert.AreEqual(1, lockDownEndedCount);

        _lockDownManager.EndLockDown();
        Assert.AreEqual(1, lockDownStartedCount);
        Assert.AreEqual(1, lockDownEndedCount);

        _lockDownManager.StartLockDown();
        Assert.AreEqual(2, lockDownStartedCount);
        Assert.AreEqual(1, lockDownEndedCount);
    }

Currently - I have the StartLockDown method in lockdownmanager method as

public void StartLockDown()
{

          LockDownStarted = setCounter();
        LockDownStarted.Invoke(this, EventArgs.Empty); // think something needs to change here - like pass the main counter value to setCounter - I tried deriving from EventArgs and adding an int field, that didn't work either
      }

public virtual void setCounter(object sender, EventArgs e)
//{
// this method is incorrect, something needs to happen here...
// Console.Write(" in setAccountLockdown----start####");

    //    //if (e.counter == 0)
    //    //    e.counter = 1;


    //}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,260 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Suraj 1 Reputation point
    2021-04-02T21:25:26.207+00:00

    Hi Karen,

    Thank you for your response. I can see your solution working too. And you're right, it requires the Whatever class being passed from the main calling program and using this to set sender.startLockDown = <whatever value we want to>

    and hopefully this should reflect in the main code.

    I haven't tested this yet. There are a bunch of constraints that am forced to get the solution for. My Handler is a no param Event Handler so am not sure how this would work without a param for eventdata being passed to the delegate.

    Maybe the question is flawed. I don't know. I was given this as a part of a test - maybe it does requires a parameter to be passed to the EventHandler/delegate.

    Good to know though. This is new for me and I am learning reading all the comments on here.

    Thank you all,
    Suraj