Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Variablen Massenhaft definieren (https://www.delphipraxis.net/205817-variablen-massenhaft-definieren.html)

Kegasetu 20. Okt 2020 09:58

Variablen Massenhaft definieren
 
Hallo Zusammen.

Bitte keinen großen Schrecken bekommen, aber ich habe etwas vor, was so manch einer als total unsinnig beschreiben würde.

Ich habe folgendne Code:
Delphi-Quellcode:
var mmb11, lb11, ew11, zg111, zg211, zg311, zg411, zg511, pz111, pz211, pz311, pz411, pz511, spv111, spv211, spv311, spv411, spv511, spb111, spb211, spb311, spb411, spb511   :integer;
var bz111, bz211, bz311, bz411, bz511 : string;

if ((ComboBox1.ItemIndex = 0) and (ComboBox2.ItemIndex = 0)) then
  begin
  mmb11 := strtoint(edit1.Text);
  lb11 := strtoint(edit2.Text);
  ew11 := strtoint(edit3.Text);
  bz111 := edit4.Text;
  bz211 := edit5.Text;
  bz311 := edit6.Text;
  bz411 := edit7.Text;
  bz511 := edit8.Text;
  zg111 := strtoint(edit9.Text);
  zg211 := strtoint(edit10.Text);
  zg311 := strtoint(edit11.Text);
  zg411 := strtoint(edit12.Text);
  zg511 := strtoint(edit13.Text);
  pz111 := strtoint(edit14.Text);
  pz211 := strtoint(edit15.Text);
  pz311 := strtoint(edit16.Text);
  pz411 := strtoint(edit17.Text);
  pz511 := strtoint(edit18.Text);
  spv111 := strtoint(edit19.Text);
  spv211 := strtoint(edit20.Text);
  spv311 := strtoint(edit21.Text);
  spv411 := strtoint(edit22.Text);
  spv511 := strtoint(edit23.Text);
  spb111 := strtoint(edit24.Text);
  spb211 := strtoint(edit25.Text);
  spb311 := strtoint(edit26.Text);
  spb411 := strtoint(edit27.Text);
  spb511 := strtoint(edit28.Text);
  end;
Nun mein Problem, ich brauche das mit jeweils anderen Endziffern 40 mal (also z.B. mmb12; mmb13..mb410). Ich wollte erst ein paar Arrays füllen, aber mein Problem ist, das Programm wird sehr unübersichtlich werden und anhand der Bezeichnungen kann ich mich strukturieter durcharbeiten.

hoika 20. Okt 2020 10:03

AW: Variablen Massenhaft definieren
 
Hallo,
spb511 ist also für dich übersichtlich und verständlich.

Dann lass es doch so.

KodeZwerg 20. Okt 2020 10:07

AW: Variablen Massenhaft definieren
 
Mit Arrays wird es eher übersichtlicher finde ich anstelle dutzende variablen zu nutzen.
Da sollte man ja wissen welches Array für was zuständig ist.
Oder ein Array aus Arrays etc...
Helfen könnte es wenn Du beschreibst was Du eigentlich vorhast, so kommen viele Ideen zur besseren Umsetzung zustande.
(Records, Klassen, Arrays etc...)

stahli 20. Okt 2020 10:12

AW: Variablen Massenhaft definieren
 
... und ein paar Infos zu den eigenen Vorkenntnissen bezüglich Programmierung und Delphi ...
(und zur genutzten Delphi-Version)

Kegasetu 20. Okt 2020 10:16

AW: Variablen Massenhaft definieren
 
Es soll ein Programm für eine statische Simulation werden. Die Formeln aufzustellen ist für mich kein Problem, aber an der Informatik hapert es. Also so ganz übersichtlich ist es nicht, das gebe ich gerne zu.
Auch den Arrays bin ich nicht unaufgeschlossen. Ich müsste nur ständig eine Liste parat haben, an der ich ablesen kann, welche Stelle für was steht.

Jedoch habe ich auch ein ganz anderes Problem, ich würde das ganze gerne automatisch Speichern, nur bin ich mir nicht sicher wie ich das machen soll. Mein Ansatz dafür lautet wie folgt:

Delphi-Quellcode:
var b1a1: array[1..28] of integer;
begin
for i := 0 to 28 do
b1a1[i] := strtoint(edit[i].Text);

end;
Zwischenzeitlich würde ich auch Probleme mit Strings bekommen, aber das würde ich lösen, indem ich die Edit1-Felder ans Ende setzte und ein neues Array öffne.

Kegasetu 20. Okt 2020 10:21

AW: Variablen Massenhaft definieren
 
Zitat:

Zitat von stahli (Beitrag 1475794)
... und ein paar Infos zu den eigenen Vorkenntnissen bezüglich Programmierung und Delphi ...
(und zur genutzten Delphi-Version)

Also ich würde sagen Anfänger. Früher war ich mal besser, aber mittlerweile ist vieles weg. Ich bleibe aber bei diesen Basic Zeug, viel komplizierter wird es nicht.
Ich nutze Delphi 10.3

KodeZwerg 20. Okt 2020 10:31

AW: Variablen Massenhaft definieren
 
Ich würde eine TStringList anstelle lauter Edit-Felder nutzen, dann kannst Du auch easy durchiterieren.
Wenn Dein Array[1..28] lautet auch bei 1 anfangen und nicht bei 0.

Also, Editfelder ein OnChange zuweisen um den Inhalt in die StringList zu pressen, irgendwann später deine for i := 1 to 28 Sache durchführen.

//Edit
Oder generell alles in eine Klasse verfrachten, das erscheint mir am sinnvollsten.

stahli 20. Okt 2020 10:42

AW: Variablen Massenhaft definieren
 
Also grundsätzlich gibt es viele Möglichkeiten ... je nachdem, welche Konzepte man einsetzen möchte.

Ich würde mal denken, Klassen und Generics sind hier auszuschließen.

Die Daten also entweder in einfache Variablen, wie Du es angedeutet hast, oder in Arrays. Du musst einfach sehen, was für Dich praktikabler ist.

Wichtig wäre, Formular und Logik zu trennen.
Man könnte als Schnittstelle zwei Funktionen definieren:

DatenInFormularSchreiben und DatenAusFormularHolen.
Dort realisierst Du das, was Du im Eingangspost gemacht hast.
Nur die zwei Funktionen sollten die Controls und die Variablen benutzen.
Dann brauchst Du noch eine Funktion BerechneDaten, die z.B. von einem Button angestoßen wird.

Dort sollten alle Berechnungen ablaufen, ohne noch einmal auf ein Edit oder anderes Control zuzugreifen.

In DatenAusFormularHolen kannst Du zwar so etwas wie FindControl('Edit1') benutzen, aber es wird einfacher sein, wenn Du es wie im Eingangspost machst und direkt Edit1.Text benutzt.
Allerdings würde ich den Controls immer sprechende Namen zuordnen, also z.B. zg111 := StrToInt(Edit_zg111.Text);
Das vereinfacht schon mal einiges und vor allem die Wartung und Fehlersuche.

Soweit vielleicht erst mal als Richtungsempfehlung...

Kegasetu 20. Okt 2020 10:50

AW: Variablen Massenhaft definieren
 
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:

Zitat von stahli (Beitrag 1475800)
Also grundsätzlich gibt es viele Möglichkeiten ... je nachdem, welche Konzepte man einsetzen möchte.

Ich würde mal denken, Klassen und Generics sind hier auszuschließen.

Die Daten also entweder in einfache Variablen, wie Du es angedeutet hast, oder in Arrays. Du musst einfach sehen, was für Dich praktikabler ist.

Wichtig wäre, Formular und Logik zu trennen.
Man könnte als Schnittstelle zwei Funktionen definieren:

DatenInFormularSchreiben und DatenAusFormularHolen.
Dort realisierst Du das, was Du im Eingangspost gemacht hast.
Nur die zwei Funktionen sollten die Controls und die Variablen benutzen.
Dann brauchst Du noch eine Funktion BerechneDaten, die z.B. von einem Button angestoßen wird.

Dort sollten alle Berechnungen ablaufen, ohne noch einmal auf ein Edit oder anderes Control zuzugreifen.

In DatenAusFormularHolen kannst Du zwar so etwas wie FindControl('Edit1') benutzen, aber es wird einfacher sein, wenn Du es wie im Eingangspost machst und direkt Edit1.Text benutzt.
Allerdings würde ich den Controls immer sprechende Namen zuordnen, also z.B. zg111 := StrToInt(Edit_zg111.Text);
Das vereinfacht schon mal einiges und vor allem die Wartung und Fehlersuche.

Soweit vielleicht erst mal als Richtungsempfehlung...

Klassen? Um Himmels Willen, bloß nicht :D

So sieht das Programm aus, vielleicht wird es dann etwas klarer, was ich machen will (Bild im Anhang).

Es sollen erstmal ein paar Informationen eingegeben werden und dann gespeichert. Durch den Button "Auswählen" sollen die Informationen dann wieder in den Edit-Feldern erscheinen.

Ich frage mal ganz frech. Gibt es dafür eventuell einen vorgefertigten Code? Eigentlich ist es ja relativ simpel.

Kegasetu 20. Okt 2020 10:51

AW: Variablen Massenhaft definieren
 
[QUOTE=KodeZwerg;1475798]Ich würde eine TStringList anstelle lauter Edit-Felder nutzen, dann kannst Du auch easy durchiterieren.
Wenn Dein Array[1..28] lautet auch bei 1 anfangen und nicht bei 0.

Also, Editfelder ein OnChange zuweisen um den Inhalt in die StringList zu pressen, irgendwann später deine for i := 1 to 28 Sache durchführen.

Das Problem mit dem StringGrid ist die Übersichtlichkeit für den Anwender. Die Werte müssen räumlich getrennt zu betrachten sein.

Klaus01 20. Okt 2020 10:53

AW: Variablen Massenhaft definieren
 
StringList ist nicht gleich StringGrid

Grüße
Klaus

Beach 20. Okt 2020 10:56

AW: Variablen Massenhaft definieren
 
Zitat:

Zitat von Kegasetu (Beitrag 1475796)
[...]
Auch den Arrays bin ich nicht unaufgeschlossen. Ich müsste nur ständig eine Liste parat haben, an der ich ablesen kann, welche Stelle für was steht.
[...]

Ich bin zwar mit Lazarus unterwegs, aber bei Delphi wird es bestimmt vergleichbar sein.
Mir kommt da der Begriff "sprechende variablen" und, wenn ich mich richtig erinnere, "enumerated Types" (?¿) in den Kopf.

Ein Beispiel
Code:
  DataLoaded_keys: (cfg, xpc, ypc, zpc);
  DataLoaded: array[cfg..zpc] of boolean;
Das Array kann ich dann "sprechend" mit DataLoaded[xpc], als Beispiel, verwenden und gibt mir dann den boolschen Hinweis ob die Daten geladen wurden oder nicht.
Es "liest" sich recht eindeutig was man gerade macht. Geht auch mit mehrdimensionalen Arrays.

Die Spezialisten hier können das bestimmt korrigieren und besser erklären.:gruebel:

KodeZwerg 20. Okt 2020 11:04

AW: Variablen Massenhaft definieren
 
Wenn ich mir das Bild anschaue, würde ich mir ein Array mit Typ Record erschaffen die dann ComboBox.Index <Bauteil> gleich sind.
Das Record mit dementsprechend viele Strings bereitstellen (Konvertierung StrToInt/IntToStr würde ich da raushalten)
Bei <Abschnitt> habe ich keine Ahnung was da passiert, also lasse ich das mal außen vor.

Kegasetu 20. Okt 2020 11:18

AW: Variablen Massenhaft definieren
 
Ich bedanke mich eure Hilfe. Ich nehme mir jetzt mal etwas Zeit zum experimentieren und melde mich später oder morgen nochmal.

Kegasetu 20. Okt 2020 13:56

AW: Variablen Massenhaft definieren
 
In mühefolster Kleinarbeit enstand in den letzten Stunden ein Gemisch aus vielen Arrays (ca. 100) und Variablen (ca. 150).
Aber es funktioniert.

Die Idee mit der Liste war gut, ich habe mich für ein Stringgrid entschieden.

holger_barntrup 21. Okt 2020 05:39

AW: Variablen Massenhaft definieren
 
Zeig doch mal wie du das gemacht hast bitte.

Kegasetu 21. Okt 2020 06:30

AW: Variablen Massenhaft definieren
 
Das ist der Befehl zum speichern



Delphi-Quellcode:
procedure TForm2.Button1Click(Sender: TObject);
var i,t,k: integer;
begin

lab5[Combobox1.ItemIndex*ComboBox1.ItemIndex]:= 'Morsch';          //für später

if ((ComboBox1.ItemIndex = 0) and (ComboBox2.ItemIndex = 0)) then
begin
 mb1a1 := strtofloat(edit1.Text);
  l1a1 := strtofloat(edit2.Text);
ewb1a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b1a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz1a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 1) and (ComboBox2.ItemIndex = 0)) then
begin
 mb2a1 := strtofloat(edit1.Text);
  l2a1 := strtofloat(edit2.Text);
