Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » Elevate Web Builder Technical Support » Support Forums » Search Forums » Search Results » View Thread |
Messages 1 to 7 of 7 total |
using const in parameters |
Fri, Nov 25 2022 4:38 AM | Permanent Link |
Ronald | Hi, In functions declarations I often see that CONST is used like this: procedure alert(const message: String); What is the difference if I do not use it? Is is more memory efficient? I can not find anything about it in the manual (however I remember I have read about it). Thanks, Ronald |
Fri, Nov 25 2022 2:11 PM | Permanent Link |
erickengelke | Ronald wrote: > In functions declarations I often see that CONST is used like this: > >procedure alert(const message: String); In general, this is usually a clue to the compiler to tell it to not let you assign a value to the variable inside a string or else raise an error. It's particularly important when you pass by reference rather than by value. For example, if you pass a TEdit, you are passing the reference and not a copy of the thing, so you may not want it modified by the code. Here's how you can protect it easily from manipulation. I don't have inside knowledge of EWB, but that's a general programming recommendation and compiler course concept that gets taught, so it's probably true here. Erick EWB Programming Books and Component Library http://www.erickengelke.com |
Fri, Nov 25 2022 2:11 PM | Permanent Link |
erickengelke | Ronald wrote: > In functions declarations I often see that CONST is used like this: > >procedure alert(const message: String); In general, this is usually a clue to the compiler to tell it to not let you assign a value to the variable inside a string or else raise an error. It's particularly important when you pass by reference rather than by value. For example, if you pass a TEdit, you are passing the reference and not a copy of the thing, so you may not want it modified by the code. Here's how you can protect it easily from manipulation. I don't have inside knowledge of EWB, but that's a general programming recommendation and compiler course concept that gets taught, so it's probably true here. Erick EWB Programming Books and Component Library http://www.erickengelke.com |
Sat, Nov 26 2022 6:14 AM | Permanent Link |
Ronald | erickengelke wrote: > In functions declarations I often see that CONST is used like this: > >procedure alert(const message: String); >In general, this is usually a clue to the compiler to tell it to not let you assign a value to the variable inside a s>tring or else raise an error. So with CONST every parameter is a reference and is therefor less memory intensive because there is no need for a copy of the variable each time the function/procedure is called. Ronald |
Sat, Nov 26 2022 1:07 PM | Permanent Link |
erickengelke | Ronald wrote: erickengelke wrote: >?In general, this is usually a clue to the compiler to tell it to not let you assign a value to the variable inside a s>tring or else raise an error. >So with CONST every parameter is a reference and is therefor less memory intensive because there is no need >for a copy of the variable each time the function/procedure is called. No, most variables are passed by value, not by reference, the exception is Objects which are by reference. In Pascal you normally use var to pass by reference for things like Integers or Strings, though I don't believe EWB's transpolar supports that. Erick EWB Programming Books and Component Library http://www.erickengelke.com |
Sun, Nov 27 2022 12:25 AM | Permanent Link |
erickengelke | erickengelke wrote: Ronald wrote: erickengelke wrote: >?In general, this is usually a clue to the compiler to tell it to not let you assign a value to the variable inside a s>tring or else raise an error. function test( const inp : string ) : string; begin inp := 'asdf'; result := inpt; end; This fails saying something about not being able to assign to inp. It's a compiler-enforced convention so that you know the value will stay unchanged throughout the function. You can also declare TObject and TExtneralObject based variables as Const, but it doesn't generate an error when you do things such as setting Visible (which calls inp.setvisible( True ); function test( const inp : TForm1 ) : string; begin inp.Visible := True; result :='asdf'; end; Many compilers only support const on "scalars". EWB doesn't;'t complain on objects, but it doesn't necessarily respect the Const then. Erick EWB Programming Books and Component Library http://www.erickengelke.com |
Tue, Nov 29 2022 7:46 AM | Permanent Link |
Raul Team Elevate | << Ronald wrote: In functions declarations I often see that CONST is used like this: procedure alert(const message: String); What is the difference if I do not use it? Is is more memory efficient? I can not find anything about it in the manual (however I remember I have read about it). >> As Erick already mentioned It just helps compiler and at dev time to avoid simple mistakes - i.e. setting const generates EWB compiler errors if you try to assign anything to it. If you look at the actual generates javascript the functions are identical since javascript always passes primitives by value. Objects are passed technically by value as well but since they are memory addresses it's really value of the reference - so you can modify content of object passed in but cannot change object to a different object (i.e. caller would always retain their object reference) Raul |
This web page was last updated on Thursday, December 5, 2024 at 09:15 PM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |