From f46a821f2b7acfe4118acaf4d132d160522729c6 Mon Sep 17 00:00:00 2001 From: JakubKorytko Date: Wed, 15 Nov 2023 12:52:23 +0100 Subject: [PATCH] Add algorithm & shared method examples --- AlgorithmsLibrary.csproj | 1 + Components/Algorithms/Algorithm.example.cs | 39 +++++++++++++++++++++ Components/AlgorithmsCore/BaseClassTools.cs | 7 ++++ 3 files changed, 47 insertions(+) create mode 100644 Components/Algorithms/Algorithm.example.cs diff --git a/AlgorithmsLibrary.csproj b/AlgorithmsLibrary.csproj index 6d7838b..a70e029 100644 --- a/AlgorithmsLibrary.csproj +++ b/AlgorithmsLibrary.csproj @@ -49,6 +49,7 @@ + diff --git a/Components/Algorithms/Algorithm.example.cs b/Components/Algorithms/Algorithm.example.cs new file mode 100644 index 0000000..fada9c1 --- /dev/null +++ b/Components/Algorithms/Algorithm.example.cs @@ -0,0 +1,39 @@ +using System; + +// include this +using AlgorithmsLibrary.AlgorithmsCore; + +namespace AlgorithmsLibrary.Algorithms +{ + + // derive from the "Algorithm" class + internal class SuperAlgorithm : Algorithm + { + // private field, you can add as many as you want + private string exclamationMark; + + // override description to be displayed in the menu + // (optional) + public override string Description { get { return "I have the description!"; } } + + // private method, you can add as many as you want + private string GetMessage(string text) + { + return "Im the algorithm! " + + text + + exclamationMark + + " 1 + 1 = " + + // method defined in the AlgorithmsCore/BaseClassTools.cs + OnePlusOne(); + } + + // ovverride Display method to handle algorithm output + // (optional, but required for functionality) + public override void Display() + { + exclamationMark = "!"; + + Console.Write(GetMessage("Hooray")); + } + } +} diff --git a/Components/AlgorithmsCore/BaseClassTools.cs b/Components/AlgorithmsCore/BaseClassTools.cs index ce465d1..cbd61e6 100644 --- a/Components/AlgorithmsCore/BaseClassTools.cs +++ b/Components/AlgorithmsCore/BaseClassTools.cs @@ -31,5 +31,12 @@ protected bool IntParseTestWithOutput(string num) return false; } } + + // example of the method to use across algorithms + // you can use any return type or method name + protected int OnePlusOne() + { + return 1 + 1; + } } }