Der Versuch, eine Funktion in C# mithilfe einer generischen Ausführungshilfsfunktion aufzurufen, die abhängig vom ErgebnC#

Ein Treffpunkt für C#-Programmierer
Guest
 Der Versuch, eine Funktion in C# mithilfe einer generischen Ausführungshilfsfunktion aufzurufen, die abhängig vom Ergebn

Post by Guest »

Code: Select all

public class ExecutionUtility
{
public static TIn2 ExecutionHelper(Func function, TIn1 input)
{
TIn2 output = default(TIn2);
int result = function(input, out output); // the out keyworkd is not accepted apparently

if (result == 0)
{
return output;
}
else
{
throw new FunctionExecutionException(result);
}
}

}
Nur ein Ausnahmecode:

Code: Select all

public class FunctionExecutionException : Exception
{
public int ErrorCode { get; }

public FunctionExecutionException(int errorCode)
: base($"Function execution failed with error code: {errorCode}")
{
ErrorCode = errorCode;
}
}
Ein Beispiel für eine statische Funktion, die ein int-Ergebnis und auch einen String-Ausgabeparameter zurückgibt

Code: Select all

public static int myfunction(int input, out string output)
{
if (input > 0)
{
output = "OUTPUT_VALUE_1";
return 0;
}
else
{
output = "OUTPUT_VALUE_2";
return -1;
}
}
Jetzt die Execution Helper-Funktion verwenden. Aus irgendeinem Grund scheint der Out-Parameter nicht autorisiert zu sein und mein Code lässt sich nicht kompilieren.

Code: Select all

return ExecutionUtility.API_VERIFY(PRD_GCAPI.myfunction, this.Index);

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post