4D Blog

Home Product Shared Classes

Shared Classes

May 2, 2024

Product

Do you want a simplified way to use shared objects, following the classic object syntax and automatically calling use/end use when needed?

Fasten your seatbelts; here come the shared classes!

Shared classes creation & management

With 4D 20 R5, we added a new concept: Shared classes.

Shared classes can only be used to create shared objects and, as such, simplify their use. To create a shared class, you just need to add the shared keyword to its constructor:

//class SharedClass

shared Class constructor()

Shared classes can’t be used to create standard objects: every time you call new() to create a new object of this class, it will be a shared object.

Extension to class functions

The keyword shared can also be used for class functions, indicating that this function modifies the object. There’s no need to call use and end use inside the function, as they are automatically called when the function is called and exited. This is obviously valid for the singleton constructor.

shared Function myFunction()

This.attribute:="whatever"

Of course, you should take care when using the shared keyword for functions that don’t need it to avoid blocking other processes that try to use the object.

Properly using shared classes and functions should allow you to manipulate your shared objects as if they were normal objects, simplifying your code.

Inheritance Considerations

One last point you need to be careful about is that Non-shared classes can inherit from shared classes, but shared classes can’t inherit from non-shared classes. So, plan your object model accordingly.

Example

Let’s say you have a long calculation to perform, and you’d prefer to use a worker to avoid blocking your important processes. But, obviously, you need to retrieve the calculation result at some point. Let’s use this CalculationResult class:

// class CalculationResult

shared Class constructor()
	
  This.isFinished:=False
	
Function makeCalculation()

  ...
  This.finished:=True
	
shared Function set finished($finished : Boolean)
	
  This.isFinished:=$finished
	
shared Function get finished()->$finished : Boolean
	
  $finished:=This.isFinished

To use it, you can just do:

$calculation:=cs.CalculationResult.new()
CALL WORKER("AnyWorker"; Formula($calculation.makeCalculation()))

While (Not($calculation.isFinished))
  //Code that will be executed while the calculation is in progress
End while 

I hope the shared classes will help you use shared objects across your application. If you have any questions, don’t hesitate to bring them to the 4D forum.

Discuss

Tags 20 R5, 21, Class, Programming, Singleton

Latest related posts

  • June 16, 2025

    Generate, share and use web sessions One-Time Passcodes (OTP)

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

Nicolas Brachfogel
Nicolas Brachfogel
Product Owner & Senior Developer - Nicolas Brachfogel joined 4D in 2017 as senior developer (4D Server and networking) and as Product Owner to manage the 4D version on Apple Silicon. He is tasked with the redaction of user stories and functional specifications, as well as the verification that the new features are in line with customers' needs. With a degree from Institut Supérieur d'Informatique Appliquée Paris (INSIA), Nicolas started his career as a software developer in 2001. After many years of development in Java and C++, he specialized himself in client-server development in the video game industry. As a developer/software architect, he worked on the server architectures of many games (Dofus Arena, Drakerz, Trivial Pursuit Go !).
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy