Für Fragen zur Programmiersprache auf welcher Lazarus aufbaut
Mathias
Beiträge: 6194 Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz
Beitrag
von Mathias » So 1. Mär 2020, 17:25
Wie kann ich so eine Menge durchlaufen ?
Code: Alles auswählen
type tcputype = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ;var cputype : tcputype;begin for cputype do MacheEtwas; // Wie get's ?
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot
wp_xyz
Beiträge: 4889 Registriert: Fr 8. Apr 2011, 09:01
Beitrag
von wp_xyz » So 1. Mär 2020, 18:15
Code: Alles auswählen
type TCPUType = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ;var cputype : TCPUType;begin for cputype in TCPUType do MacheEtwas;
Mathias
Beiträge: 6194 Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz
Beitrag
von Mathias » So 1. Mär 2020, 19:07
Danke erst mal.
Die Schleife würde jetzt laufen.
Aber wie kann ich auf die einzelnen Elemente zugreifen ?
Code: Alles auswählen
type TCPUType = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ;var cputype : TCPUType;const arr: array [ TCPUType] of Byte = ( 1 , 2 , 3 ) ;begin for cputype in TCPUType do WriteLn( arr[ ??] ) ; // wie geht's ? end ;
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot
Mathias
Beiträge: 6194 Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz
Beitrag
von Mathias » So 1. Mär 2020, 19:16
Ich habe etwas gebastelt, was funktioniert.
Aber es gibt sicher etwas eleganteres ?
Code: Alles auswählen
type TCPUType = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ;var cputype : TCPUType; i: Integer;const arr: array [ TCPUType] of Byte = ( 7 , 8 , 9 ) ;begin i := 0 ; for cputype in TCPUType do begin Memo1.Lines .Add ( arr[ TCPUType( i) ] .ToString ) ; inc( i) ; end ;
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot
Socke
Lazarusforum e. V.
Beiträge: 3158 Registriert: Di 22. Jul 2008, 19:27
OS, Lazarus, FPC: Lazarus: SVN; FPC: svn; Win 10/Linux/Raspbian/openSUSE
CPU-Target: 32bit x86 armhf
Wohnort: Köln
Kontaktdaten:
Beitrag
von Socke » Mo 2. Mär 2020, 11:15
Deine Zählvariable cputype ist doch bereits vom selben Typ wie der Array-Index!
Code: Alles auswählen
type TCPUType = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ;var cputype : TCPUType;const arr: array [ TCPUType] of Byte = ( 7 , 8 , 9 ) ;begin for cputype in TCPUType do begin Memo1.Lines .Add ( arr[ cputype] .ToString ) ; end ;end ;
MfG Socke Ein Gedicht braucht keinen Reim//Ich pack’ hier trotzdem einen rein
Mathias
Beiträge: 6194 Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz
Beitrag
von Mathias » Mo 2. Mär 2020, 17:05
Ich habe wohl vor lauter Bäume den Wald nicht gesehen.
Danke
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot
Mathias
Beiträge: 6194 Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz
Beitrag
von Mathias » Mi 4. Mär 2020, 15:50
Nochmals eine Frage:
Wie kann ich die Länge der Array ermitteln ?
Code: Alles auswählen
var cputype: TCPUType; l: integer;const arr: array [ TCPUType] of byte = ( 7 , 8 , 9 ) ;begin for cputype in TCPUType do begin Memo1.Lines .Add ( arr[ cputype] .ToString ) ; end ; l := Length( TCPUType) ; // geht nicht l := Length( cputype) ; // geht nicht
Dies würde funktionieren:
Ist aber ein gebastel.
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot
wp_xyz
Beiträge: 4889 Registriert: Fr 8. Apr 2011, 09:01
Beitrag
von wp_xyz » Mi 4. Mär 2020, 16:51
Die Länge von arr? Length(arr)
Winni
Beiträge: 1577 Registriert: Mo 2. Mär 2009, 16:45
OS, Lazarus, FPC: Laz2.2.2, fpc 3.2.2
CPU-Target: 64Bit
Wohnort: Fast Dänemark
Beitrag
von Winni » Mi 4. Mär 2020, 18:19
Hi! Zählen! type TCPUType = (cpu_none, cpu_avrtiny, cpu_avrxmega3 ); Das macht 1,2,3 Winni
Winni
Beiträge: 1577 Registriert: Mo 2. Mär 2009, 16:45
OS, Lazarus, FPC: Laz2.2.2, fpc 3.2.2
CPU-Target: 64Bit
Wohnort: Fast Dänemark
Beitrag
von Winni » Mi 4. Mär 2020, 22:22
Hi!
Mal den code n bischen aufräumen:
Code: Alles auswählen
procedure TForm1.Button3Click ( Sender: TObject) ;type tcputype = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ;const cpuInfo : array [ tcputype] of string = ( 'cpu_none' , 'cpu_avrtiny' , 'cpu_avrxmega3' ) ;var cputype : tcputype; begin for cputype in Tcputype do showMessage ( CpuInfo[ cputype] ) ; showMessage ( IntToStr( length( cpuInfo) ) ) ;end ;
Nachtrag:
Happy hacking
Winni
Winni
Beiträge: 1577 Registriert: Mo 2. Mär 2009, 16:45
OS, Lazarus, FPC: Laz2.2.2, fpc 3.2.2
CPU-Target: 64Bit
Wohnort: Fast Dänemark
Beitrag
von Winni » Do 5. Mär 2020, 00:02
Hi!
Nochmal kürzer. Da waren dunkle Erinnerungen an die unit
typinfo , die ich erst aber nachlesen musste.
Code: Alles auswählen
uses ....................., typinfo; .... procedure TForm1.Button3Click ( Sender: TObject) ;type tcputype = ( cpu_none, cpu_avrtiny, cpu_avrxmega3 ) ; var cputype : tcputype; len : integer;begin for cputype in Tcputype do showMessage ( GetEnumName( TypeInfo( TCpuType) , Ord( cputype) ) ) ; len := ord( high( TCputype) ) - ord( low( TCpuType) ) + 1 ; showMessage ( IntToStr( len) ) ;end ;
Eine unit mehr und ne ganze Menge Hirnschmalz und Tippen weniger.
Winni