Autor Beitrag
mcst09
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Fr 02.06.06 22:09 
Hallo,

ich habe folgendes Problem:
Von allen Records eines Grids sollen pro Record mehrere Werte (strings) zwischengespeichert werden, um in einem Dialog wieder abgerufen zu werden.

Beispiel:
Es sollen die ID, der Artikel und ein Pfad (alle drei Werte sind im Record) pro Record eingelesen werden und im Dialog wieder abgefragt werden in einer Schleife um dort was zu füllen.

Erst hatte ich nur zwei Werte, das habe ich dann mit einer TStringList gemacht (Name "ID" = Value "Artikel "). Lief auch wunderbar. Nun habe ich aber drei Felder (zusätzlich noch den Pfad). Habe das erst mit zwei TStringLists gemacht. Läuft aber ned so toll.

Wie kann ich das noch machen?
Nochnmal:
Es sollen in einer "Liste" pro Record drei Felder gespeichert werden (alle stings).
Die Felder möchte ich später wieder anhand der ID auslesen.

Axl
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 02.06.06 22:48 
Warum kein dynamisches Array?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
type
  blubb = array of record
     id, value : integer;
     pfad : string;
  end;

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Sa 03.06.06 00:01 
Hi,

danke für deinen vorschlag.
hmm, also einen array mit drei string.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
type  
  blubb = array of record  
     id : string;
     value : string;  
     pfad : string;  
  end;


wie kannich dann auf die einzelnen strings zugreifen??
FrankLink
Hält's aus hier
Beiträge: 8

Win XP, Win 2003, LINUX
Delphi 2005 Arch.; JAVA, C#,...
BeitragVerfasst: Sa 03.06.06 09:02 
Hallo Axl,
generell hast Du zwei Möglichkeiten Dein Problem zu lösen,

1. Du verwendest das array of record wie Marco es beschrieben hat.

Der Zugriff auf ein solches Array ist denkbar einfach <Name des array>[1].<Variablenname>. Das array hat aus meiner Sicht einen Nachteil, ich kann nach der Id nur sehr umständlich suchen.

2. Du verwendest eine Kombination StringList und einer zusätzlichen Klasse. In der Klasse legst Du Deine zu verwaltenden Daten ab (hat den Vorteil, dass Du jederzeit weitere Daten in die Klasse eintragen und über die Stringliste verwalten kannst). Die INstanzen der Klasse verwaltest Du in der StringListe über die ID.

Beispiel:

ausblenden volle Höhe 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:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
type

   TListItem = class
   private
   public
      Id        : Integer;     
      Variable1 : String;
      Variable2 : String;
   end;

var
    List : TStringList;
    Item : ListItem;
    id   : Integer;

begin
    List := TStringList.Create;
    // In die Liste einhängen
    for id := 0 to 10 do
    begin
        Item := TListItem.Create;
        Item.Id := Id;
        Item.Variable1 := 'Variable1 ' + Item.Id;
        Item.Variable2 := 'Variable2 ' + Item.Id;
        List.AddObject( IntToStr( Id ), Item );
    end;

    // aus der Liste holen
    for i := 0 to 10 do
    begin
        Item := List.Objects[i] as TListItem;
        Item.Variable1 := ....
    end;

    // In der Liste nach einer Id suchen
    i := List.IndexOf('5')
    if i >= 0 then
    begin
        Item := List.Objects[i] as TListItem;
    end;
end;


Über dieses Beispiel kannst Du z.B. sehr einfach prüfen, ob eine bestimmte Id bereits vorhanden ist, oder Änderungen an einem bestimmten Eintrag vornehmen ohne erst dass ganze Array nach dem Element zu durchsuchen.

Ich hoffe das Beispiel hilft Dir bei Deinem Problem.

Gruss
Frank

_________________
Ein fehlerfreies Programm ist ein Novum
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Sa 03.06.06 15:50 
Hallo,

danke für die Hilfe.
Habe jetz mal das mit der extra Klasse ausprobiert, leider funktioniert das aber nicht bei mir. Habe folgendes gemacht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
type
TCutListItem = class
   private
   public
      CutNo   : String;
      CutPath : String;
      CutSize : String;
   end;

var
  lItem : TCutListItem;

begin

lItem := sPathList.Objects[i] as TCutListItem;


sPathList ist die StringList, die auch wunderbar gefüllt wird (in einer anderen Funktion).
Die Stringlist wird an obige Funktion übergeben.
Funktioniert alles, bis auf die oben genannte Stelle.
Er bringt immer die Fehlermeldung "Ungültige Typenumwandlung".
Du hast es doch aber auch so gemacht, oder??

Axl
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 03.06.06 16:09 
Hast du auch den Constructor von lItem aufgerufen?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Sa 03.06.06 17:08 
Hallo,

hmm, den Constructor??
Nein habe ich ned. Muss ich das machen??
Warum und wie muss ich das machen??
Wofür wird der hier gebraucht??


Axl
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 03.06.06 17:28 
ausblenden Delphi-Quelltext
1:
lItem:=TCutListItem.create;					

Er erschafft sozusagen lItem.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Sa 03.06.06 19:57 
Hi,

also ich habe das jetz erzeugt, aber trotzdem kommt die Fehlermeldung "Ungültige Typumwandlung".

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TShowCutImages.Init(sPathList: TStringList; sReadOnly: boolean);
var
  i : integer;
  lCutName : string;
  lFilePath : string;
  lCutSize : string;
  lItem : TCutListItem;

