Forum > General
Fields and variables
(1/1)
typo:
Is there a significant difference between I use a TStringList as a field (FMyList) or as a variable?
Zoran:
Generally, yes, the difference is significant. However, in many common situations it will make no real difference.
Fist, I want to enfasize that for this observation the type of the field/variable is totally irrelevant -- the same applies to TStringList, Integer, String or any other type, might be simple, structured or class type.
The variables are global to application, so you have only one instance of a variable. The fields are class members, which means that every object of the class whose member this field is, will create separate field instance. So, if you need one StringList which will hold and change strings in the application, you may use a variable.
If your application is GUI-based (visual application, with forms), then you usually have form objects (at least one) which will be created when the application starts and reside in memory until the end of the application. Furthermore, it will be very uncommon that you create more than one instance of such form types. So, you can see that declaring a field in your main form class will actually lead to globally (application-wide) only one instance of that field, which can be used in the application quite same as if it were a variable.
If you, however, want to design a class which will be instantiated multiple times, you may want to declare some fields which will belong to that class.Those fields' roles cannot be substituted by variables.
You might find this confusing... Sorry, I had to assume your basic OOP knowledge. I advise you read these two chapters in Free Pascal Reference guide:
http://community.freepascal.org:10000/docs-html/ref/refch4.html
and
http://community.freepascal.org:10000/docs-html/ref/refch6.html
Navigation
[0] Message Index