Board index » delphi » Typed Constants

Typed Constants

Ok, here's my prob. Any help will be greatly appreciated.
UNIT Service;
USES Crt;

INTERFACE
Etc...(s:string);

IMPLEMENTATION

CopyAttr: Byte= TextAttr; <---Error 133 Cannot evaluate this expression

BEGIN
END.

Does anyone know why my compiler won't evaluate this? If at all
possible, could I get a a private reply or at least forward the message
to me personally? I've been away for a while that I have quite a few
unread messages. Thanks.

 

Re:Typed Constants


Quote
> Ok, here's my prob. Any help will be greatly appreciated.
> UNIT Service;
> USES Crt;
> INTERFACE
> Etc...(s:string);
> IMPLEMENTATION
> CopyAttr: Byte= TextAttr; <---Error 133 Cannot evaluate this expression

> BEGIN
> END.

> Does anyone know why my compiler won't evaluate this? If at all
> possible, could I get a a private reply or at least forward the message
> to me personally? I've been away for a while that I have quite a few
> unread messages. Thanks.

   TextAttr is a system variable (not a constant), so it can't be used in
this context.  If you wish to use the value of TextAttr, you'll have to
explicitly assign it to a variable of that type.  Also, you don't have
the declaration of a typed constant here - unless there's a "Const"
preceding this declaration...

Re:Typed Constants


Quote
Mike Copeland wrote:

> > Ok, here's my prob. Any help will be greatly appreciated.
> > UNIT Service;
> > USES Crt;
> > INTERFACE
> > Etc...(s:string);
> > IMPLEMENTATION
> > CopyAttr: Byte= TextAttr; <---Error 133 Cannot evaluate this expression

> > BEGIN
> > END.

> > Does anyone know why my compiler won't evaluate this? If at all
> > possible, could I get a a private reply or at least forward the message
> > to me personally? I've been away for a while that I have quite a few
> > unread messages. Thanks.

>    TextAttr is a system variable (not a constant), so it can't be used in
> this context.  If you wish to use the value of TextAttr, you'll have to
> explicitly assign it to a variable of that type.  Also, you don't have
> the declaration of a typed constant here - unless there's a "Const"
> preceding this declaration...

Oops. Even though I left it out here, the CONST keyword *is* in my unit.
So, what should I do? This?

VAR
  Temp: Byte= TextAttr;

CONST
  CopyAttr: Byte= Temp;

Would that work? Oh well, I'm gonna try it right now so I'll know the
answer shortly.

Re:Typed Constants


Nope, i dont think this will work. For a types constant, the value must be
known at compile-time. It's stored in the EXE file. If you try to put a
variable into a initialized constant you use run-time information. This
works:

Const
  SomeConst : Byte = 2;

Pay attention to the following. SomeValue is not a Const but a variable
that is a initial value that is stored in your EXE file. The discussion is
however if this is something you should do. From a programmer point of view
(ok, my point of view) you should avoid this akward declaration. Use it
with care. There's always the following alternative:

Unit Example;

Interface
Uses
  Crt;

{some stuff}

Implementation

Var
  CopyAttr : Byte;

Begin
  {unit init stuff}
  CopyAttr := TextAttr;
End.

In fact you have declared a global variable CopyAttr with this unit as
scope. Not a nice situation but for what i see this is what you want.

Cheers,
Anco.

Re:Typed Constants


Quote
> Oops. Even though I left it out here, the CONST keyword *is* in my unit.
> So, what should I do? This?

> VAR
>   Temp: Byte= TextAttr;

> CONST
>   CopyAttr: Byte= Temp;

> Would that work? Oh well, I'm gonna try it right now so I'll know the
> answer shortly.

   No, that won't work, either.  It seems you have the whole concept of
typed constants wrong, because assigning a constant the value of a
variable isn't what it's all about.  Besides, here you have declared a
Var prior to the Const, and that's not strictly allowed - it makes no
senses to do this.
   Typed constants are designed to give you the ability to "initialize
variables at compile time", which you certain;y aren't doing with this
code.  It's an extension of Pascal (which doesn't support such things in
the language) which mirrors what some other languages can do, such as
COBOL.  It's _actually_ having the compiler generate some extra hidden
initialization code to do the work, because typed constants are really
variables - you can change their values during execution, so they're not
"constants" at all.  Here's how it's done:

Const X : byte = -17;  { assign initial value to variable X }
Const Months : array[1..12] of string[9] = ('January','February','March',
              'April','May','June','July','August','September',
              'October','November','December');

Re:Typed Constants


Quote
Mike Copeland wrote:

> > Oops. Even though I left it out here, the CONST keyword *is* in my unit.
> > So, what should I do? This?

> > VAR
> >   Temp: Byte= TextAttr;

> > CONST
> >   CopyAttr: Byte= Temp;

> > Would that work? Oh well, I'm gonna try it right now so I'll know the
> > answer shortly.

>    No, that won't work, either.  It seems you have the whole concept of
> typed constants wrong, because assigning a constant the value of a

I agree. That CopyAttr: Byte= Temp is straight out of one of my Pascal
books. It's my fault, the book is written by 3 russians. That's probably
where my problem lies. I've used what you said (CopyAttr: Byte= 29) so I
just didn't know why this code in the book was wrong.

Re:Typed Constants


Quote
Mike Copeland <mrc...@primenet.com> writes:
>    Typed constants are designed to give you the ability to "initialize
> variables at compile time", which you certain;y aren't doing with this
> code.  It's an extension of Pascal (which doesn't support such things in
> the language) which mirrors what some other languages can do, such as
> COBOL.  It's _actually_ having the compiler generate some extra hidden
> initialization code to do the work,

That's not correct. There is no code generated to initialize the typed
"constants". Instead, their initial values are directly stored in the
data segment part of the .EXE file. Of course, this doesn't matter too
much, because it only makes a difference of a few bytes in code and a few
clock cycles at program start...

Frank

Other Threads