Compile time error in assignment statement

DelPhi1

I have this code:

TTimer(TimerList.Items[Row]) := TTimer.Create(Self);

When I run the program, there is the following error:

unit1.pas(70,19) Error: Argument cannot be assigned to

Why is this error thrown and how can I fix it?

J...

You're including an explicit typecast on the left hand side - this can be allowed for variables, but here Items is a property accessed via a getter and so it is not a variable but a function result. The only place you can typecast here, if needed, is on the right hand side of the assignment.

We don't know the type of TimerList, but assuming it is a compatible type like TList<TTimer> you can just assign it directly.

 TimerList.Items[Row] := TTimer.Create(Self);

If the list or array has elements of a different, compatible, but not implicitly type-castable type then you would typecast the right hand side of the assignment to match the expected type of the left hand side.


In the case of a plain TObjectList (from comments) the list elements are plain TObject, so any object, including a TTimer can be assigned to the list element as above. Where you would need to typecast is going the other way - assigning the TObject back to a TTimer variable :

var 
  LMyTimer : TTimer
begin
  LMyTimer := TTimer(TimerList.Items[someIndex]);

Also be careful that in creating the object with TTimer.Create(self) you are giving self (probably the form?) ownership of the timer object. By placing it into a TObjectList you are then also giving ownership of the object to the list. Both will try to free the object when they are themselves freed - whichever one does so last will cause a crash, so this is a bug.

Decide which object will have ownership responsibility and implement that - an object should not have two owners! Either have the TObjectList own the timer, and therefore create it with TTimer.Create(nil), or leave the ownership with the (form?) using TTimer.Create(self) and use a plain TList instead of a TObjectList.

If you have a modern version of Delphi, I would strongly prefer a typed generic list, either TList<TTimer> or TObjectList<TTimer>. This gets around the need to typecast entirely.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is a LINT/synthesis safe statement to throw an error at compile time?

Why a missing package statement does not give a compile time error in java?

Did not get a compile time error after deleting copy/move constructors and assignment operators during passing object to function

Why does the assignment of a short variable to an Integer reference produce a compile time error?

Why is Rust telling "unknown size at compile time" instead of another error in a (invalid) slice to slice assignment?

Compile-time assignment of consecutive numbers to #defines

Groovy Compile time AST transformation: Assignment to a field

combobox error at compile time

Gflags Compile time error

Redefinition error at compile time

Getting Compile time error

Compile Time Error

Compile error :lvalue required as left operand of assignment

Fortran Compile Error: Unclassifiable statement

WebStorm error: expression statement is not assignment or call

Borrowing error occurs when there is an assignment in an if statement

is it possible to check if statement containing an enum at compile time

Compile time error in C#

Why is this not a syntax / compile time error?

c++ compile time error

Unexpected compile time error with dynamic

Compile time error on online compilation?

Compile time error and Constructor "Super"

Linked list compile time error

How to catch at compile-time an assignment of a subtype to a supertype?

Assignment and increment at the same time gives error, why?

My VBA IF statement is generation a compile error

Error in task: syntax error in assignment statement l-value

Why does assignment in static init block compile without error?