Autor Beitrag
delphijanka
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Do 15.04.10 18:21 
ich habe:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
Func(const OldArray: array of String);
var NewArray: array of String;
begin
...

was ich möchte:
ausblenden Delphi-Quelltext
1:
NewArray := OldArray;					

dies geht aber nicht. Brauche Hilfe, wie ich dies schaffen kann.

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Do 15.04.10 18:37 
Am einfachsten wäre glaube ich sowas:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var
  i: Integer;
begin
  for i := 0 to Length(OldArray) - 1 do
    NewArray[i] := OldArray[i]
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 15.04.10 19:15 
Moin!

user profile icondelphijanka hat folgendes geschrieben Zum zitierten Posting springen:
was ich möchte:
ausblenden Delphi-Quelltext
1:
NewArray := OldArray;					
Was man so nicht sehen kann: möchtest du denn eine Kopie der Werte anlegen oder die Referenz duplizieren? :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Do 15.04.10 19:34 
Soll doch die Compiler-Magic entscheiden :D

Es geht, wenn du dir einen Typ TStringArray oder so definierst, und beide Variablen von diesem Typ sind.

Sonst denkt der Compiler, die wären unterschiedlich und meckert.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
thepaine91
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Fr 16.04.10 07:32 
Da es ja ein Dyn Array ist muss er da nicht erst mal
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
setlength(NewArray, length(Oldarray));
// falsch
Newarray := oldarray;
// richtig
  for i := 0 to high(oldarray) do
  begin
    Newarray[i] := oldarray[i];
  end;

Edit: Quelltext angepasst.


Zuletzt bearbeitet von thepaine91 am Fr 16.04.10 09:26, insgesamt 1-mal bearbeitet
delphijanka Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Fr 16.04.10 09:07 
@Narses

wie geht es mit Referenz?
Gammatester
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 328
Erhaltene Danke: 101



BeitragVerfasst: Fr 16.04.10 09:13 
user profile icondelphijanka hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
Func(const OldArray: array of String);					
OldArray ist kein dynamisches Array sondern ein "Open Array Parameter". Um es dynamisch zu machen, must Du wie Martok schon angdeutet hat, einen separaten Typ type TStringArray = array of String; definieren und OldArray und NewArray damit deklarieren.
delphijanka Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Fr 16.04.10 09:18 
@Gammatester

ich habe bereits mehrere andere Funktionen deklariert, wo die Übergabearrays "array of String" sind.
hier möchte ich den globalen <NewArray> an diese Funktionen übergeben.
sollte sich der Typ auf TStringArray geändert haben, muss ich dies in allen Funktionen anpassen, und das ist für mich ein grosser Aufwand.
thepaine91
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Fr 16.04.10 09:23 
Wie Jakob_Ullman schon im 2. Post ähnlich schrieb wäre es hiermit getan:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  setlength(Newarray, length(Oldarray));
  for i := 0 to high(oldarray) do
  begin
    Newarray[i] := oldarray[i];
  end;

Dann musst du auch in keiner anderen Funktion/Prozedur etwas ändern.
delphijanka Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Fr 16.04.10 11:23 
@thepaine91

Ich möchte bei dieser Lösung ein anderes Problem schildern. Ich brauche dieses "Kopieren" von Arrays an ziemlich mehreren Stellen.

Ich habe mir dann gleich gedacht, dafür eine Prozedur zu schreiben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure KopiereArray(const VonArray: array of Stringvar ToArray: array of String);
var Index: Word;
begin
//Speicher:
SetLength(ToArray, Length(VonArray));

//Der Kopiervorgang:
from Index := 0 to ...

Das Problem bei dieser Lösung ist jedoch, dass ich den SetLength auf ToArray nicht anwenden kann.

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
Gammatester
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 328
Erhaltene Danke: 101



BeitragVerfasst: Fr 16.04.10 12:02 
Ich kann nur wiederholen, daß das keine dynamischen Arrays sind, sondern Open Array Parameter. Das sind fixe Arrays! Der Unterschied liegt darin, daß Arrays unterschiedlicher Länge übergehen werden können, und die High()-Funktion liefert die aktuelle Länge. Das es nix Dynmisches gibt, kann selbstverständlich die Länge nicht geändert werden.
delphijanka Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Fr 16.04.10 12:17 
@Gammatester:

wenn ich ein Array übergebe, übergebe ich damit die Anfangsadresse.
wie kann ich diese Adresse an "ToArray" zuweisen? (Referenz)
Gammatester
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 328
Erhaltene Danke: 101



BeitragVerfasst: Fr 16.04.10 12:39 
user profile icondelphijanka hat folgendes geschrieben Zum zitierten Posting springen:
@Gammatester:

wenn ich ein Array übergebe, übergebe ich damit die Anfangsadresse.
wie kann ich diese Adresse an "ToArray" zuweisen? (Referenz)

Wahrscheinlich gar nicht. Eine mögliche (relativ unschöne) Lösung für Dein Problem wäre ein Copy-Funktion, etwa so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
type
  TArrayOfString = array of string;

function ArrayCopy(const OldArray: array of string): TArrayOfString;
var
  i: integer;
begin
  setlength(Result, length(Oldarray));
  for i:=0 to high(OldArray) do Result[i] := oldarray[i];
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x,y: array of string;
begin
  setlength(x,2);
  x[0] := '0000000000';
  x[1] := '11111111111111';
  TArrayOfString(y) := ArrayCopy(x);  //Hier wird kopiert
  Button1.Caption := y[1];
end;

Den Typecast TArrayOfString() wirst Du nicht los, da Delphi zwei verschiedene "Array of string"-Deklarationen als zwei verschiedene Typen behandelt.