![]() |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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); |
|
#3
|
|||
|
|||
|
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
Port Harcourt, Nigeria |
|
#4
|
|||
|
|||
|
Thank you Ralph
What do you think of what I actually did before, to pass the complete component as a var? |
|
#5
|
|||
|
|||
|
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; |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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... |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|