Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Set-Type (also Menge) und for-in-Schleife (https://www.delphipraxis.net/151684-set-type-also-menge-und-schleife.html)

s.h.a.r.k 28. Mai 2010 02:50


Set-Type (also Menge) und for-in-Schleife
 
Hallo zusammen,

ich stehe gerade vor einem seltsamen sprachlichen Problem, was ich nicht ganz nachvollziehen kann. Ich hoffe ihr könnt mir hier etwas Klarheit verschaffen. Und zwar geht es um folgenden Code:

Delphi-Quellcode:
type
TRttiPropertyInfoAttributeTypes = (
  atMapping,
  atVisible,
  atNotNull
);
TRttiPropertyInfoAttributeTypesSet = Low(TRttiPropertyInfoAttributeTypes)..High(TRttiPropertyInfoAttributeTypes);

{ ... }

// folgendes lässt der Compiler zu
FAttributedProperties : array[TRttiPropertyInfoAttributeTypesSet] of TStringList;

// diese for-in-Schleife allerdings nicht
for AttributeType in TRttiPropertyInfoAttributeTypesSet do
begin

end;
Im Quelltext sind schon die wichtigen Stelle markiert. Ich verstehe nicht ganz, wieso ich diese Array-Definition machen darf, allerdings diese for-in-Schleife nicht funktioniert. Ich muss doch in beiden Fällen die gleiche Angabe machen, oder läuft das etwas anders? :gruebel:

Hier noch schnell die Fehlermeldung, die bei der for-in-Konstruktor erscheint:
Code:
[DCC Fehler] RttiPropertyInfo.pas(108): E2029 '(' erwartet, aber 'DO' gefunden
[DCC Fehler] RttiPropertyInfo.pas(109): E2066 Operator oder Semikolon fehlt

sx2008 28. Mai 2010 03:44

Re: Set-Type (also Menge) und for-in-Schleife
 
Delphi-Quellcode:
var
  AttributeType : TRttiPropertyInfoAttributeTypesSet;
begin
  for AttributeType:=Low(TRttiPropertyInfoAttributeTypes) to High(TRttiPropertyInfoAttributeTypes) do

s.h.a.r.k 28. Mai 2010 03:44

Re: Set-Type (also Menge) und for-in-Schleife
 
Das kenne ich schon selbst :zwinker: Ich habe ja nicht nach der Lösung des Problems gefragt, sondern nach dem warum.

alzaimar 28. Mai 2010 05:51

Re: Set-Type (also Menge) und for-in-Schleife
 
TRttiPropertyInfoAttributeTypeSet ist äquivalent zu TRttiPropertyInfoAttributeType, keine Menge und überflüssig.
Ersetze das durch TRttiPropertyInfoAttributeType und vrzichte auf for..in. Das geht mit Enumtypen nicht.

s.h.a.r.k 28. Mai 2010 05:56

Re: Set-Type (also Menge) und for-in-Schleife
 
Kann ich dann aber trotzdem diese Array-Definition beibehalten?

himitsu 28. Mai 2010 06:04

Re: Set-Type (also Menge) und for-in-Schleife
 
Code:
for AttributeType in [color=#ff003f]TRttiPropertyInfoAttributeTypesSet[/color] do
Da gehört eine Variable oder Konstante und kein Typ hin.

IN durchläuft Speicherinhalte und keine Deklarationen.

for AttributeType = Low(TRttiPropertyInfoAttributeTypesSet) to High(TRttiPropertyInfoAttributeTypesSet) do

Delphi-Quellcode:
for AttributeType = Low(TRttiPropertyInfoAttributeTypes) to High(TRttiPropertyInfoAttributeTypes) do


temp := [Low(TRttiPropertyInfoAttributeTypes)..High(TRttiPropertyInfoAttributeTypes)];
for AttributeType in temp do
PS: Es gab schonmal 'nen Thread zu sowas (letztes Jah ... find ihn nur grade nicht), in welchem es auch darum ging alle Felder eines SET zu durchlaufen.

Und da nur ein Hinweis: sowas geht nur mit vollständigen SETs

Sobald etwas wie Folgendes vorkommt, dann gibt es keine RTTI zu den Feldern des Enum/Set.
Delphi-Quellcode:
type
  a = (c=3, e=9, f);
  b = set of a;

alzaimar 28. Mai 2010 06:10

Re: Set-Type (also Menge) und for-in-Schleife
 
Zitat:

Zitat von s.h.a.r.k
Kann ich dann aber trotzdem diese Array-Definition beibehalten?

Probieren geht über studieren ;-)
(Ja)

himitsu 28. Mai 2010 06:24

Re: Set-Type (also Menge) und for-in-Schleife
 
Zitat:

Delphi-Quellcode:
TRttiPropertyInfoAttributeTypesSet =
  Low(TRttiPropertyInfoAttributeTypes)..High(TRttiPropertyInfoAttributeTypes);

Wie schon gesagt wurde: TRttiPropertyInfoAttributeTypesSet ist kein SET, sondern ebenfalls ein ENUM!

Es ist quasi nur sowas wie eine Kopie von TRttiPropertyInfoAttributeTypes.
(ein neuer Typ, mit dem selben Wertebereich)
Code:
TRttiPropertyInfoAttributeTypesSet =
  [color=#ff0000][[/color]Low(TRttiPropertyInfoAttributeTypes)..High(TRttiPropertyInfoAttributeTypes)[color=#ff0000]][/color];

// oder

TRttiPropertyInfoAttributeTypesSet = [color=#ff0000]set of[/color]
  Low(TRttiPropertyInfoAttributeTypes)..High(TRttiPropertyInfoAttributeTypes);

// oder

TRttiPropertyInfoAttributeTypesSet = set of [color=#ff0000]TRttiPropertyInfoAttributeTypes[/color];





für SETs geht aber sowas:
Code:
for AttributeType in [color=#ff003f]TRttiPropertyInfoAttributeTypesSet[/color] do
Da gehört eine Variable oder Konstante und kein Typ hin.

IN durchläuft Speicherinhalte und keine Deklarationen.

for AttributeType = Low(TRttiPropertyInfoAttributeTypesSet) to High(TRttiPropertyInfoAttributeTypesSet) do

Delphi-Quellcode:
for AttributeType = Low(TRttiPropertyInfoAttributeTypes) to High(TRttiPropertyInfoAttributeTypes) do


temp := [Low(TRttiPropertyInfoAttributeTypes)..High(TRttiPropertyInfoAttributeTypes)];
for AttributeType in temp do
PS: Es gab schonmal 'nen Thread zu sowas (letztes Jah ... find ihn nur grade nicht), in welchem es auch darum ging alle Felder eines SET zu durchlaufen.

Und da nur ein Hinweis: sowas geht nur mit vollständigen SETs

Sobald etwas wie Folgendes vorkommt, dann gibt es keine RTTI zu den Feldern des Enum/Set.
Delphi-Quellcode:
type
  a = (c=3, e=9, f);
  b = set of a;

Uwe Raabe 28. Mai 2010 07:14

Re: Set-Type (also Menge) und for-in-Schleife
 
So geht es (ich hab mal den Namen verkürzt):

Delphi-Quellcode:
type
  TMyEnum = (
    atMapping,
    atVisible,
    atNotNull
  );
  TMyEnumSet = set of TMyEnum;

const
  cAllMyEnums: TMyEnumSet
    = [Low(TMyEnum)..High(TMyEnum)];

var
  FAttributedProperties : array[TMyEnum] of TStringList;

procedure Test;
var
  AttributeType: TMyEnum;
begin
  for AttributeType in cAllMyEnums do
  begin

  end;
end;

himitsu 28. Mai 2010 07:49

Re: Set-Type (also Menge) und for-in-Schleife
 
Jupp (hatte ich ja auch schon gesagt ... Variable oder Konstante)

aber Achtung (wie ebenfalls erwähnt):
Hier wird die For-Schleife 10 Werte liefern und nicht nur 3.
Delphi-Quellcode:
type
  TMyEnum = (
    atMapping,
    atVisible,
    atNotNull = 9   
  );
  TMyEnumSet = set of TMyEnum;

const
  cAllMyEnums: TMyEnumSet
    = [Low(TMyEnum)..High(TMyEnum)];

var
  FAttributedProperties : array[TMyEnum] of TStringList;

procedure Test;
var
  AttributeType: TMyEnum;
begin
  for AttributeType in cAllMyEnums do
  begin

  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:14 Uhr.
Seite 1 von 2  1 2      

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