Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2005, 02:05 AM
DelphiTinkerer DelphiTinkerer is offline
Junior Member
 
Join Date: Jan 2005
Posts: 15
Default How to use parts of a component as var

Hello

Would like to pass e.g. Edit.Text or ComboBox.Text or ComboBox.Items as a var into e.g.
Procedure DefineMyComponents(var Edit.Text : String;
var Box.Text : String;
var Box.Items : TStrings);
by calling it e.g. with
DefineMyComponents(MyForm.Edit1.Text,
MyForm.ComboBox1.Text,
MyForm.ComboBox1.Items);

The Compiler (D5S) does not accept this. But how should it be done to be correct?

If I pass the complete component as a var parameter it works, like
Procedure DefineMyComponents(var Edit : TEdit;
var Box : TcomboBox);
But how should it be done if one only would like to pass parts of the component and not the complete component?
Reply With Quote
  #2  
Old 01-30-2005, 05:26 AM
DelphiTinkerer DelphiTinkerer is offline
Junior Member
 
Join Date: Jan 2005
Posts: 15
Default RE: How to use parts of a component as var

Sorry, made a mistake in my original thread, but the QUESTION REMAINS.. The procedure I testet did not have periods, it did look like this example:

Procedure DefineMyComponents(var EditText : String;
var BoxText : String;
var BoxItems : TStrings);
Reply With Quote
  #3  
Old 01-30-2005, 07:41 AM
rsawoseyin rsawoseyin is offline
Senior Member
 
Join Date: Nov 2001
Posts: 515
Default RE: How to use parts of a component as var

The component properties are allocated space within the component, and could give unpredictable results when passed on their own. You should first assign those properties to local variables, and then use those local variables as arguments in your function call:

Code:
var
editText:String;
boxText:String
boxItems:TStringList;
begin
    editText:=TEdit1.Text;
    boxText:=TComboBox1.Text
    boxItems.Create(self);
    boxItems.Strings:=TComboBox1.Items;
    DefineMyComponents(editText,boxText,boxItems);
end
Raph Awoseyin
Port Harcourt, Nigeria
Reply With Quote
  #4  
Old 01-30-2005, 07:58 AM
DelphiTinkerer DelphiTinkerer is offline
Junior Member
 
Join Date: Jan 2005
Posts: 15
Default RE: How to use parts of a component as var

Thank you Ralph
What do you think of what I actually did before, to pass the complete component as a var?
Reply With Quote
  #5  
Old 01-30-2005, 08:17 AM
DelphiTinkerer DelphiTinkerer is offline
Junior Member
 
Join Date: Jan 2005
Posts: 15
Default RE: How to use parts of a component as var

Do I understand your sample code correct, that it doesn't need after

DefineMyComponents(editText,boxText,boxItems);

to actually define the components, such as e.g.

Edit1.Text:=editText;
Reply With Quote
  #6  
Old 01-30-2005, 09:51 AM
rsawoseyin rsawoseyin is offline
Senior Member
 
Join Date: Nov 2001
Posts: 515
Default RE: How to use parts of a component as var

My understanding is that you have entered some values into the TEdit and TComboBox fields and want to pass those values to the function DefineMyComponents. You do not need to assign the editText or the boxText to those components again. Edit1.Text is automatically assigned whatever text you entered into the Edit1 text field. The same goes for the ComboBox.

Passing the components to the function is also ok. In fact, it may consume less overhead because only the addresses of those components are passed to the function. The function then reads the property values from those addresses. You then save yourself the additional memory required to hold those temporary variables.

Raph Awoseyin
Port Harcourt, Nigeria
Reply With Quote
  #7  
Old 01-30-2005, 10:45 AM
DelphiTinkerer DelphiTinkerer is offline
Junior Member
 
Join Date: Jan 2005
Posts: 15
Default RE: How to use parts of a component as var

Thank you Raph
DefineMyComponents is a procedure which defines equivelent components on different forms. So certain components in the form receive their settings only through this procedure. In my previous version I passed the complete components as var parameters, this until someone told me, that it would be better to do it only with the properties needed. I thought first, that it would be an easy task to change it. But now I get the impression that this latter solution becomes much more complicated to what I already had...
Reply With Quote
  #8  
Old 01-30-2005, 11:03 AM
rsawoseyin rsawoseyin is offline
Senior Member
 
Join Date: Nov 2001
Posts: 515
Default RE: How to use parts of a component as var

If you want to change the properties of a component (like a TEdit) in a separate procedure, you MUST pass the address of the component to that procedure, and that is what you do when you have the components as arguments in the procedure call. This is true of all VCL components. But when you pass only the property value (like Edit1.Text), you are not passing by reference and so the procedure you are calling CANNOT change that value.

Summary: A procedure outside the class in which an object is defined can only change the properties of the object if the object is passed to it by reference - not by passing the object property to it.

Raph Awoseyin
Port Harcourt, Nigeria
Reply With Quote
  #9  
Old 01-30-2005, 11:28 AM
DelphiTinkerer DelphiTinkerer is offline
Junior Member
 
Join Date: Jan 2005
Posts: 15
Default RE: How to use parts of a component as var

Thank you Raph

It remains now to set my program back to the state where I started to change it. At least I know now that what I did was correct without realy knowing it...

Greetings
Marcel from Switzerland but temp@Istanbul
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 06:20 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.