ewb2a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b2a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz2a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 2) and (ComboBox2.ItemIndex = 0)) then
begin
 mb3a1 := strtofloat(edit1.Text);
  l3a1 := strtofloat(edit2.Text);
ewb3a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b3a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz3a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 3) and (ComboBox2.ItemIndex = 0)) then
begin
 mb4a1 := strtofloat(edit1.Text);
  l4a1 := strtofloat(edit2.Text);
ewb4a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b4a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz4a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 4) and (ComboBox2.ItemIndex = 0)) then
begin
 mb5a1 := strtofloat(edit1.Text);
  l5a1 := strtofloat(edit2.Text);
ewb5a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b5a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz5a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 5) and (ComboBox2.ItemIndex = 0)) then
begin
 mb6a1 := strtofloat(edit1.Text);
  l6a1 := strtofloat(edit2.Text);
ewb6a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b6a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz6a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 6) and (ComboBox2.ItemIndex = 0)) then
begin
 mb7a1 := strtofloat(edit1.Text);
  l7a1 := strtofloat(edit2.Text);
ewb7a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b7a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz7a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 7) and (ComboBox2.ItemIndex = 0)) then
begin
 mb8a1 := strtofloat(edit1.Text);
  l8a1 := strtofloat(edit2.Text);
ewb8a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b8a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz8a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 8) and (ComboBox2.ItemIndex = 0)) then
begin
 mb9a1 := strtofloat(edit1.Text);
  l9a1 := strtofloat(edit2.Text);
ewb9a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b9a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz9a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 9) and (ComboBox2.ItemIndex = 0)) then
begin
 mb10a1 := strtofloat(edit1.Text);
  l10a1 := strtofloat(edit2.Text);
ewb10a1 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b10a1[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz10a1[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 0) and (ComboBox2.ItemIndex = 1)) then
begin
 mb1a2 := strtofloat(edit1.Text);
  l1a2 := strtofloat(edit2.Text);
ewb1a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b1a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz1a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 1) and (ComboBox2.ItemIndex = 1)) then
begin
 mb2a2 := strtofloat(edit1.Text);
  l2a2 := strtofloat(edit2.Text);
ewb2a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b2a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz2a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 2) and (ComboBox2.ItemIndex = 1)) then
begin
 mb3a2 := strtofloat(edit1.Text);
  l3a2 := strtofloat(edit2.Text);
ewb3a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b3a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz3a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 3) and (ComboBox2.ItemIndex = 1)) then
begin
 mb4a2 := strtofloat(edit1.Text);
  l4a2 := strtofloat(edit2.Text);
ewb4a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b4a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz4a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 4) and (ComboBox2.ItemIndex = 1)) then
begin
 mb5a2 := strtofloat(edit1.Text);
  l5a2 := strtofloat(edit2.Text);
