Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

typed constants -v- true constants 1

Status
Not open for further replies.

mmaxwell

Programmer
Oct 27, 2003
4
GB
I am new to Delphi.

The following should work -

procedure TForm1.Button1Click(Sender: TObject);
const a: Integer=0;
begin
a:=7;
end;

I get error "Left side cannot be assigned to"

I would expect this if I used const a:=12; but not as above.

I would appreciate some guidance.
 
Delphi is right :) Constants cannot be assigned to aside from an initial declaration.

The difference between typed and non-typed constants is simply that Delphi will implicitly type a constant if you don't specify one. Consider the following:

procedure UseNumber(AInteger: Int64);
begin
...
end;

const
xxHelp = 0;
begin
UseNumber(xxHelp);
end.

In this example it will cause a compiler error, because xxHelp has been implicitly declared as an Integer, and therefore doesn't match the type of the UserNumber procedure. However, changing the const declaration to:

const
xxHelp : Int64 = 0;

will allow the program to compile.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top