Autor Beitrag
D. Annies
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: So 23.01.11 22:06 
Hi, Delpher,

wer macht sich mal die Mühe, und kontrolliert die folgenden Schnipsel auf Richtigkeit?
Vielen Dank!

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:
function isCellSelected(stringgrid: tstringgrid; x,y: longint): boolean;
begin
  result := false;
  try
    if (x >= stringgrid.selection.left) and (x <= stringgrid.selection.right) and
       (y >= stringgrid.selection.top)  and (y <= stringgrid.selection.bottom) then
      result := true;
  except
  end
end;

function TBuchMain.ConnectOpOff:boolean;
var rueckgabe : boolean;
begin
  try
    Server := CreateOleObject('com.sun.star.ServiceManager');
    Desktop := Server.createInstance('com.sun.star.frame.Desktop');
    rueckgabe := true;
    showmessage('OpenOffice ist verfügbar (ConnectOpOff) ');
  except
    rueckgabe := false;
  end;
  result := rueckgabe;
end;


Es geht mir darum, ob der Code so in Ordnung ist oder verbessert werden sollte.
Gruß, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
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: So 23.01.11 22:20 
Wozu das try..except in der ersten Funktion? Da kann doch eine Exception allenfalls auftreten, wenn das StringGrid nicht existiert, und das kannst du ja abfangen.

Für diesen Beitrag haben gedankt: D. Annies
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: So 23.01.11 22:27 
Genau um solche Unnötigkeiten geht es mir, aber auch, wenn erwas noch fehlt!
Danke! :)

_________________
ut vires desint, tamen est laudanda voluntas
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 23.01.11 22:56 
- Ich würde Server und Desktop als lokale Referenzen halten bis klar ist, dass nichts schiefgelaufen ist. Erst dann den Klassenfeldern zuordnen.

Dann spiele ich mal FxCop/StyleCop für Delphi: ;)

- Top-Level Exceptions sollte man nicht fangen, ausser man re-raised.
- Die Codeformatierung entspricht keiner gängigen Richtlinie.
- Hardcodierte Strings.
- isCellSelected Funktion in globalem Scope; Könnte man als Class-Helper gestalten oder sonst in eine Hilfsklasse tun.
- isCellSelected: Du weist dem Resultat mehrfach etwas zu. Du könntet das if weglassen und eine direkte Zuweisung machen.
- ConnectOpOff: Du weist dem Resultat mehrfach etwas zu.
- (angenommen ConnectOpOff ist nicht private): Du validierst stringgrid als Referenz nicht, bevor du sie verwendest.
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: So 23.01.11 23:22 
Schade, dass ich (fast) nicht ein einziges Wort verstanden habe.

_________________
ut vires desint, tamen est laudanda voluntas
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 23.01.11 23:35 
Irgendwie so, natürlich nicht alles direkt nacheinander; und alles, was public ist noch ein bisschen kommentieren.
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:
const
  ServiceManagerClassName = 'com.sun.star.ServiceManager';
  DesktopClassName = 'com.sun.star.frame.Desktop';

type
  TStringGridHelper = class helper for TStringGrid
  public
    function IsCellSelected(X, Y: Integer): Boolean;
  end;

function TStringGridHelper.IsCellSelected(X, Y: Integer): Boolean;
var
  Selection: TGridRect;
begin
  Selection := Self.Selection;
  Result := (X >= Selection.Left) and (X <= Selection.Right) and
            (Y >= Selection.Top) and (Y <= Selection.Bottom);
end;

function TBuchMain.Connect: Boolean;
var
  Server, Desktop: Variant;
begin
  try
    Server := CreateOleObject(ServiceManagerClassName);
    Desktop := Server.CreateInstance(DesktopClassName);
  except
    on E: EOleError do
      Exit(False);
  end;
  FServer := Server;
  FDesktop := Desktop;
  Result := True;
end;
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: So 23.01.11 23:58 
Wobei class helper erst ab Delphi 2006 funktionieren und die parametrisierte Exit-Funktion ab Delphi 2009 oder so. ;-)
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 24.01.11 00:03 
Naja, war auch nur ein Vorschlag. Vielleicht driftet meine Denkweise langsam Richtung C# ab...
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Mo 24.01.11 09:07 
Trotzdem danke für deine Idee!

_________________
ut vires desint, tamen est laudanda voluntas