ewb5a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b5a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz5a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 5) and (ComboBox2.ItemIndex = 1)) then
begin
 mb6a2 := strtofloat(edit1.Text);
  l6a2 := strtofloat(edit2.Text);
ewb6a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b6a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz6a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 6) and (ComboBox2.ItemIndex = 1)) then
begin
 mb7a2 := strtofloat(edit1.Text);
  l7a2 := strtofloat(edit2.Text);
ewb7a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b7a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz7a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 7) and (ComboBox2.ItemIndex = 1)) then
begin
 mb8a2 := strtofloat(edit1.Text);
  l8a2 := strtofloat(edit2.Text);
ewb8a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b8a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz8a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 8) and (ComboBox2.ItemIndex = 1)) then
begin
 mb9a2 := strtofloat(edit1.Text);
  l9a2 := strtofloat(edit2.Text);
ewb9a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b9a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz9a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 9) and (ComboBox2.ItemIndex = 1)) then
begin
 mb10a2 := strtofloat(edit1.Text);
  l10a2 := strtofloat(edit2.Text);
ewb10a2 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b10a2[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz10a2[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;
if ((ComboBox1.ItemIndex = 0) and (ComboBox2.ItemIndex = 2)) then
begin
 mb1a3 := strtofloat(edit1.Text);
  l1a3 := strtofloat(edit2.Text);
ewb1a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b1a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz1a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 1) and (ComboBox2.ItemIndex = 2)) then
begin
 mb2a3 := strtofloat(edit1.Text);
  l2a3 := strtofloat(edit2.Text);
