![]() |
Überschreitung des Maximums (-1) im stringgrid
hallo Leute,
folgendes ist mir gerade passiert, er soll an der kommentierten stelle checken ob die vorhandenen Koordinaten außerhalb meines Stringgrids liegen. Mein stringgrid geht von 0 -9 in beide richtungen. bitte helft mir.. weiß echt nicht mehr weiter:?:)
Code:
if Form1.stringgrid1.cells[Globala, Globalb]='1' then
begin Form1.stringgrid1.cells[Globala,Globalb]:='3'; if (Form1.stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='2')or (Form1.stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='4') or(Globala+Globala1<0) or (Globala+Globala1>9) or (Globalb+Globalb1<0) or (Globalb+Globalb1>9) then // hier an dieser stelle kommt der error sobald globala oder globalb -1 oder kleiner ist.. begin if (Form1.stringgrid1.cells[Globalx-Globala1,Globaly-Globalb1]='2') or (Form1.stringgrid1.cells[Globalx-Globala1,Globaly-Globalb1]='4') or (Globalx-Globala1<0) or (Globalx-Globala1>9) or (Globaly-Globalb1<0) or (Globaly-Globalb1>9) then begin versenkt:=1; showmessage('Schiff Versenkt'); counter:=counter-1; flaute:=0; zugbeginn; end else begin showmessage('Ich habe dein Schiff getroffen' + inttostr(globalx)+'/'+ inttostr(globaly)) ; treffer2; end; end else begin treffer2; end; end else begin Form1.stringgrid1.cells[Globala,Globalb]:='5'; Flaute:=0; end; end; |
AW: Überschreitung des Maximums (-1) im stringgrid
Also bevor ich mich dazu äußere eine Frage: ist deine Formatierung ein Ding des Zufalls oder steckt da System dahinter?
Hier sind 6 Zeichen eingerückt
Delphi-Quellcode:
Hier zwei 2 Zeichen
if Form1.stringgrid1.cells[Globala, Globalb]='1' then
begin
Delphi-Quellcode:
Hier 4 Zeichen
else
begin
Delphi-Quellcode:
Schwer zu entscheiden, oder? ;)
else
begin also deinen Fehler kann ich nicht überprüfen, da da einiges fehlt, aber diese Zeile gefällt mir persönlich nicht so recht:
Delphi-Quellcode:
Was gefällt mir da nicht?
if (Form1.stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='2')or (Form1.stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='4') or(Globala+Globala1<0) or (Globala+Globala1>9) or (Globalb+Globalb1<0) or (Globalb+Globalb1>9) then // hier an dieser stelle kommt der error sobald globala oder globalb -1 oder kleiner ist..
//...
Delphi-Quellcode:
Zuerst greifst du anscheinend auf das StringGrid zu, per Zeilen- und Spaltenwert...
if (Form1.stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='2')or (Form1.stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='4') ...
//...
Delphi-Quellcode:
... aber erst nach dem du zugegriffen hast, prüfst du ob die Werte ok sind.
... or(Globala+Globala1<0) or (Globala+Globala1>9) or (Globalb+Globalb1<0) or (Globalb+Globalb1>9) then // hier an dieser stelle kommt der error sobald globala oder globalb -1 oder kleiner ist..
//... Was hältst du davon zuerst zu prüfen ob die Werte ok sind, und dann erst auf das StringGrid zuzugreifen? Etwa so (ungeprüft):
Delphi-Quellcode:
if NOT ((Globala+Globala1<0) or (Globala+Globala1>9) or (Globalb+Globalb1<0) or (Globalb+Globalb1>9)) then //ich denke NOT ist hier gewünscht
if (stringgrid1.cells[Globala+Globala1, Globalb+Globalb1] = '2') or (stringgrid1.cells[Globala+Globala1,Globalb+Globalb1]='4') then // //... |
AW: Überschreitung des Maximums (-1) im stringgrid
Zitat:
Zitat:
Zitat:
|
AW: Überschreitung des Maximums (-1) im stringgrid
Ein bischen Trennung hilft schon:
Delphi-Quellcode:
function CheckPosition(x, y: Integer): Boolean;
begin Result := (y >= 0) and (y <= 9) and (y >= 0) and (y <= 9); end; function GetPosition(x, y: Integer): string; begin if CheckPosition(x, y) then Result := Form1.stringgrid1.cells[x, y] else Result := '#'; // außerhalb des Spielfelds end; function SetPosition(x, y: Integer; AValue: string); begin if CheckPosition(x, y) then Form1.stringgrid1.cells[x, y] := AValue; end; {ob der Ablauf so richtig ist...} if GetPosition(Globala, Globalb) = '1' then begin SetPosition(Globala, Globalb) := '3'; s := GetPosition(Globala + Globala1, Globalb + Globalb1); if (s = '2') or (s = '4') or (s = '#') then begin s := GetPosition(Globalx - Globala1, Globaly - Globalb1); if (s = '2') or (s = '4') or (s = '#') then begin versenkt:=1; showmessage('Schiff Versenkt'); counter:=counter-1; flaute:=0; zugbeginn; end else begin showmessage('Ich habe dein Schiff getroffen' + inttostr(globalx)+'/'+ inttostr(globaly)); treffer2; end; end else begin treffer2; end; end else begin SetPosition(Globala, Globalb) := '5'; Flaute:=0; end; |
AW: Überschreitung des Maximums (-1) im stringgrid
Vielen Dank für all eure Antworten, es war tasächlich der Fehler gewesen, dass ich erst auf das stringgrid zu gegriffen habe und dann gechekct habe ob es überhaupt drin liegt, also prompt mal getauscht jetzt löuft es wie geschmiert, vielen dank leute ihr seit super ! :) und fröhliche Weihnachten euch allen ;)
|
AW: Überschreitung des Maximums (-1) im stringgrid
Und wieso überall Form1 angeben. Ich gehe davon aus das du den Codeauszug als Methode des entsprechenden Formular definiert hast.
Hier ist dann der Zugriff auf Form1 eher Problemen behaftet wenn man mal den Code umbaut oder extrahiert. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:58 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz