Autor Beitrag
Smartie
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 205



BeitragVerfasst: Di 26.04.05 09:20 
Hallo!

Ich hab ein "kleines" Problemchen:

Ich möchte in einen Array verschiedene Datentypen abspeichern können. Geht das überhaubt?

Es handelt sich hierbei um Integer, Double sowie Strings.

Ich hoffe da weiß jemadn eine Lösung.

Ich habe bisher nur mit "Array of String" etc. pp. gearbeitet, noch nie mit gemischten Typen.

Lieben Gruß

Smartie
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 26.04.05 09:23 
Geht evtl mit einem Array of Variant.
Ist aber keine gute Lösung. Variant ist langsam.
Smartie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 205



BeitragVerfasst: Di 26.04.05 09:25 
Danke, ich werds mal ausprobieren.

Geschwindigkeit ist hier nicht das Problem, zum Glück.

Lieben Gruß

Smartie
Smartie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 205



BeitragVerfasst: Di 26.04.05 09:49 
Servus!

Also, ich hab ein Testprojekt gestartet, um den Array of Variant auszutesten.

Ich hab folgendes Probiert, aber er bringt mir einen Fehler:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);
var a : integer;
var b: double;
var c: String;
type ZielArray = Array[0..2of Variant;
begin
a := 43;
b := 4.2;
c := 'Hallo ArrayWelt';
ZielArray[0] := a;
ZielArray[1] := b;
ZielArray[2] := c;
end;


*Grübel*

Kann mir jemand bitte sagen, woran das liegt?

Danke und Lieben Gruß

Smartie

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 26.04.05 09:51 
Welchen Fehler (immer dran denken)?
btw: Nimm statt Code-Tags lieber Delphi-Tags. Dann ist das schöner formatiert.

Habs mir mal angesehen. Bitte wie folgt ändern:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
  type tZielArray = Array[0..2of Variant; // Korrektur
  var a : integer;
  var b: double;
  var c: String;
  var ZielArray : tZielArray; // Korrektur
begin
  a := 43;
  b := 4.2;
  c := 'Hallo ArrayWelt';
  ZielArray[0] := a;
  ZielArray[1] := b;
  ZielArray[2] := c;
end;


Zuletzt bearbeitet von jasocul am Di 26.04.05 09:56, insgesamt 1-mal bearbeitet
Smartie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 205



BeitragVerfasst: Di 26.04.05 09:54 
Fehler: [Fehler] Unit1.pas(35): '(' erwartet, aber '[' gefunden

Ich kan nmir da nichtwirklich n Reim drauf machen. Ich vermute mal ein Array of Variant benötigt eine "besondere" Behandlung.

Lieben Gruß

Smartie
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 26.04.05 09:57 
Habe meinen Beitrag editiert. Siehe oben.
Smartie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 205



BeitragVerfasst: Di 26.04.05 10:05 
:beer:

Jawohl! Tausend Dank! Es funktioniert!

Ich hab auch gleich mit Berechnungen experimentiert, und es läuft, kaum zu glauben!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Button1Click(Sender: TObject);
var a : integer;
var b, f: double;
var c: String;
type TZielArray = Array[0..2of Variant;
var ZielArray : tZielArray;
begin
a := 43;
b := 4.2;
c := 'Hallo ArrayWelt';
ZielArray[0] := a;
ZielArray[1] := b;
ZielArray[2] := c;
label1.Caption := ZielArray[0];
label2.Caption := ZielArray[1];
label3.Caption := ZielArray[2];
f := a + b;
label4.Caption := FloattoStr(f);
end;


Herzlichen Dank

Smartie
MitschL
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 211

Win 98 SE, Win 2000
D5 Pers, D6 Pers und D7 Pro
BeitragVerfasst: Di 26.04.05 11:13 
Moin!

Ich hab damit ein Problem. Woher seh ich, welchen Typ ich da vorfinde?
Dann frage ich mich auch: Kann ich die Daten unterschiedlichen Typs gruppieren? Dann würde ich mit Arrays arbeiten, die Records als Elemente enthalten. Wie sähe es ansonsten mit Records mit varianten Inhalten aus?

gegrüßt!

_________________
"Bloßes Ignorieren ist noch keine Toleranz." (Theodor Fontane)
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 26.04.05 11:25 
@MitschL:
Dein Einwand ist richtig, aber danach hat er nicht gefragt.
Selbst wenn du einen varianten Record für das Array benutzt, ist der Typ nicht zwangsläufig bekannt. Das hängt dann von der Konstruktion ab.
Wenn das Array allerdings nur diese 3 Elemente besitzt, die wie im Beispiel angeordnet sind, wäre ein Record sowieso die bessere Wahl gewesen.

Aber das Raten habe ich mir irgendwann mal abgewöhnt.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 26.04.05 11:26 
user profile iconMitschL hat folgendes geschrieben:
Moin!

Ich hab damit ein Problem. Woher seh ich, welchen Typ ich da vorfinde?

Variant -> F1 (siehe auch VarType :roll: )

Für Records mit variablem Inhalt, siehe TVarRec. Und siehe auch array of const.

PS: user profile iconMitschL ist ja gar nicht der Thread-Steller. Bitte eröffne bei neuen Fragen einen neuen Thread. Und schau erst in der Delphihilfe nach. Dass die Lösung deines Problems unter Variant zu finden ist, ist ja eigentlich offensichtlich. Du solltest stets die Dokumentation durchlesen, bevor du mit neuen Sachen arbeitest!! Dafür gibt's ja die Dokumentation. Die erklärt das ganze meistens viel besser und genauer, als es hier jemals jemand erklären wird :roll:

//Edit: Sorry, hab den Beitrag von MitschL falsch verstanden. Ich nehme meine Belehrung zurück ;)
Smartie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 205



BeitragVerfasst: Di 26.04.05 12:34 
user profile iconMitschL hat folgendes geschrieben:
Moin!

Ich hab damit ein Problem. Woher seh ich, welchen Typ ich da vorfinde?



Ist für mich kein Problem, da ich ja aus meiner INI rauslese, wie ich meine Daten zu interpretieren habe, wie sie dann zu speichern sind, eben in diesem Varainten-Array, und diese rausgelesenen Informationen speichere ich zudem in einem String-Array. D.h. Wenn ich aus welchen Grünen Auch immer wissen wollte, was für ein Typ in ZielArr[x1] ist, lese ich einfach meine Zielformat[x1] aus.

Gruß

Smartie