.netwinformsoxygene

Is this copying or referencing?


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?


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.