Autor Beitrag
nepleurepas
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Mo 31.03.08 21:12 
Hallo,
habe folgenden code

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
function getit(adress, username, pass: string): Tstrings;
var inhalt: Tstrings;

begin
with form1 do
 begin
 idFTP1.Username:= username;
 idFTP1.Password:= pass;
 idFTP1.Host:= adress;
 idFTP1.Connect;
 IdFTP1.List(inhalt);
 end;
result:= inhalt;

end;

procedure TForm1.Button2Click(Sender: TObject);
var name, adress, pass: string;
begin
name:= edit1.text;
adress:= edit2.Text;
pass:= edit3.Text;

listbox1.Items:= getit(adress,name,pass);

end;


dabei soll getit einfach den serverinhalt als Tsstrings liefern.
Nun kommt jedoch folgende Meldung "inhalt might not have been initialized"

Daher meine Frage. Was bedeutet das? Wann muss ich initialisieren und wann nicht? Dachte wenn ich variable mit var deklariere kann ich sie einfach verwenden?
Hoffe ihr könnt mir helfen.

Gruß Stefan
Agawain
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 460

win xp
D5, MySQL, devxpress
BeitragVerfasst: Mo 31.03.08 21:15 
hi

umgekehrt wird ja wohl eher ein Schuh draus.

Inhalt := result;

_________________
Gruß Aga
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 31.03.08 21:18 
Moin!

Nicht TStrings direkt instanziieren, sondern Descendants: z.B. TStringList! :mahn: ;)

cu
Narses

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

Win XP

BeitragVerfasst: Mo 31.03.08 21:19 
wie meinst du das? inhalt hat doch ab z.11 einen wert. Diesen möchte ich als funktionsergebnis haben. daher is

result:= inhalt

doch richtig?

Stefan
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Mo 31.03.08 21:21 
user profile iconNarses hat folgendes geschrieben:
Moin!

Nicht TStrings direkt instanziieren, sondern Descendants: z.B. TStringList! :mahn: ;)

cu
Narses


hi narses,
habe jetz inhalt und die funktion als Tstringlist deklariert. Führt aber zum gleichen meldung. "variable inhalt might not have.."

Hab ich dich richtig verstanden?
Agawain
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 460

win xp
D5, MySQL, devxpress
BeitragVerfasst: Mo 31.03.08 21:21 
oh sorry, hab da eine Zeile überlesen, nehm alles zurück.

_________________
Gruß Aga
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Mo 31.03.08 21:29 
mir wäre auch schon geholfen wenn mir jemand sagt wann man generel initialisieren muss oder mir sagt wo ich das erklärt finde.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 31.03.08 21:29 
Moin!

Probier das mal so: :idea: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function getit(adress, username, pass: string): TStringList;
begin
  Result := TStringList.Create;
  with form1 do begin
    idFTP1.Username:= username;
    idFTP1.Password:= pass;
    idFTP1.Host:= adress;
    idFTP1.Connect;
    IdFTP1.List(Result);
  end;
end;
Aber nachher nicht vergessen, die Stringliste auch wieder freizugeben! :mahn:

cu
Narses

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

Win XP

BeitragVerfasst: Mo 31.03.08 21:34 
ok danke, jetzt funktionierts.
Warum muss ich jetz indem Fall resul erst createn? Hängt das vom Variablentyp ab?

Ich mein
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function SummeAusDrei(zahl1, zahl2, zahl3: integer): integer;
  begin
    result := zahl1 + zahl2 + zahl3;
  end;

geht ja auch.

Wann/wo muss ich wieder freigeben?
thx
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 31.03.08 21:43 
Moin!

user profile iconnepleurepas hat folgendes geschrieben:
Warum muss ich jetz indem Fall resul erst createn? Hängt das vom Variablentyp ab?
Wenn an der Variable ein Objekt "hängt", dann muss dieses natürlich irgend wann mal angelegt werden (und auch irgendwann mal wieder freigegeben). :idea: Dein Beispiel arbeitet mit einem einfachen Datentyp (Integer), der kann von der CPU direkt verarbeitet werden, das ist der Unterschied.

