BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to specify an object as a parameter of a procedure

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Gerhard Venter
Guest





PostPosted: Tue Nov 18, 2003 6:17 pm    Post subject: how to specify an object as a parameter of a procedure Reply with quote



When you pass an object as a parameter of a procedure, and in the procedure
you will change a property of the object do you specify the Object parameter
as var or as const or nothing?
I know that even if it is specified as const you can still change the
properties of the object but what I want to know is why or in what case
would you specify an object parameter as const? Do you need to specify it at
all? (because objects are dynamic pointers in Object Pascal and is it
implied that they are var?

Thanks
Gerhard


Back to top
Eric Hill
Guest





PostPosted: Tue Nov 18, 2003 6:38 pm    Post subject: Re: how to specify an object as a parameter of a procedure Reply with quote



Quote:
When you pass an object as a parameter of a procedure, and in the
procedure
you will change a property of the object do you specify the Object
parameter
as var or as const or nothing?

Typically nothing. An object variable is a special type of pointer under
the hood. You're basically passing a pointer to the object into the
routine.

Quote:
I know that even if it is specified as const you can still change the
properties of the object but what I want to know is why or in what case
would you specify an object parameter as const?

"const" parameters are used so the compiler can do a few optimizations
(like on string handling) and to ensure that you don't change the value
passed into the routine. Using a "const" on an object pointer ensures that
you can't change the pointer, but doesn't specify anything about the
contents (the object) of the pointer. As a general rule, you shouldn't
pass objects with the "const" parameter.

Quote:
Do you need to specify it at
all? (because objects are dynamic pointers in Object Pascal and is it
implied that they are var?

Just realize that an object passed into a routine without the "var" keyword
isn't a "var" because you're not changing the pointer, you're changing the
values of the object that the pointer points to. <g>

Eric



Back to top
Gerhard Venter
Guest





PostPosted: Tue Nov 18, 2003 7:03 pm    Post subject: Re: how to specify an object as a parameter of a procedure Reply with quote



Thanks Eric (second time you have helped me to clarify my own
interpretations, previously with RO Smile ). It is like looking at a word
sometimes and the spelling looks wrong but yet when you look it up it is
correct. And then there is the English language - not a very descriptive
language like many others :)

One more question : So if an object has the "var" keyword does it mean that
I can change the pointer? If that is true what will be an example when you
want to do that for instance?

By the way I decided to and have written an Indy framework with persistent
connections for my messaging project.

Gerhard

"Eric Hill" <eric (AT) ijack (DOT) net> wrote

Quote:
When you pass an object as a parameter of a procedure, and in the
procedure
you will change a property of the object do you specify the Object
parameter
as var or as const or nothing?

Typically nothing. An object variable is a special type of pointer under
the hood. You're basically passing a pointer to the object into the
routine.

I know that even if it is specified as const you can still change the
properties of the object but what I want to know is why or in what case
would you specify an object parameter as const?

"const" parameters are used so the compiler can do a few optimizations
(like on string handling) and to ensure that you don't change the value
passed into the routine. Using a "const" on an object pointer ensures
that
you can't change the pointer, but doesn't specify anything about the
contents (the object) of the pointer. As a general rule, you shouldn't
pass objects with the "const" parameter.

Do you need to specify it at
all? (because objects are dynamic pointers in Object Pascal and is it
implied that they are var?

Just realize that an object passed into a routine without the "var"
keyword
isn't a "var" because you're not changing the pointer, you're changing the
values of the object that the pointer points to.
Eric





Back to top
Mauricio Magni
Guest





PostPosted: Tue Nov 18, 2003 7:04 pm    Post subject: Re: how to specify an object as a parameter of a procedure Reply with quote

Hello Eric

"Eric Hill" <eric (AT) ijack (DOT) net> wrote

Quote:
As a general rule, you shouldn't
pass objects with the "const" parameter.

I usually pass objects as const, in order to avoid confuse my self and
assign a new object reference to the parameter passed. I think there is no
cost associated with const parameters. Can you explain me why I shouldn't do
this?
Thanks a lot.
Mauricio.





Back to top
Eric Hill
Guest





PostPosted: Tue Nov 18, 2003 8:25 pm    Post subject: Re: how to specify an object as a parameter of a procedure Reply with quote

Quote:
One more question : So if an object has the "var" keyword does it mean
that
I can change the pointer? If that is true what will be an example when
you
want to do that for instance?

The use of var to pass an object back is not very common, but is useful in
some situations. If you need to have a routine instantiate many variables
at once, this makes more sense:

procedure MakeInstances(var ObjLeft, ObjRight, ObjTop, ObjBottom: TSide);

It's not a great example though as you would more commonly have a TBox
object that has those four sides as properties :)

Quote:
By the way I decided to and have written an Indy framework with
persistent
connections for my messaging project.

Cool. I just started using the NexusDB transport system for just such a
need - it has been designed around messaging from the ground up so it lends
itself rather well to that functionality :)

Eric



Back to top
Eric Hill
Guest





PostPosted: Tue Nov 18, 2003 8:27 pm    Post subject: Re: how to specify an object as a parameter of a procedure Reply with quote

Quote:
I usually pass objects as const, in order to avoid confuse my self and
assign a new object reference to the parameter passed. I think there is
no
cost associated with const parameters. Can you explain me why I shouldn't
do
this?

I can't tell you you /shouldn't/ do this since it's not hurting you. <g>

I can only tell you that it's /more common/ to use the (AObject: TObject)
syntax over (const AObject: TObject).

Eric



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.