4

I am trying to add an Advantech (BDaqCL.pas) library to my application project in Delphi 5 but the .PAS file is riddled with code such as:

    AccessMode = (
  ModeRead = 0,
  ModeWrite,
  ModeWriteWithReset
);

which causes the error:

',' or ')' expected but '=' found

Is this a feature of the later versions of Delphi which is not compatible in D5? If so is it worth manually changing everyone (there are many) or am I bound to run in to much deeper problems?

Edit: Most the enumerations are as above but there's also plenty of more complex like below. Far too many to do manually:

DioPortDir = (
  Input   = $00,
  LoutHin = $0F,
  LinHout = $F0,
  Output  = $FF
);

Edit: I'm beginning the process of declaring them as constants. Could anyone tell me if it's correct to do the following

var
    DioPortDir = LongInt;
const
      Input   = $00;
      LoutHin = $0F;
      LinHout = $F0;
      Output  = $FF;

where I am leaving the var declaration listed and moving the const declarations to the top.

I am unsure what to do when the const is not equal to anything, for e.g.

ValueRange = (
  V_OMIT = -1,            // Unknown when get, ignored when set
  V_Neg15To15 = 0,        // +/- 15 V
  V_Neg10To10,            // +/- 10 V
  V_Neg5To5,              // +/- 5 V
  V_Neg2pt5To2pt5,        // +/- 2.5 V
  V_Neg1pt25To1pt25,      // +/- 1.25 V
  V_Neg1To1,              // +/- 1 V

  V_0To15,                // 0~15 V
  V_0To10,                // 0~10 V
  V_0To5,                 // 0~5 V
  V_0To2pt5,              // 0~2.5 V
  V_0To1pt25,             // 0~1.25 V
  V_0To1,                 // 0~1 V

  mV_Neg625To625,         // +/- 625mV
  mV_Neg500To500,         // +/- 500 mV
  mV_Neg312pt5To312pt5,   // +/- 312.5 mV
  mV_Neg200To200,         // +/- 200 mV
  mV_Neg150To150,         // +/- 150 mV
  mV_Neg100To100,         // +/- 100 mV
  mV_Neg50To50,           // +/- 50 mV
  mV_Neg30To30,           // +/- 30 mV
  mV_Neg20To20,           // +/- 20 mV
  mV_Neg15To15,           // +/- 15 mV
  mV_Neg10To10,           // +/- 10 mV
  mV_Neg5To5,             // +/- 5 mV

  mV_0To625,              // 0 ~ 625 mV
  mV_0To500,              // 0 ~ 500 mV
  mV_0To150,              // 0 ~ 150 mV
  mV_0To100,              // 0 ~ 100 mV
  mV_0To50,               // 0 ~ 50 mV
  mV_0To20,               // 0 ~ 20 mV
  mV_0To15,               // 0 ~ 15 mV
  mV_0To10,               // 0 ~ 10 mV

  mA_Neg20To20,           // +/- 20mA
  mA_0To20,               // 0 ~ 20 mA
  mA_4To20,               // 4 ~ 20 mA
  mA_0To24,               // 0 ~ 24 mA

  // For USB4702_4704
  V_Neg2To2,              // +/- 2 V
  V_Neg4To4,              // +/- 4 V
  V_Neg20To20,            // +/- 20 V

  Jtype_0To760C = $8000, // T/C J type 0~760 'C
  Ktype_0To1370C,          // T/C K type 0~1370 'C
  Ttype_Neg100To400C,     // T/C T type -100~400 'C
  Etype_0To1000C,          // T/C E type 0~1000 'C
  Rtype_500To1750C,       // T/C R type 500~1750 'C
  Stype_500To1750C,       // T/C S type 500~1750 'C
  Btype_500To1800C,       // T/C B type 500~1800 'C

  Pt392_Neg50To150,       // Pt392 -50~150 'C
  Pt385_Neg200To200,       // Pt385 -200~200 'C
  Pt385_0To400,           // Pt385 0~400 'C
  Pt385_Neg50To150,       // Pt385 -50~150 'C
  Pt385_Neg100To100,      // Pt385 -100~100 'C
  Pt385_0To100,           // Pt385 0~100 'C  
  Pt385_0To200,           // Pt385 0~200 'C 
  Pt385_0To600,         // Pt385 0~600 'C 
  Pt392_Neg100To100,      // Pt392 -100~100 'C  
  Pt392_0To100,           // Pt392 0~100 'C 
  Pt392_0To200,           // Pt392 0~200 'C 
  Pt392_0To600,           // Pt392 0~600 'C 
  Pt392_0To400,           // Pt392 0~400 'C 
  Pt392_Neg200To200,      // Pt392 -200~200 'C  
  Pt1000_Neg40To160,      // Pt1000 -40~160 'C  

  Balcon500_Neg30To120,   // Balcon500 -30~120 'C  

  Ni518_Neg80To100,       // Ni518 -80~100 'C 
  Ni518_0To100,           // Ni518 0~100 'C 
  Ni508_0To100,           // Ni508 0~100 'C 
  Ni508_Neg50To200,       // Ni508 -50~200 'C 

  Thermistor_3K_0To100,   // Thermistor 3K 0~100 'C 
  Thermistor_10K_0To100,  // Thermistor 10K 0~100 'C 

  Jtype_Neg210To1200C,    // T/C J type -210~1200 'C
  Ktype_Neg270To1372C,    // T/C K type -270~1372 'C
  Ttype_Neg270To400C,     // T/C T type -270~400 'C
  Etype_Neg270To1000C,    // T/C E type -270~1000 'C
  Rtype_Neg50To1768C,     // T/C R type -50~1768 'C
  Stype_Neg50To1768C,     // T/C S type -50~1768 'C
  Btype_40To1820C,        // T/C B type 40~1820 'C

  Jtype_Neg210To870C,     // T/C J type -210~870 'C
  Rtype_0To1768C,         // T/C R type 0~1768 'C
  Stype_0To1768C,         // T/C S type 0~1768 'C

  // 0xC000 ~ 0xF000 : user customized value range type
  UserCustomizedVrgStart = $C000,
  UserCustomizedVrgEnd = $F000,

  // AO external reference type
  V_ExternalRefBipolar = $F001,  // External reference voltage unipolar
  V_ExternalRefUnipolar = $F002  // External reference voltage bipolar
);