user profile iconnepleurepas hat folgendes geschrieben:
Wann/wo muss ich wieder freigeben?
Sobald du das Objekt nicht mehr brauchst. Aber da du schon fragst, mach das lieber so, das ist weniger risikoreich: :?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure GetFtpSrvList(const adress, username, pass: String; AStrings: TStrings);
begin
  if Assigned(AStrings) then
    with form1 do begin  
      idFTP1.Username:= username;  
      idFTP1.Password:= pass;  
      idFTP1.Host:= adress;  
      idFTP1.Connect;  
      IdFTP1.List(AStrings);  
    end;  
end; 

procedure TForm1.Button2Click(Sender: TObject);  
begin  
  GetFtpSrvList(edit1.text,
                edit2.text,
                edit3.text,
                listbox1.Items);  
end;
cu
Narses

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

Win XP

BeitragVerfasst: Mo 31.03.08 21:50 
wird hier nun in zeile 3 automatisch Astrings created sobald ihm ein wert zugewiesen werden soll?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 31.03.08 21:53 
Moin!

user profile iconnepleurepas hat folgendes geschrieben:
wird hier nun in zeile 3 automatisch Astrings created sobald ihm ein wert zugewiesen werden soll?
Nein, mit Assigned prüfst du, ob der Zeiger "gültig" (naja, jedenfalls nicht NIL) ist.

AStrings ist in diesem Fall (bei deinem Aufruf) einfach ein Alias für ListBox1.Items, das ist schon der ganze "Trick". :idea:

cu
Narses

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

Win XP

BeitragVerfasst: Mo 31.03.08 21:57 
thx soweit. eine frage noch. Du hast gesagt ich soll die objekte freigeben, sobald ich sie nicht mehr brauch.(also wenn ich das so mach mit dem createn)

Aber wann soll ich sie dann in dem fall freigeben? Ich kann result ja nicht während der funktion freigeben, dann wär das ergebnis ja wieder weg. Und von einer andeeren procedure kann ich doch nicht auf das result von getit zugreifen oder?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 31.03.08 22:11 
Moin!

user profile iconnepleurepas hat folgendes geschrieben:
Du hast gesagt ich soll die objekte freigeben, sobald ich sie nicht mehr brauch.(also wenn ich das so mach mit dem createn)

Aber wann soll ich sie dann in dem fall freigeben? Ich kann result ja nicht während der funktion freigeben, dann wär das ergebnis ja wieder weg. Und von einer andeeren procedure kann ich doch nicht auf das result von getit zugreifen oder?
Du solltest dich am besten von deiner getit-Funktion verabschieden, das ist kein guter Programmierstil (so wie da verwendet). :? :| Aber um die Frage zu beantworten: du brauchst noch eine weitere Variable, die das Funktionsergebnis aufnimmt (in diesem Fall den Zeiger auf die Stringliste). Darüber gibst du das dann frei. :idea: Und um die Probleme auch direkt noch anzusprechen: was ist, wenn ein Fehler in getit auftritt? :hair:

cu
Narses

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

Win XP

BeitragVerfasst: Di 01.04.08 10:36 
hmm ok, muss wohl noch viel grundlegendes lernen...
falls du lust hast mich noch weiter zu belehren, hätt ich auf jeden fall nochne frage.

wie meinst du das mit der weiteren variablen? wie würde das funktionieren? due funktion liefert doch immer nur result zurück? oder muss result von einem typ sein, der nicht created werden muss?
wär über paar zeilen code echt dankbar. Auch wenn die getit funktion grundlegend keinem guten stil entspricht, würde es mir helfen, die problematik mit zeigern, createn und freigegen besser zu verstehen.

auf jeden fall mal danke soweit
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 01.04.08 11:08 
Moin!

user profile iconnepleurepas hat folgendes geschrieben:
falls du lust hast mich noch weiter zu belehren,
:P Der war gut. ;)

