Autor Beitrag
SteheAufDerLeitung
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 104



BeitragVerfasst: Do 18.10.07 10:18 
Hallo!

Ich möchte eine Funktion schreiben, die unter Anderem zwei Strings ermittelt und diese übergibt. Gibts denn ne Möglichkeit zwei "Results" zu übergeben? Wenn ja, was muss ich dann zu ner Funktion mit einem Rückgabewert hinzufügen, ändern, ..?!

Beste Grüße und Dank

Sebastian


Moderiert von user profile iconNarses: Topic aus VisualCLX (Component Library for Cross Platform) verschoben am Do 18.10.2007 um 11:21
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 18.10.07 10:24 
Moin!

Du musst in diesem Fall vorher einen entsprechenden Typ definieren und diesen als Rückgabewert(typ) verwenden:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type
  T2Strings = record
    s1,s2: String;
  end;
// oder
  T2Strings = array[0..1] of String;
// oder
  T2Strings = array[Boolean] of String;

  function DoSomething: T2Strings;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Alpha_Wolf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 297

Ubuntu, Win XP, Win Vista
C#, Delphi 6 Prof, Delphi 2007 Prof, Java
BeitragVerfasst: Do 18.10.07 10:24 
Du kannst auch mit Var Parametern arbeiten...

ausblenden Delphi-Quelltext
1:
function Get2Things(var aFirst: string; var aSecond: string): boolean;					


Was möglich sein könnte wäre auch als result einen Pointer auf einen Record zu übergeben (habe ich allerdings noch nie ausprobiert.. wofür gibts di eVar Parameter)

_________________
Diskutiere nie mit einem Irren - er zieht dich auf sein Niveau und schlägt dich mit seiner Erfahrung.
SteheAufDerLeitung Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 104



BeitragVerfasst: Do 18.10.07 10:44 
soo..

hab nun nen Typen t2Strings. Aber wie beschreibe ich denn in meiner Funktion nun die array-Felder?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
function blubb():t2Strings;
begin
  
  t2Strings(0) := 0; //bringt: ungueltige Typumwandlung
  t2Strings[0] := 0; // auch..
..
..
end;


Wisst ihr weiter? Oder muss ich erst ne Variable vom Typ t2Strings erzeugen? Und das noch vor der Typ-Deklaration?

Dank euch..

Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 18.10.07 10:48 
Probier mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function blubb():t2Strings; 
begin 
  result[0] := 'Hallo'; 
  result[1] := 'Welt'; 
end;

_________________
We are, we were and will not be.
SteheAufDerLeitung Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 104



BeitragVerfasst: Do 18.10.07 10:50 
Habs auch grad eben rausbekommen :-D Danke vielmals!!
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Do 18.10.07 10:52 
Mache doch einfach das Vorgeschlagene mit den Parametern und statt Funktion einfach Prozedur.

_________________
Gruß
Hansa
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: So 21.10.07 17:54 
Ansonsten lohnt sich auch ein Blick zum Schlüsselwort Out; das funktioniert genau wie var, teilt aber mit, dass es sich dabei um einen Output-Only-Parameter handelt; also kein Wert an die Funktion übergeben wird.

_________________
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.
Fabian E.
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 554

Windows 7 Ultimate
Visual Studio 2008 Pro, Visual Studion 2010 Ultimate
BeitragVerfasst: So 21.10.07 20:34 
user profile iconBenBE hat folgendes geschrieben:
Ansonsten lohnt sich auch ein Blick zum Schlüsselwort Out; das funktioniert genau wie var, teilt aber mit, dass es sich dabei um einen Output-Only-Parameter handelt; also kein Wert an die Funktion übergeben wird.


und was genau hat das dann für einen vorteil gegenüber "var"?

gruß
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: So 21.10.07 20:42 
Einen ganz wichtigen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
Procedure Foo;
var
    Tmp1, Tmp2: Integer;
begin
    FuncVar(Tmp1, Tmp2); // <-- Uninitialized-Warnung vom Compiler
end;


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
Procedure Foo;
var
    Tmp1, Tmp2: Integer;
begin
    FuncOut(Tmp1, Tmp2); // <-- Keine Warnung vom Compiler
end;


Was ist der Vorteil: Der Compiler weiß zu jedem Zeitpunkt mit Out, was man machen will UND, dass keine Initialisierung benötigt wird. Damit wird die Warnung vermieden UND jemand, der deinen Source warten muss, weiß, dass es sich hierbei um einen Result-Wert handelt. Du erleichterst damit den Leuten das Lesen deines Codes ...

BTW: Intern ist eine Function auch nix anderes als ne Procedure mit einem Out-Parameter, der explizit nach EAX gemappt wird.

_________________
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.