Autor Beitrag
JRegier
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: Fr 08.07.05 10:41 
Gibts in Delphi diesen Ternären Operator wie in Java?

Also:

Bedingung ? mach das : ansonsten mach das
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 08.07.05 10:50 
Meinst du sowas:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if Bedingung then
  machwas
else
  mach was anderes;

Du musst nur das ? durch ein then und das : durch ein else ersetzen.

_________________
We are, we were and will not be.
JRegier Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: Fr 08.07.05 11:07 
user profile iconGausi hat folgendes geschrieben:
Meinst du sowas:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if Bedingung then
  machwas
else
  mach was anderes;

Du musst nur das ? durch ein then und das : durch ein else ersetzen.


Na ja aber ein if muß doch davor?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 08.07.05 11:25 
Der ternäre Operator leistet ja auch mehr, als nur eine Anweisung auszuführen. Er gibt ja auch noch einen Wert zurück, da wird die entsprechende if-Anweisung noch ein wenig größer.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
JRegier Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: Fr 08.07.05 12:25 
user profile iconChristian S. hat folgendes geschrieben:
Der ternäre Operator leistet ja auch mehr, als nur eine Anweisung auszuführen. Er gibt ja auch noch einen Wert zurück, da wird die entsprechende if-Anweisung noch ein wenig größer.


Was den jetzt gibts in Delphi der Ternäre Operator oder nicht?
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Fr 08.07.05 12:40 
Ich hab kA, was ein ternärer Operator sein soll, aber es gibt auch noch case

AXMD
ManuelGS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: Fr 08.07.05 12:58 
Wäre echt cool, wenn da jemand Licht ins Dunkel bringen könnte.
Ich frage mich auch schon länger, was Anweisungen wie funktion1 xor funktion2; bedeuten sollen!?

_________________
"Leben ist gänzlich Bühne und Spiel; so lerne denn spielen
und entsage dem Ernst - oder erdulde das Leid." - Palladas von Alexandria
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 08.07.05 13:13 
Ternäre Operatoren sind Operatoren, die 3 Argumente benötigen (sagt ja schon der Name). Verwendet wird das z.B. um Inline-If-Abfragen zu realisieren:

ausblenden PHP-Source:
1:
$ergebnis = 'Das Ergebnis ist ' . ($resultval (*!=0*) ? 'ungleich' : 'gleich') . ' null.';					


In Delphi säh der gleiche Source etwas umfangreicher aus:

ausblenden Delphi-Source für <=D6:
1:
2:
3:
4:
If ResultVal <> 0 Then
    Ergebnis := 'Das Ergebnis ist ungleich null.'
else
    Ergebnis := 'Das Ergebnis ist gleich null.';


Ab Delphi 7 gibt es dafür aber eine extra Funktion

ausblenden Delphi-Source für >=D7:
1:
Ergebnis := 'Das Ergebnis ist ' + IfThen(ResultVal <> 0'ungleich''gleich') + ' null.';					


HTH.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.