General X++ Development
How do I create a singleton class in X++?
How do I create a singleton class in X++?
1 answer
Write an answerUse a private constructor and a static accessor that returns the shared instance.
final class MySingleton
{
private static MySingleton instance;
private void new()
{
}
public static MySingleton instance()
{
if (!instance)
{
instance = new MySingleton();
}
return instance;
}
}Only use a singleton when there should genuinely be one shared instance. Do not use it just because passing an object around feels inconvenient.
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer