xsharp.eu • Indirekter Function-Aufruf über einen Delegaten - Page 2
Page 2 of 2

Indirekter Function-Aufruf über einen Delegaten

Posted: Fri Mar 01, 2019 8:44 am
by lumberjack
Peter,
pemo18 wrote:Thank you everyone.
This is the shortest (and still readable) version I came up with:
Nice one, will definitely go into my examples box.
If XSharp is as close to C# as I assume I probably don't have to use a delegate.
No you don't, but using a delegate can shorten some very complex if statements to a one liner, in any language that understand delegates.
Here is your example modified to not use a delegate.

Code: Select all

USING System

BEGIN NAMESPACE IndirekterAufruf

FUNCTION Start() AS VOID STRICT
    VAR funArr := <STRING>{"Cloudy day ahead", ;
                           "Sunny the whole day", "Rain, rain and even more rain" }
    LOCAL z := Random{}:@@Next(1,funArr:Length + 1) as int // @@Next(0, funArr:Length) if /az set
    Console.WriteLine("The weather forecast for today: {0}", funArr[z]())
I even found a translation class ;)
Me too:

Code: Select all

DELEGATE Translate(AnyString AS STRING) AS STRING

FUNCTION Start() AS VOID
    LOCAL lang AS Translate
    lang := English
    ? lang("Ich liebe dich") // I love you
    ? lang("Je t'aime") // I love you
:)