ewb2a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b2a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz2a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 2) and (ComboBox2.ItemIndex = 2)) then
begin
 mb3a3 := strtofloat(edit1.Text);
  l3a3 := strtofloat(edit2.Text);
ewb3a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b3a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz3a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 3) and (ComboBox2.ItemIndex = 2)) then
begin
 mb4a3 := strtofloat(edit1.Text);
  l4a3 := strtofloat(edit2.Text);
ewb4a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b4a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz4a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 4) and (ComboBox2.ItemIndex = 2)) then
begin
 mb5a3 := strtofloat(edit1.Text);
  l5a3 := strtofloat(edit2.Text);
ewb5a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b5a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz5a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 5) and (ComboBox2.ItemIndex = 2)) then
begin
 mb6a3 := strtofloat(edit1.Text);
  l6a3 := strtofloat(edit2.Text);
ewb6a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b6a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz6a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 6) and (ComboBox2.ItemIndex = 2)) then
begin
 mb7a3 := strtofloat(edit1.Text);
  l7a3 := strtofloat(edit2.Text);
ewb7a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b7a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz7a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 7) and (ComboBox2.ItemIndex = 2)) then
begin
 mb8a3 := strtofloat(edit1.Text);
  l8a3 := strtofloat(edit2.Text);
ewb8a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b8a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz8a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 8) and (ComboBox2.ItemIndex = 2)) then
begin
 mb9a3 := strtofloat(edit1.Text);
  l9a3 := strtofloat(edit2.Text);
ewb9a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b9a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz9a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 9) and (ComboBox2.ItemIndex = 2)) then
begin
 mb10a3 := strtofloat(edit1.Text);
  l10a3 := strtofloat(edit2.Text);
ewb10a3 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b10a3[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz10a3[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;
if ((ComboBox1.ItemIndex = 0) and (ComboBox2.ItemIndex = 3)) then
begin
 mb1a4 := strtofloat(edit1.Text);
  l1a4 := strtofloat(edit2.Text);
ewb1a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b1a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz1a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 1) and (ComboBox2.ItemIndex = 3)) then
begin
 mb2a4 := strtofloat(edit1.Text);
  l2a4 := strtofloat(edit2.Text);
ewb2a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b2a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz2a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 2) and (ComboBox2.ItemIndex = 3)) then
begin
 mb3a4 := strtofloat(edit1.Text);
  l3a4 := strtofloat(edit2.Text);
ewb3a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b3a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz3a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 3) and (ComboBox2.ItemIndex = 3)) then
begin
 mb4a4 := strtofloat(edit1.Text);
  l4a4 := strtofloat(edit2.Text);
