Autor Beitrag
R4id
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Fr 21.08.09 21:29 
Hallo.

Ich hab hier eine Klasse:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
type
  TVGUIGameMenuItem = class(TObject)
  private
    FID: Integer;
    FName: string;
    FCommand: string;
    FOnlyInGame: Boolean;
  public
    constructor Create(); overload;
    constructor Create(const ID: Integer; const Name, Command: stringconst OnlyInGame: Boolean); overload;

    function AsString(): string;

    property ID: Integer read FID write FID;
    property Name: string read FName write FName;
    property Command: string read FCommand write FCommand;
    property OnlyInGame: Boolean read FOnlyInGame write FOnlyInGame;
  end;
und das dazu gehörige Array:
ausblenden Delphi-Quelltext
1:
2:
type
  TVGUIGameMenuItems = array of TVGUIGameMenuItem;
Wenn ich nun 2 Items in diesem Array habe, wie kann ich diese miteinander vertauschen?
Beispiel:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
  Array[0] = ID:         0
             Name:       'Index 0'
             Command:    'cmd'
             OnlyInGame: true

  Array[1] = ID:         1
             Name:       'Index 1'
             Command:    'cmd'
             OnlyInGame: false
Diese 2 Items sollen nun miteinander vertauscht werden.

Ich hab mir hier schon mal eine Procedure zusammen "gewürfelt", bin mir aber nicht sicher ob diese 100% fehler frei funktioniert (Ich meine mit dem Speicher [Memory], und Access Violation).
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure SwitchItems(var AArray: TVGUIGameMenuItems; const AIndex1, AIndex2: Integer);
var
  Item1, Item2: Pointer;
begin
  Item1 := Pointer(AArray[AIndex1]);
  Item2 := Pointer(AArray[AIndex2]);

  AArray[AIndex1] := TVGUIGameMenuItem(Item2);
  AArray[AIndex2] := TVGUIGameMenuItem(Item1);
end;


Gruß
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 21.08.09 21:34 
Was spricht gegen die normale Vorgehensweise ohne die ganzen unnötigen Casts usw.? :gruebel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure SwitchItems(var AArray: TVGUIGameMenuItems; const AIndex1, AIndex2: Integer);
var
  TempItem: TVGUIGameMenuItem;
begin
  TempItem := AArray[AIndex1];
  AArray[AIndex1] := AArray[AIndex2];
  AArray[AIndex2] := TempItem;
end;
R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Sa 22.08.09 12:51 
Ich hatte immer eine Access Violation bei dieser Variante, lag aber daran das ich auf einen ungelültigen Index zugreifen wollt.

Danke!

Gruß
Dude566
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Sa 22.08.09 14:05 
Das ist im Prinzip wie beim Bubblesort wo auch eine Hilfsvariable hinzugezogen wird um beide auszutauschen, nachdem verglichen wurde ob Variable1 größer als Variable2 ist.

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Sa 22.08.09 16:21 
Darauf hätt ich auch kommen können, mir da was abgucken zukönnen ^^

Gruß