Skip to content

Commit

Permalink
ExDM- Harmonize the code with the 01-DelegateEventsScript.md document #…
Browse files Browse the repository at this point in the history
…404

- Updated sample code for event.
- UT 👍
  • Loading branch information
mpostol committed Sep 30, 2024
1 parent 80f89db commit 3e183a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void EventTestMethod()
_newInstance.CurrentStateHandler.GoToIdle();
Assert.AreEqual<State>(State.Idle, _currentState);
//_newInstance.OnStateChanged(_newInstance, _newInstance.CurrentStateHandler.CurrentState); //Error CS0070 The event 'AnonymousFunctions.OnStateChanged' can only appear on the left hand side of += or -= (except when used from within the type 'AnonymousFunctions')
//_newInstance.OnStateChanged = null; //Error CS0070 The event 'AnonymousFunctions.OnStateChanged' can only appear on the left hand side of += or -= (except when used from within the type 'AnonymousFunctions')
}

#region Instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//_____________________________________________________________________________________________________________________________________

using System;
using System.ComponentModel;
using System.Diagnostics;

namespace TP.FunctionalProgramming
Expand All @@ -35,13 +36,13 @@ public class AnonymousFunctions
internal delegate void CallBackTestDelegate(bool testResult);

/// <summary>
/// Invoking this method the consistency of this state machine is checked.
/// The result is transferred to the user through the <paramref name="testResult"/> delegate pointing
/// out a method invoked to transfer the check result. While raising this delegate variable
/// the null-conditional operator is not applied. Hence, this argument must not be null to prevent
/// Invoking this method the consistency of this state machine is checked.
/// The result is transferred to the user through the <paramref name="testResult"/> delegate pointing
/// out a method invoked to transfer the check result. While raising this delegate variable
/// the null-conditional operator is not applied. Hence, this argument must not be null to prevent
/// throwing an exception.
/// </summary>
/// <param name="testResult">This parameter contains a delegate value pointing out a method invoked
/// <param name="testResult">This parameter contains a delegate value pointing out a method invoked
/// to transfer the check result. This argument must not be null to prevent throwing an exception.
/// </param>
/// <exception cref="NullReferenceException">exception thrown if the <paramref name="testResult"/> evaluates to null.
Expand All @@ -63,19 +64,27 @@ public AnonymousFunctions()

public IStateHandler CurrentStateHandler { get; private set; }

public event EventHandler<State> OnStateChanged;
/// <summary>
/// event declarations sample code
/// </summary>
public event EventHandler<State> OnStateChanged = null;

#endregion state machine context

#region states implementation

private abstract class StateHandlerBase : IStateHandler
{
/// <summary>
/// constructor of the <see cref="StateHandlerBase"/>
/// </summary>
/// <param name="context">source of event notification</param>
public StateHandlerBase(AnonymousFunctions context)
{
m_Context = context;
m_Context.CurrentStateHandler = this;
m_Context.OnStateChanged?.Invoke(context, CurrentState);
m_Context.OnStateChanged?.Invoke(context, CurrentState); //Invocation of all the methods added to OnStateChanged, if any,
//CurrentState contains the event data transfered to users.
}

public abstract State CurrentState { get; }
Expand Down

0 comments on commit 3e183a9

Please sign in to comment.