Ian Branch 127 Posted June 3, 2021 Hi Team, I have never knowingly used the 'as' operator and have no idea what it is about and how to use it. I looked at the EMB description of the operator but was none the wiser. So, 1. can somebody point me to a clear explanation of the use of the 'as' operator, preferably with examples, and 2. I have the following code.. pptr := PaPInAddr(phe^.h_addr_list); Which also uses something that I never caught on to as well, pointers. Using Pascal Analyzer, it suggests I should use 'as' somewhere in there. Could someone enlighten me where and why it may be better. Although that insight may come from 1. above. Regards & TIA, Ian Share this post Link to post
Lars Fosdal 1792 Posted June 3, 2021 Ref.1: The as operator is a checked typecast. Let's say that you have a method that takes a TObject as parameter aInstance. The difference between a simple typecast and a checked typecase is that var Unchecked := TMyClass(aInstance); will never complain, while var Unchecked := aInstance as TMyClass; will raise an exception if aInstance is not a TMyClass Also see the is operator which can be used to validate the type before the cast. if aInstance is TMyClass then (aInstance as TMyClass).SomeTMyClassMethod; Share this post Link to post
Fr0sT.Brutal 900 Posted June 3, 2021 5 hours ago, Ian Branch said: 2. I have the following code.. pptr := PaPInAddr(phe^.h_addr_list); Which also uses something that I never caught on to as well, pointers. Using Pascal Analyzer, it suggests I should use 'as' somewhere in there. PA is wrong here. "AS" only deals with classes (you can try it yourself) Share this post Link to post
Anders Melander 1783 Posted June 3, 2021 5 hours ago, Ian Branch said: 2. I have the following code.. pptr := PaPInAddr(phe^.h_addr_list); Which also uses something that I never caught on to as well, pointers. Using Pascal Analyzer, it suggests I should use 'as' somewhere in there. I think Pascal Analyser is wrong in this case. If PaPInAddr is a pointer type then 'is' and 'as' can't be used since these only operate on object types (i.e. object references/pointers). Share this post Link to post