ewb4a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b4a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz4a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 4) and (ComboBox2.ItemIndex = 3)) then
begin
 mb5a4 := strtofloat(edit1.Text);
  l5a4 := strtofloat(edit2.Text);
ewb5a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b5a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz5a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 5) and (ComboBox2.ItemIndex = 3)) then
begin
 mb6a4 := strtofloat(edit1.Text);
  l6a4 := strtofloat(edit2.Text);
ewb6a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b6a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz6a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 6) and (ComboBox2.ItemIndex = 3)) then
begin
 mb7a4 := strtofloat(edit1.Text);
  l7a4 := strtofloat(edit2.Text);
ewb7a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b7a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz7a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 7) and (ComboBox2.ItemIndex = 3)) then
begin
 mb8a4 := strtofloat(edit1.Text);
  l8a4 := strtofloat(edit2.Text);
ewb8a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b8a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz8a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 8) and (ComboBox2.ItemIndex = 3)) then
begin
 mb9a4 := strtofloat(edit1.Text);
  l9a4 := strtofloat(edit2.Text);
ewb9a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b9a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz9a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;

if ((ComboBox1.ItemIndex = 9) and (ComboBox2.ItemIndex = 3)) then
begin
 mb10a4 := strtofloat(edit1.Text);
  l10a4 := strtofloat(edit2.Text);
ewb10a4 := strtofloat(edit3.Text);
k:= 1;
  for i := 1 to 4 do
      for t := 1 to 13 do
      begin
          b10a4[k] := strtofloat(stringgrid1.Cells[i,t]);
          bz10a4[k] := stringgrid1.Cells[0,t];
        k:=k+1;
      end;
end;


end;

Jumpy 21. Okt 2020 09:28

AW: Variablen Massenhaft definieren
 
Wenn mb,l,ewb als 2-dimensionale Arrays angelegt wären, könnte man schreiben:

Delphi-Quellcode:
 mb[ComboBox1.ItemIndex,ComboBox2.ItemIndex] := strtofloat(edit1.Text);
 l[ComboBox1.ItemIndex,ComboBox2.ItemIndex] := strtofloat(edit2.Text);
 ewb[ComboBox1.ItemIndex,ComboBox2.ItemIndex] := strtofloat(edit3.Text);
Wenn dann b und bz 4-dimensionale Array wären und man die Schleifenreihenfolge umdreht könnte man k weglassen und schreiben
Delphi-Quellcode:
  for t := 1 to 13 do
    begin
    bz[ComboBox1.ItemIndex,ComboBox2.ItemIndex,0,t] := stringgrid1.Cells[0,t];
    for i := 1 to 4 do
      begin
          b[ComboBox1.ItemIndex,ComboBox2.ItemIndex,i,t] := strtofloat(stringgrid1.Cells[i,t]);
      end;
    end;
Oder Alternativ ein 3-dimensionales Array mit k, wobei man k statt hochzuzählen auch aus i und t berechnen könnte (k:=t+(i-1)*13).
Auf jeden Fall hätte man sich viel Code gespart.

Rollo62 21. Okt 2020 12:23

AW: Variablen Massenhaft definieren
 
Zitat:

Zitat von Kegasetu (Beitrag 1475812)
Die Idee mit der Liste war gut, ich habe mich für ein Stringgrid entschieden.

Ja das Grid ist wohl die richtige Entscheidung, wenn es um Unmengen an Variablen zur Eingabe geht
(siehe Excel).

himitsu 21. Okt 2020 13:50

AW: Variablen Massenhaft definieren
 
Delphi-Quellcode:
uses
  System.SysConst, System.SysUtils;

type
  // bei uralten Delphi z.B. als "TEdit = class(StdCtrls.TEdit)" über die TForm1-Deklaration
  TEditHelper = class helper for TEdit
  strict private
    function GetFloat: Float;
    procedure SetFloat(Value: Float);
  public
    property AsFloat: Float read GetFloat write SetFloat;
  end;

function TEditHelper.GetFloat: Float;
{begin
  Result := StrToFloat(Self.Text);
end;}
begin
  it not TryStrToFloat(Self.Text, Result) then begin
    if Self.CanFocus then
      Self.SetFocus;
    raise EConvertError.CreateFmt('%s: ' + LoadStr(@SInvalidFloat), [Self.Name, Self.Text]);
  end;  
