Board index » delphi » C Constants to Delphi Constants?

C Constants to Delphi Constants?

Writing an interface unit so that a D2 app can access a .DLL written in C
... given the C Source listed below, how does one write it with Delphi 2 so
that it's compatible?

< C SOURCE - from a .h file >

  const unsigned long Bit1    = (1<<0);
  const unsigned long Bit2    = (1<<0);
  const unsigned long Bit3    = (1<<0);
  const unsigned long Bit4    = (1<<0);
  const unsigned long AllBits = ( Bit1 | Bit2 | Bit3 | Bit4 );

  const unsigned long FirstBit  = (1<<0);
  const unsigned long SecondBit = (1<<24);
  const unsigned long BothBits  = ( FirstBit | SecondBit);

< D2 SOURCE - from the .pas interface unit >

const
    Bit1    : LongInt = (1 shl 0);
    Bit2    : LongInt = (1 shl 1);
    Bit3    : LongInt = (1 shl 2);
    Bit4    : LongInt = (1 shl 3);
    AllBits : LongInt = ( Bit1 | Bit1 | Bit1 | Bit1 );  << ERROR!

    FirstBit  : LongInt = (1 shl 0);
    SecondBit : LongInt = (1 shl 24);
    BothBits  : LongInt = ( FirstBit | SecondBit );     << ERROR!

Thanks for any help or suggestions!

Rick Wheat
Wheatworks Software

 

Re:C Constants to Delphi Constants?


| is OR.

        -- Kirk

Re:C Constants to Delphi Constants?


Thanks, Kirk ...

I failed to make that change in the message I posted ...

<snip>
    FirstBit  : LongInt = (1 shl 0);
    SecondBit : LongInt = (1 shl 24);
    BothBits  : LongInt = ( FirstBit OR SecondBit );
<snip>

When I check syntax using the code above, Delphi generates the following
error:  Error 163, Constant expression expected.

Is it not possible in Delphi to declare a constant which requires an
operation?  The D2 Help files state that the Boolean operators can be used
in constant expressions ... do you see anything wrong with the way I'm
declaring the const BothBits ?

Rick

Quote
kroma wrote in message <6spupd$...@bgtnsc02.worldnet.att.net>...
>| is OR.

>        -- Kirk

Re:C Constants to Delphi Constants?


Quote
In article <6spfq7$bp...@news.bayou.com>, "Rick Wheat" <r...@wheatworks.com> wrote:
>Thanks, Kirk ...
>I failed to make that change in the message I posted ...
><snip>
>    FirstBit  : LongInt = (1 shl 0);
>    SecondBit : LongInt = (1 shl 24);
>    BothBits  : LongInt = ( FirstBit OR SecondBit );
><snip>
>When I check syntax using the code above, Delphi generates the following
>error:  Error 163, Constant expression expected.
>Is it not possible in Delphi to declare a constant which requires an
>operation?  The D2 Help files state that the Boolean operators can be used
>in constant expressions ... do you see anything wrong with the way I'm
>declaring the const BothBits ?

Actually, what you've done is NOT just to declare constants, but rather typed
constants, which actually take up memory, and can be changed if the {$J}
directive is on.  By giving them a type, they behave as variables in that
calculations that use them come from the memory involved, not from a
substitution.

Constants are typically declared without a type - the type comes into play
during their use.

And yes, you can do operations involving constants like this... here's your
code snippet, rewritten:

const
  Bit1 = 1 shl 0;
  Bit2 = 1 shl 1;
  Bit3 = 1 shl 2;
  Bit4 = 1 shl 3;
  AllBits = Bit1 or Bit2 or Bit3 or Bit4;

  FirstBit = 1 shl 0;
  SecondBit = 1 shl 24;
  BothBits = FirstBit or SecondBit;

Note, too, that Delphi has sets, which work in a binary fashion:

type
  TBits = (Bit1, Bit2, Bit3, Bit4, Bit5, Bit6, Bit7, Bit8);
const
  MyBits = [Bit1, Bit2, Bit4];
  TheirBits = [Bit1, Bit3, Bit6];
  AndingBits = MyBits * TheirBits;
  OringBits = MyBits + TheirBits;
  AndNottingBits = MyBits - TheirBits;
begin
  if Bit1 in MyBits then
    { MyBits has Bit1 on };
  if [Bit2, Bit4] <= MyBits then
    { Bit2 and Bit4 are contained in MyBits };
end;  

You can typecast between integers and enumerated types easily enough, too
(they're otherwise considered strictly off each other's turf - saves a fair
bit of debugging :)

Hope that helps..

  --=- Ritchie Annand

Re:C Constants to Delphi Constants?


Quote
Rick Wheat wrote:

> Thanks, Kirk ...

> I failed to make that change in the message I posted ...

> <snip>
>     FirstBit  : LongInt = (1 shl 0);
>     SecondBit : LongInt = (1 shl 24);
>     BothBits  : LongInt = ( FirstBit OR SecondBit );
> <snip>

> When I check syntax using the code above, Delphi generates the following
> error:  Error 163, Constant expression expected.

> Is it not possible in Delphi to declare a constant which requires an
> operation?  The D2 Help files state that the Boolean operators can be used
> in constant expressions ... do you see anything wrong with the way I'm
> declaring the const BothBits ?

By specifying that FirstBit et al are Longints, you are declaring typed
constants. These are no different than initialized variables (unless you
use the compiler directive (*$J-*)), and can thus be modified, and the
compiler just won't let you declare BothBits like such. Just make them
untyped constants; the compiler will figure out how to type them when it
needs to:

const
  FirstBit  = 1 shl 0;
  SecondBit = 1 shl 24;
  BothBits  = FirstBit or SecondBit;

Dave

Other Threads