As you can see I have my work cut-out so it's best to make sure I'm doing it right first. Thanks

notidaho
  • 588
  • 8
  • 28
  • i think enums were in D5 too...this is not the problem... – evilone Oct 24 '11 at 15:43
  • 2
    @Evilone Enums were in D1, Turbo Pascal etc. It's the explicit ordinality that causes the compiler to gag. – David Heffernan Oct 24 '11 at 16:00
  • Answers should note that in this particular case, you can safely remove the explicitly assigned ordinality, because it is redundant, but since you said the code was riddled with these, if some of the similar declarations are *not* redundant, then you may run into trouble if there is a particular reason in code why this got done. – Nick Hodges Oct 24 '11 at 17:19
  • Yes. Thanks for all the information but this unit is over 2500 lines long. While I'll only be using a tiny bit of it, I can't imagine manually changing it all. I will ask Advantech if they have a version without Enums but it's unlikely as it's used to talk to an access control PCI driver on Windows7 – notidaho Oct 24 '11 at 18:33
  • For things like DioPortDir see @da-soft answer. – da-soft Oct 25 '11 at 04:33
  • I am floored that anybody stuck with Delphi 5 when it was (and is) so easy to move up to Delphi 7, or if you can get over the IDE style, Delphi 2007, at least. – Warren P Oct 25 '11 at 12:12
  • @Warren P You may be right Warren. I inherited the IDE and the code and was told the (many) components installed and relied upon by the many applications we have would not make it past the upgrade. I may look in to this and re-post but in the short-term I just need this class converted asap. thanks – notidaho Nov 03 '11 at 15:59
  • If you have any binary-only components (you have only DCU files) you should find out by trying to move up to Delphi 2007, and any component which is dead and no longer available with source code, you should make plans to replace it. It is dangerous to leave your app crippled and stuck in the past like that. – Warren P Nov 03 '11 at 16:21

3 Answers3

10

This feature, Enumerated Types with Explicitly Assigned Ordinality, was added in Delphi 6 which is why the code will not compile in Delphi 5.

In your example the code is equivalent to

AccessMode = (
  ModeRead,
  ModeWrite,
  ModeWriteWithReset
);

and you can use that safely. I don't quite understand why the author of the code explicitly assigned 0 to the first ordinal since it has no effect on the meaning of the type.

If you encounter code that explicitly sets ordinals whose lowest value is not 0, or has non-contiguous ordinality, then it is more complex to workaround.

Note that you typically only need to explicitly set ordinality when you are interoperating with something external, e.g. file formats, calls to or from other libraries. If the variables of an enumerated type only ever exist in memory, and in your code, then you most likely don't care what ordinality they have.

I'm not 100% sure why this feature was added but I seem to recall that it was needed to support the Kylix product.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • This library supports various communication with a PCI card, but I will only actually be using one tiny feature - Digital Output. There are other more complex ones like the one I included in my edit, it's maybe safe to presume they're needed. – notidaho Oct 24 '11 at 18:40
  • The more complex ones will need to be declared as `Byte`, `Word`, etc. depending on how wide the values are, and the actual values defined as constants. – David Heffernan Oct 24 '11 at 18:44
  • Could you have a look at my edit in the OP to tell me whether I'm declaring it right, and also what do when the consts have no value. – notidaho Nov 03 '11 at 15:56
  • The key to working this all out is this statement from the documentation to which I linked: "Any value that isn't explicitly assigned an ordinality has ordinality one greater than that of the previous value in the list. If the first value isn't assigned an ordinality, its ordinality is 0." I think with that information it should be clear what to do. My advice is to follow the link in my answer and read it very carefully. It has some useful examples. – David Heffernan Nov 03 '11 at 20:13
5
  1. The syntax allowing to specify enum member value was introduced after Delphi 5, probably in Delphi 6.
  2. You should replace ModeRead = 0 with just ModeRead here and in similar places, when the updated enum preserves the same member values (member[0] = 0, member[i] = member[i - 1] + 1).
  3. Otherwise you should replace the original enum type with something like that:

    type
      AccessMode = Byte;
    const
      ModeRead = 0;
      ModeWrite = 1;
      ModeWriteWithReset = 2;
    

Pay attention to = Byte. Depending on the number of enum members, it may be:

  • 1 ... 256 -> Byte
  • 257 ... 2**16 -> Word
  • 2**16 + 1 ... -> LongWord
da-soft
  • 7,670
  • 28
  • 36
  • Sorry it's been a while since I did powers. To clarify - a Byte would be OK for = $00 and even my biggest ones like $8000? I'd only need a Word declaration for value with length > 15 digits? – notidaho Nov 03 '11 at 15:24
3

Since D6 or D7 you can explicitly assign values to enumeration members. In the case you posted just removing the = 0 should fix your problems. In more complicated cases this article might give you some ideas.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83