Indirekter Function-Aufruf über einen Delegaten

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Indirekter Function-Aufruf über einen Delegaten

Post 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
:)
______________________
Johan Nel
Boshof, South Africa
Post Reply