Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-01

Delphi incompatibility - hard cast

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alejandro Gonzalo

In Delphi this will work even if the Sender is not a TdbGrid, as long as it has a DataSource property:

? TdbGrid(Sender).datasource:=myDataSource;

But it won't work in Lazarus if Sender is a different dbAware component., the compiler will complain it's not a dbGrid.

Is there anyway to accomplish this same result in Lazarus, perhaps by using RTTI?? Or is there a compiler directive to stop this check?

An example would be great.? Thanks.

A. G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140117/40a92f33/attachment.html>

Reply by email

Hans-Peter Diettrich
to Alejandro Gonzalo
In Delphi this will work even if the Sender is not a TdbGrid, as long as it has a DataSource property:

It only *happens* to work for you in Delphi.

TdbGrid(Sender).datasource:=myDataSource;

This hack requires that Sender has a DataSource property in *exactly* the same place where it sits in TdbGrid. That's very unlikely for any other class.

DoDi

Reply by email

Bob Hofman
to Hans-Peter Diettrich

Depending on what the Sender is it might work if it can be cast to a TDBGrid. Can you actually run an application with that cast? What is the Sender?

I put a DBGrid and a DBMemo on a form and in the OnShow assigned the DataSource property of each to a DataSource I had also placed on the form. Everything worked as expected. Then I changed the assignment to the DBMemo to the following:

TDBGrid(DBMemo1).DataSource := DataSource1;

Both a syntax check and a build did not throw any errors but if I attempted to run the application it would error out with an access violation as soon as it got to the above assignment.

I think that FPC is just catching it at compile time while Delphi is not.

Bob

Hans-Peter Diettrich wrote 24 lines
Alejandro Gonzalo schrieb: In Delphi this will work even if the Sender is not a TdbGrid, as long as > it has a DataSource property: > It only *happens* to work for you in Delphi. TdbGrid(Sender).datasource:=myDataSource; > This hack requires that Sender has a DataSource property in *exactly* the same place where it sits in TdbGrid. That's very unlikely for any other class. DoDi -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140117/ed644fc4/attachment.html>

Reply by email

Marco van de Voort
to Bob Hofman
Bob Hofman wrote 5 lines
Both a syntax check and a build did not throw any errors but if I attempted to run the application it would error out with an access violation as soon as it got to the above assignment. I think that FPC is just catching it at compile time while Delphi is not.

Note that there is also a runtime check available for this in FPC. It is called -CR and very, very useful.

Reply by email

Sven Barth
to Marco van de Voort
Marco van de Voort wrote 9 lines
On Fri, Jan 17, 2014 at 09:55:25PM -0500, Bob Hofman wrote: > Both a syntax check and a build did not throw any errors but if I attempted > to run the application it would error out with an access violation as soon > as it got to the above assignment. > I think that FPC is just catching it at compile time while Delphi is not. Note that there is also a runtime check available for this in FPC. It is called -CR and very, very useful.

And in trunk it even raises an explicit exception class instead of merely an exception: EObjectCheck. :)

Regards,
Sven

Reply by email

Alejandro Gonzalo

OK, how about a compiler switch (that can be turned on or off) that SEARCHES for the property (or event if defined in code and not by the Object Inspector)?? It shouldn't be so difficult to deal with Sender (or components in a loop) where the exact Type is not known in advance, just the presence of a specific property or Event.? If the switch can be turned Off again after the routine, the impact on performance shouldn't be a concern.? Greater flexibility and ease of use, especially for newbies (RTTI is difficult and poorly documented) is needed if freepascal/Lazarus is to become universally adopted.

Can there please be a discussion on this?? Thanks!

A. G.

On Thursday, January 30, 2014 3:58 PM, Alejandro Gonzalo wrote:

OK, how about a compiler switch (that can be turned on or off) that SEARCHES for the property (or event if defined in code and not by the Object Inspector)?? It shouldn't be so difficult to deal with Sender (or components in a loop) where the exact Type is not known in advance, just the presence of a specific property or Event.? If the switch can be turned Off again after the routine, the impact on performance shouldn't be a concern.? Greater flexibility and ease of use, especially for newbies (RTTI is difficult and poorly documented) is needed if freepascal/Lazarus is to become universally adopted.?

Can there please be a discussion on this?? Thanks!

A. G.

On Friday, January 17, 2014 6:22 PM, Hans-Peter Diettrich wrote:

In Delphi this will work even if the Sender is not a TdbGrid, as long as it has a DataSource property:

It only *happens* to work for you in Delphi.

? TdbGrid(Sender).datasource:=myDataSource;

This hack requires that Sender has a DataSource property in *exactly* the same place where it sits in
TdbGrid. That's very unlikely for any
other class.

DoDi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140130/646e64ee/attachment.html>

Reply by email

Michael Van Canneyt
to Alejandro Gonzalo
Alejandro Gonzalo wrote 6 lines
OK, how about a compiler switch (that can be turned on or off) that SEARCHES for the property (or event if defined in code and not by the Object Inspector)?? It shouldn't be so difficult to deal with Sender (or components in a loop) where the exact Type is not known in advance, just the presence of a specific property or Event.? If the switch can be turned Off again after the routine, the impact on performance shouldn't be a concern.? Greater flexibility and ease of use, especially for newbies (RTTI is difficult and poorly documented) is needed if freepascal/Lazarus is to become universally adopted.

You just need to understand what you are doing, and use

   SetObjectProp(Sender,'Datasource',MyDataSource);

that's all there is to it.

Michael.

Reply by email

Alejandro Gonzalo
to Michael Van Canneyt

Is there also a way of setting an event such as OnEnter for Sender or a Component in a loop (if the Event is defined in code and not with the Object Inspector)?

Thank you.

A. G.

On Friday, January 31, 2014 12:19 AM, Michael Van Canneyt wrote:

Alejandro Gonzalo wrote 6 lines
OK, how about a compiler switch (that can be turned on or off) that SEARCHES for the property (or event if defined in code and not by the Object Inspector)?? It shouldn't be so difficult to deal with Sender (or components in a loop) where the exact Type is not known in advance, just the presence of a specific property or Event.? If the switch can be turned Off again after the routine, the impact on performance shouldn't be a concern.? Greater flexibility and ease of use, especially for newbies (RTTI is difficult and poorly documented) is needed if freepascal/Lazarus is to become universally adopted.

You just need to understand what you are doing, and use

?  SetObjectProp(Sender,'Datasource',MyDataSource);

that's all there is to it.

Michael.

Reply by email