Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Fehler E2072 bei const Array


mtm - Fr 29.09.17 16:13
Titel: Fehler E2072 bei const Array
Hallo zusammen,
ich hätte da mal ein Problem.
In einem meiner Programme definiere ich eine Konstante Tabelle. Diese ist folgendermaßen aufgebaut :

Delphi-Quelltext
1:
2:
3:
4:
5:
  TTagRec = record
    Typ: TTagTypen;
    Desc: String;
    max: Integer;
  end;

...

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:
const
  TTagArray: Array[TTag2] of TTagRec = (
    (Typ:ttString; Desc:'Objekt-Name';max:64),
    (Typ:ttString; Desc:'Bearbeitungsstatus';max:64),
    (Typ:ttInt; Desc:'Dringlichkeit';max:1),
    (Typ:ttString; Desc:'Kategorie';max:3),
    (Typ:ttString; Desc:'Ergänzende Kategorie';max:32),
    (Typ:ttString; Desc:'Schlüsselwort';max:64),
    (Typ:ttString; Desc:'Spezielle Anweisungen';max:256),
    (Typ:ttDate; Desc:'Erstellungsdatum';max:8),
    (Typ:ttTime; Desc:'Erstellungsuhrzeit';max:11),
    (Typ:ttDate; Desc:'Digitalisierungsdatum';max:8),
    (Typ:ttTime; Desc:'Digitalisierungsuhrzeit';max:11),
    (Typ:ttString; Desc:'Ursprungsprogramm';max:32),
    (Typ:ttString; Desc:'Ersteller';max:32),
    (Typ:ttString; Desc:'Ersteller-Titel';max:32),
    (Typ:ttString; Desc:'Stadt/Ort';max:32),
    (Typ:ttString; Desc:'Ortsdetail';max:32),
    (Typ:ttString; Desc:'Bundesland';max:32),
    (Typ:ttString; Desc:'ISO-Ländercode';max:3),
    (Typ:ttString; Desc:'Land';max:64),
    (Typ:ttString; Desc:'Original-Übertragungs-Referenz';max:32),
    (Typ:ttString; Desc:'Überschrift';max:256),
    (Typ:ttString; Desc:'Anbieter';max:32),
    (Typ:ttString; Desc:'Quelle';max:32),
    (Typ:ttString; Desc:'Urheberrechtsvermerk';max:128),
    (Typ:ttString; Desc:'Kontakt';max:128),
    (Typ:ttString; Desc:'Beschreibung';max:2000),
    (Typ:ttString; Desc:'Autor';max:32)
  );


TTag2 ist folgende Enumeration :

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:
  TTag2 = (
    ti2ObjectName                  =   5//e.g. 'Ferry Sinks' (nice and blunt then...)
    ti2EditStatus                  =   7//e.g. 'Lead', 'CORRECTION'
    ti2Urgency                     =  10,
    ti2Category                    =  15,
    ti2SupplementaryCategory       =  20//repeatable
    ti2Keyword                     =  25//repeatable (one keyword per instance)
    ti2SpecialInstructions         =  40,
    ti2DateCreated                 =  55//YYYYMMDD; 'to designate the date the intellectual content of the objectdata was created rather than the date of the creation of the physical representation' (IPTC spec)
    ti2TimeCreated                 =  60//HHMMSS±HHMM
    ti2DigitalCreationDate         =  62//YYYYMMDD
    ti2DigitalCreationTime         =  63//HHMMSS±HHMM
    ti2OriginatingProgram          =  65,
    ti2Byline                      =  80//repeatable
    ti2BylineTitle                 =  85//e.g. 'Staff Photographer', repeatable
    ti2City                        =  90,
    ti2SubLocation                 =  92,
    ti2ProvinceOrState             =  95,
    ti2CountryCode                 = 100//three-letter code
    ti2CountryName                 = 101//three-letter code
    ti2OriginalTransmissionRef     = 103,
    ti2Headline                    = 105,
    ti2Credit                      = 110,
    ti2Source                      = 115,
    ti2CopyrightNotice             = 116,
    ti2Contact                     = 118//repeatable
    ti2CaptionOrAbstract           = 120,
    ti2WriterOrEditor              = 122  //repeatable
  );


Wenn ich mich jetzt nicht komplett verzählt habe, sind sowohl in der Enumeration, als auch in dem Array 27 Elemente. Trotzdem kommt beim Kompilieren folgende Fehlermeldung :

E2072 Anzahl der Elemente (27) weicht von der Deklaration (118) ab


Keine Ahnung, wo der die 118 herholt. Hat vielleicht jemand schon mal ein ähnliches Problem gehabt. Wäre für Tips sehr dankbar.

mit vielem Dank im Voraus
mtm

Moderiert von user profile iconNarses: Quote- durch Delphi-Tags ersetzt
Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Fr 29.09.2017 um 16:36


Blup - Fr 29.09.17 17:30

118(Anzahl Elemente) = 122(grösstes Element) - 5(kleinstes Element) + 1

Der numerische Wert - 5 wird direkt als Index beim Zugriff auf das Element verwendet.
Der Compiler mag keine Lücken, er müsste eigentlich selbständig die Lücken im Array z.B. mit nil-Pointer auffüllen.
Das haben die Compilerbauer aber nicht vorgesehen. Die Prüfung auf Bereichsüberschreitungen wäre deutlich schwieriger.


baka0815 - Mo 02.10.17 14:38

Wie user profile iconBlup schon sagt, müssen in so einem Fall die Einträge der Enumeration fortlaufend sein.

Was du machen könntest ist, die Enumeration fortlaufend zu definieren und einen Helper zu erstellen.

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:
35:
  TTag2 = (
    ti2Nichts                      = 0,
    ti2ObjectName                  = 1//  5, //e.g. 'Ferry Sinks' (nice and blunt then...)
    ti2EditStatus                  = 2//  7, //e.g. 'Lead', 'CORRECTION'
    ti2Urgency                     = 3// 10,
    ti2Category                    = 4// 15,
    ti2SupplementaryCategory       = 5// 20, //repeatable
    ti2Keyword                     = 6// 25, //repeatable (one keyword per instance)
...
  );

  TTag2Helper = record helper for TTag2
    function ToInteger: Integer;
    function FromInteger(const AValue: Integer): TTag2;
  end;

function TTag2Helper.ToInteger: Integer;
begin
  case Self of
    ti2ObjectName:
      Result := 5
    ti2EditStatus:
      Result := 7
    ...
  end;
end;

function TTag2Helper.FromInteger(const AValue: Integer): TTag2;
begin
  if (AValue = 5then
    Result := ti2ObjectName
  else if (AValue = 7then
    Result := ti2EditStatus
  ...
end;


Der Zugriff könnte dann über TTagArray[ti2Nichts.FromInteger(7)] laufen.


ub60 - Mo 02.10.17 15:41

user profile iconmtm hat folgendes geschrieben Zum zitierten Posting springen:
Keine Ahnung, wo der die 118 herholt.

Nur mal so als Idee: Von 5 bis 122 sind 118 Elemente.

ub60


Gammatester - Mo 02.10.17 17:48

Delphi6-Hilfe hat folgendes geschrieben:
An enumerated type is, in effect, a subrange whose lowest and highest values correspond to the lowest and highest ordinalities of the constants in the declaration.
D.h. Dein Typ ist ein für den Compiler im wesentlichen ein Aufzählungstyp 5..122 mit 122-5+1 = 118 Werten. Der Compiler ist auch sehr konsequent, für eine Variable

Delphi-Quelltext
1:
2:
var
  tta: Array[TTag2] of TTagRec;
gibt liefert er 118 für length(tta).