Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To copy from one object to another, can I assign the variables directly, or must I assign their properties individually?

Tags:

delphi

I would like to clarify one doubt. In my current project I find that there are two TClientDataSet components. And one client dataset's properties and event handlers are assigned to another client dataset as below:

  cds2.AfterApplyUpdates := cds1.AfterApplyUpdates;
  cds2.AfterCancel       := cds1.AfterCancel;
  cds2.AfterClose        := cds1.AfterClose;

And

  cds2.CommandText       := cds1.CommandText;
  cds2.AutoCalcFields    := cds1.AutoCalcFields;
  cds2.DisableStringTrim := cds1.DisableStringTrim;

Is the above assignment of event handlers and properties required?

If we simply assign one client dataset to another as shown below, isn't it enough?

  cds2 := cds1;
like image 606
Vishal Tiwari Avatar asked Jul 22 '14 12:07

Vishal Tiwari


People also ask

How do you copy properties from one object to other?

assign(): By using the Object. assign() method, all enumerable properties of one or more source objects are copied to the target object. This method returns the modified target object. The properties of the target object are overwritten if they have the same key as properties in the sources.

How do you assign variables to objects?

You assign an object to such a variable by placing the object after the equal sign ( = ) in an assignment statement or initialization clause.

How do you assign values from one object to another in TypeScript?

To use the Object. assign() method in TypeScript, pass a target object as the first parameter to the method and one or more source objects, e.g. const result = Object. assign({}, obj1, obj2) . The method will copy the properties from the source objects to the target object.

How do you copy properties from one object to another in JavaScript?

Object.assign() The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

What happens when you copy an object with two variables?

When an object variable is copied, the reference is copied, but the object itself is not duplicated. Now we have two variables, each storing a reference to the same object: As you can see, there’s still one object, but now with two variables that reference it. We can use either variable to access the object and modify its contents:

What happens when you assign one variable to another variable?

This happens, because assignment of one object reference variable to another didn't create any memory, they will refer to the same object. In other words, any copy of the object is not created, but the copy of reference is created. For example,

How to copy properties from one object to another?

Object.assign is the standard way to copy properties from one object to another. It is often used for copying properties that are one-layer deep. (One-layer deep means there are no nested objects).

How do you assign an object to another object?

Object.assign () Method Among the Object constructor methods, there is a method Object.assign () which is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [ [Get]] on the source and [ [Set]] on the target.


2 Answers

Delphi classes are reference types. That means that when you define a variable whose type is a class, what you actually have is a reference, or pointer, to the object. And that makes the meaning of the assignment operator := be reference assignment rather than value assignment.

Suppose you have the following declarations:

var
  o1, o2: TObject;
....
o1 := TObject.Create;
o2 := o1;

At this point, you have created one object, and both o1 and o2 refer to, or point to, the same object. Any changes you make through the o1 reference are also visible through the o2 reference since there is only one object, or instance.

So, in your scenario, you have cds1 and cds2 that, presumably, refer to different instances. And that means that

cds2.CommandText := cds1.CommandText;

Copies the value of cds1.CommandText to cds2.CommandText.

That's totally different from

cds2 := cds1;

which copies references, and results in you losing track of the separate object that cds2 refers to.

Find more discussion of reference types and value types here: Why should we use classes rather than records, or vice versa?.

In summary, the two options that you present do very different things. The code that you currently use, which copies property values, presumably works. In which case, your suggested change to copy references will certainly not do what you want.

like image 119
David Heffernan Avatar answered Oct 13 '22 23:10

David Heffernan


First of all: All components events are actually just properties which reference to event methods which are then used as event handlers. So when you assing one event as

cds2.AfterApplyUpdates := cds1.AfterApplyUpdates;

you are only assigning reference to the same method. With events this is quite commonly used in order to avoid writing same code multiple times.

Second of all: As David already sad when you are assigning one object variable to another using

cds2 := cds1;

you are only assigning same reference to object instance to another variable. You are not copying any actual data of the object.

Third of all: Even if what you are asking would work I doubt you would wannt to use it. Why? Becouse doing so you would get two TClientDataSets which will be accesing same database. And having tho of these is just pointelss.

like image 2
SilverWarior Avatar answered Oct 14 '22 00:10

SilverWarior