final class VKTest { /// <summary> /// Class entry point. The system will call this method when a designated menu /// is selected or when execution starts and this class is set as the startup class. /// </summary> /// <param name = "_args">The specified arguments.</param> public static void main(Args _args) { info('Start using'); using(VKTestContext context = VKTestContext::construct()) { context.boolProperty = true; VKTestExecutable exec = new VKTestExecutable(); exec.performAction(); } VKTestExecutable exec = new VKTestExecutable(); exec.performAction(); info('End using'); // try-finally alternative info('Start try'); VKTestContext context = VKTestContext::construct(); try { context.boolProperty = true; exec = new VKTestExecutable(); exec.performAction(); } finally { context.dispose(); context = null; } exec = new VKTestExecutable(); exec.performAction(); info('End try'); } }
Start usingTrueFalseEnd usingStart tryTrueFalseEnd try
/// <summary> /// <c>VKTestContext</c> contains ... context used to ... /// </summary> class VKTestContext implements System.IDisposable { static VKTestContext instance; public boolean boolProperty; protected void new() { if(instance) { // Nesting of %1 is not supported throw error(strFmt("@WAX:NestingOfContextIsNotSupportedWarning", classStr(VKTestContext))); } instance = this; } /// <summary> /// Construct method to create new instance /// </summary> /// <returns>VKTestContext</returns> static public VKTestContext construct() { VKTestContext ret = new VKTestContext(); return ret; } public void dispose() { instance = null; } /// <summary> /// Returns current VKTestContext instance if it was previously created /// </summary> /// <returns>VKTestContext</returns> static public VKTestContext current() { return instance; } }
class VKTestExecutable { void performAction() { VKTestContext context = VKTestContext::current(); if (context && context.boolProperty) { info("True"); } else { info("False"); } } }https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern
If you found value in what I share, I've set up a Buy Me a Coffee page as a way to show your support.
Buy Me a CoffeeNo comments. Be the first one to comment on this post.
DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics 365 FO.
Authors are allowed to set their own "buy me a coffee" link.
Join us.