begin
  try
    lItem := TCutListItem.Create;
    
    if Assigned(sPathList) then begin
      for i := 0 to sPathList.Count -1 do begin
        lItem := sPathList.Objects[i] as TCutListItem;  //<-- Fehlermeldung
        lCutName  := lItem.CutNo;
        lFilePath := lItem.CutPath;
        lCutSize  := lItem.CutSize;
end;


Woran kann das liegen??
Axl
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Sa 03.06.06 21:44 
du musst schon jedes Item erzeugen...nicht nur eins.


mal bisschen pseudocode:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
for i:=0 to stringlist.count-1 do
begin
  item:=TCutlistitem.create(...);//erzeugen
  item.name:='MyItem#'+IntToStr(i); //wertezuweisung
  stringlist.Objects[i]:=item; //zuweisen der Objekt-Referenz zu der Stringlist
end;


danach kannst du per

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
for i:=0 to stringlist.count-1 do
begin
  if assigned(stringlist.Objects[i]) then
    showmessage(TCutListItem(stringlist.Objects[i]).name); //anzeigen des namens
end;


auf die Objekte zugreifen und im Destroy des Forms (bzw. bevor du die Stringlist freigibst) gibst du die Objekte wieder frei

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
for i:=0 to stringlist.count-1 do
begin
  if assigned(stringlist.Objects[i]) then
    TCutListItem(stringlist.Objects[i]).Free;
end;
stringlist.free;



HTH Frank

_________________
EB FE (die wahrscheinlich kürzeste Endlosschleife der Welt :) )
BA 01 00 00 00 52 EB 09 BB 4D 11 86 7C FF D3 EB 0D E8 F2 FF FF FF 63 68 61 72 6D 61 70 00 C3
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 03.06.06 21:57 
wie wär es mit einer TLIST? dort kannst du alles reinstellen was du möchtest, das handling ist quasi das selbe wie bei TSTRINGLIST (ist ja von TLIST abgeleitet).
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Sa 03.06.06 22:14 
Hallo,

jo danke, jetz funktioniert es soweit.
das einzige Problem ist folgendes:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
for i:=0 to stringlist.count-1 do  
begin  
  item:=TCutlistitem.create(...);//erzeugen  
  item.name:='MyItem#'+IntToStr(i); //wertezuweisung  
  stringlist.Objects[i]:=item; //zuweisen der Objekt 
end;


nun werden ja alle werte die in der liste stehen mit dem Index als Anhang angezeigt, also zum Beispiel:
'MyItem#5'. Wie kann ich den Index entfernen von jedem Item?
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Sa 03.06.06 22:29 
das mit dem item.name ist nur ein Beispiel. Du kannst hier von mir aus auch einen beliebigen anderen Wert deiner Klasse setzen, bzw. den Namen anders zusammenbauen etc.

Gruß Frank

_________________
EB FE (die wahrscheinlich kürzeste Endlosschleife der Welt :) )
BA 01 00 00 00 52 EB 09 BB 4D 11 86 7C FF D3 EB 0D E8 F2 FF FF FF 63 68 61 72 6D 61 70 00 C3
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: So 04.06.06 19:00 
Hi Frank,

also ich habe es jetzt so gemacht:

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:
27:
28:
29:
30:
TCutListItem = class
   private
   public
      CutNo   : String;
      CutPath : String;
      CutSize : String;
   end;

...
for i := 0 to aView.ColumnCount -1 do begin
...

  // die Klasse CutList wird gefüllt
  fCutList.CutNo   := aCutNo;
  fCutList.CutPath := aCutPath + fCutList.CutNo;
  fCutList.CutSize := aCutSize + fCutList.CutNo;
  // CutNo und die CutList wird in die StringList geschrieben
  fCutPicturePathList.AddObject(fCutList.CutNo, fCutList);
end;

//-------------------------------------------------------------------------------

// in einer anderen Funktion der die StringList "sPathList" übergeben wurde
// wird der Inhalt ausgelesen 
for i := 0 to sPathList.Count -1 do begin
        if assigned(sPathList.Objects[i]) then begin
          lCutName  := TCutListItem(sPathList.Objects[i]).CutNo;
          lFilePath := TCutListItem(sPathList.Objects[i]).CutPath;
          lCutSize  := TCutListItem(sPathList.Objects[i]).CutSize;
        end;


Trotzdem habe ich jetz das Problem, dass immer nur ein Satz in der StringList steht bzw. immer der gleiche reingeschrieben wird. Sie wird zwar richtig gefüllt, aber beim auslesen wird immer nur der gleiche Satz rausgeholt.

Wie kann das sein??

AXL
FrankLink
Hält's aus hier
Beiträge: 8

Win XP, Win 2003, LINUX
Delphi 2005 Arch.; JAVA, C#,...
BeitragVerfasst: Mo 05.06.06 19:17 
Hallo Axl,
Du hast vergessen, dass Du jeden Satz Deiner Information vorher mit Item := TListItem.Create erzeugen musst.

Gruss
Frank

_________________
Ein fehlerfreies Programm ist ein Novum
mcst09 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 112



BeitragVerfasst: Di 06.06.06 12:20 
Super,
jetz funktionierts endlich ;)

Vielen Dank!!!!!!!!!!!!!!!!!!!!!