Tech Tip: Basics of using Data Store and Class Store
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: August 20, 2024
Data stores and class stores are key concepts for manipulating objects and classes. Especially for developers new to 4D, it can be confusing to know how and when to use the ds and cs keywords which maniuplate the data store and class store respectively.
The ds keyword should be used when the developer wants to work with actual data. ds is used to manage objects that are defined by classes. When developers want to manipulate data within their application they can use ds like so:
$e:=ds.Rectangle.new() $e.width:=5 $e.height:= 6 $e.save() |
The above code creates an instance of the RectangleEntity dataclass and maniuplates an object that will be stored within the database. Class store and the cs keyword are more complex and have a broader scope of use than ds. cs stores all of the classes used by 4D, some of those classes being the ones that define tables like Rectangle that was used in the previous example.
Now if the developer wants to modify the definition of the RectangleEntity, they can do so using the class store. First create a new file in your methods folder like so:
The following code modifies the class store and adds to the definition of the RectangleEntity class the computed attribute "Area"
Class extends Entity Function get Area() $0:=This.Width*This.Height |
This attribute can be accessed by using:
$area:=ds.Rectangle.all().first().Area |
If instead the developer were to define Rectangle as its own class it would be defined like so:
Class constructor($width : Integer; $height : Integer) This.width:=$width This.height:=$height Function Area() $0:=This.width*This.height |
To instantiate this definition the cs keyword would be used like so:
$r:=cs.Rectangle.new() |
Developers can read more about data stores here and class stores here.