In case of an object (class instance), it is the instance (reference
in the variable) that is affected by "var". Not the data in the instance.
There is only one instance. So the data in the instance (as you found out) is always the same.
Now you have 2 variables. They can both point to the same instance, or they can have different instances
var a, b: Tstringlist;
begin
a:= Tstringlist.create;
b:= a; // only one instance // 2 vars refering to it
b.add('foo');
writeln(tsl.text);
freeAndNil(a);
// b in NOT nil. But it is NO LONGER VALID, the data it points to is gone
end.
{$mode objfpc}
uses classes;
procedure myproc(var slist: Tstringlist); // NO var OR out KEYWORD USED HERE;
begin
freeandnil(slist);
end;
var
tsl: Tstringlist;
begin
tsl:= Tstringlist.create;
myproc(tsl);
// tsl is nil
end.
Without the var param, tsl would NOT be nil. Yet tsl would be invalid.
{$mode objfpc}
uses classes;
procedure myproc(var slist: Tstringlist); // NO var OR out KEYWORD USED HERE;
begin
slist := TMySubclassedStringList.create.
end;
var
tsl: Tstringlist;
begin
tsl:= Tstringlist.create;
myproc(tsl);
// tsl is TMySubclassedStringList
end.
without the var, tsl would still be a valid stringlist.
In this case you did not free the stringlist (may lead to a leak). So without "var" tsl stays valid, as the stringlist's data still exists.