end;

procedure TEditHelper.SetFloat(Value: Float);
begin
  Self.Text := FloatToStr(Value); // oder Format oder sonstwas
end;
Dann gibst du deinen Edits endlich mal ordentliche Namen und lässt das scheinbar nutzlose Umkopieren sein (von KeineAhnungEdit in SoLalaVariable).

Statt
Delphi-Quellcode:
StrToFloat(Edit1.Text)
ein
Delphi-Quellcode:
la4.AsFloat
, oder vielleicht mit einem noch "verständlicherem" Namen.

Kegasetu 21. Okt 2020 14:29

AW: Variablen Massenhaft definieren
 
Es gäbe sicher sehr viele Ansätze der Vereinfachung. Aber das verstehe ich halt :D

Mal eine ganz andere Frage, es sind ja nicht so wenig Variablen. Kann es sein, dass das Programm deswegen Resourcenprobleme bekommen könnte?

freimatz 21. Okt 2020 16:23

AW: Variablen Massenhaft definieren
 
Nein.

Rollo62 21. Okt 2020 17:07

AW: Variablen Massenhaft definieren
 
Zitat:

Zitat von himitsu (Beitrag 1475900)
.. und lässt das scheinbar nutzlose Umkopieren sein ...

Ich vermute mal das Du die Variablen öfters als einmal brauchst,
je nachdem macht dann ein EINMALIGES Wandeln und Kopieren schon Sinn,
wenn es danach sehr oft benutzt wird.

himitsu 21. Okt 2020 17:14

AW: Variablen Massenhaft definieren
 
Nja, "einfache" Lesbarkeit, gegen ein bissl mehr Rechenzeit von ein paar Nanosekunden ... das fällt meistens auch nicht auf, aber ja kann man ... denoch schadet es nicht die Edits ordentlich zu bennen und das Konvertieren inkl. Fehlerbehandlung auszulagern.
Man könnte sogar in den Helper diesen Cache (fertig konvertierte Variable) relativ einfach einfügen.

Resourcenprobleme:
Irgendwann schon, wenn du zu viele bzw. zu große Variablen anlegst, aber soooooo viel kannst du dir nicht ausdenken.

Lokalen Variablen landen auf dem Stack (standardmäßig bis 1 MB groß) ... diese (veränderbare) Grenze erreicht man schonmal eher.

Rollo62 21. Okt 2020 17:21

AW: Variablen Massenhaft definieren
 
[QUOTE=himitsu;1475915
Lokalen Variablen landen auf dem Stack (standardmäßig bis 1 MB groß) ... diese (veränderbare) Grenze erreicht man schonmal eher.[/QUOTE]

Ich würd mir ja gleich eine Klasse TMegaStatistik dafür anlegen, wo Alles drin ist.
Dann kann ich das auch noch im nächsten Programm nutzen.

Kegasetu 22. Okt 2020 06:33

AW: Variablen Massenhaft definieren
 
Zitat:

Zitat von himitsu (Beitrag 1475915)
Nja, "einfache" Lesbarkeit, gegen ein bissl mehr Rechenzeit von ein paar Nanosekunden ... das fällt meistens auch nicht auf, aber ja kann man ... denoch schadet es nicht die Edits ordentlich zu bennen und das Konvertieren inkl. Fehlerbehandlung auszulagern.
Man könnte sogar in den Helper diesen Cache (fertig konvertierte Variable) relativ einfach einfügen.

Resourcenprobleme:
Irgendwann schon, wenn du zu viele bzw. zu große Variablen anlegst, aber soooooo viel kannst du dir nicht ausdenken.

Lokalen Variablen landen auf dem Stack (standardmäßig bis 1 MB groß) ... diese (veränderbare) Grenze erreicht man schonmal eher.

Ich habe momentan nur 3 Edit-Fenster. Die Variablen und Arrays sind alle logisch benannt und ich habe zudem eine Liste auf der alle Definitionenn eingetragen sind.


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:39 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz