Question

If I were to do the following, do you consider that to be copying an object from one variable to another or referencing where the pointer to an object is copied.

myPanel:Panel;
myControl:Control;

myPanel := new Panel;

myControl := myPanel;

if this is not referencing, then changing any setting with myControl will not change myPanel setting. Am I correct in saying that?

Était-ce utile?

La solution

In .net, most types -- particularly stuff as complex as Windows controls -- are reference types. This means the variable's value is just a reference to an object, and you're just copying references -- not whole objects -- when you assign variables of those types. The big result being that in your example, changing myControl would change myPanel (and vice versa), because the two refer to the same object.

To be sure, check the documentation. If the object's type inherits from System.ValueType (or System.Enum, which extends ValueType as well), then it's a value type, and the whole thing will get copied when you assign. Otherwise it's a reference type, and you're just copying references.

Autres conseils

Both myPanel and myControl are references. When you make the assignment you're copying a reference, not making a reference-to-a-reference. (Think about it as copying an address in memory if you like.)

However, when you call methods or set fields on myPanel or myControl, you're de-referencing, i.e. acting on the referenced object. This "changing settings" with myControl (I assume you mean setting fields) changes them in the same object referenced by myPanel.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top