user profile iconnepleurepas hat folgendes geschrieben:
wie meinst du das mit der weiteren variablen? wie würde das funktionieren?
OK, da du ja nicht locker lassen willst :roll: ;) (oder eher nicht deinen Ansatz aufgeben willst :zwinker:), hier also noch die "andere" Variante (wie schon gesagt, was ist, wenn da ein Fehler auftritt, wie willst du das vernünftig kapseln?):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
function getit(const adress, username, pass: string): TStringList;
begin
  Result := TStringList.Create;
  with form1 do begin
    idFTP1.Username:= username;
    idFTP1.Password:= pass;
    idFTP1.Host:= adress;
    idFTP1.Connect;
    IdFTP1.List(Result);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
  var
    SL: TStringList;
begin
  SL := getit(edit1.text,edit1.text,edit1.text);
  listbox1.Items.Assign(SL);
  SL.Free;
end;

cu
Narses

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


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Di 01.04.08 11:34 
user profile iconNarses hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure GetFtpSrvList(const adress, username, pass: String; AStrings: TStrings);
begin
  if Assigned(AStrings) then
    with form1 do begin  
      idFTP1.Username:= username;  
      idFTP1.Password:= pass;  
      idFTP1.Host:= adress;  
      idFTP1.Connect;  
      IdFTP1.List(AStrings);  
    end;  
end; 

procedure TForm1.Button2Click(Sender: TObject);  
begin  
  GetFtpSrvList(edit1.text,
                edit2.text,
                edit3.text,
                listbox1.Items);  
end;
cu
Narses

Wieso muss hier eigentlich kein var vor AStrings:TSTrings?

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 01.04.08 11:54 
Moin!

user profile iconklezmor hat folgendes geschrieben:
Wieso muss hier eigentlich kein var vor AStrings:TSTrings?
Weil du so nur den Zeiger kopierst, eh nicht das Objekt :idea: (kann der Compiler dann aber meistens schon in einem Register unterbringen und muss nicht mit Indexarithmetik arbeiten (aber das weiss BenBE eh alles besser als ich)) ;)

cu
Narses

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


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Di 01.04.08 13:04 
user profile iconNarses hat folgendes geschrieben:
Moin!
Weil du so nur den Zeiger kopierst, eh nicht das Objekt :idea: (kann der Compiler dann aber meistens schon in einem Register unterbringen und muss nicht mit Indexarithmetik arbeiten (aber das weiss BenBE eh alles besser als ich)) ;)

cu
Narses

Aber ich dachte man möchte gerade mit dem Zeiger des objekts arbeiten und nicht dem objekt selbst, was ist der nachteil dabei?

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Di 01.04.08 13:36 
user profile iconNarses hat folgendes geschrieben:
Moin!


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
function getit(const adress, username, pass: string): TStringList;
begin
  Result := TStringList.Create;
  with form1 do begin
    idFTP1.Username:= username;
    idFTP1.Password:= pass;
    idFTP1.Host:= adress;
    idFTP1.Connect;
    IdFTP1.List(Result);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
  var
    SL: TStringList;
begin
  SL := getit(edit1.text,edit1.text,edit1.text);
  listbox1.Items.Assign(SL);
  SL.Free;
end;

cu
Narses


an welcher stelle wird hier jetzt result wieder freigegeben? gescheiht das mit der freigabe bon SL?
Und warum muss ich SL nicht createn? Is doch der gleiche datentyp wie result.
Warum machst du listbox.items.assign(SL) anstatt listbox.items:= SL?

zu meinem ansatz, da kann ich wohl nich viel abkapseln, wobei ich nichmal genau weißt was du damit meinst ;-). meinst du, dafür zu sorgen, dass das programm nich abstüzt, wenn bsp. die zugansdaten falsch sin? Das würde mich in der tat interessieren, wie man erreciht, dass das programm nich gleich abstürzt ,wenn man den name falsch eingegebn hat, sondern dann eher eher ne showmessage mit entsprechender mitteilung kommt.

Also wenn du zufällig grad n guten ansatz auf lagen habe solltest, was ja sein könnte ;-), her damit :-)

thx


Oje, ich glaub meine Fragensdurst ist noch lange nicht gestillt.. ;-) ,
thx