Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Problem with variables declared in ClassDeclaration

(0) ShareShare
ReportReport
Posted on by 2,987
This must be an easy one, but I'm quite new at programming in AX. The thing is I've created a class, and delcared the main function as static, so I can directly run it from the AOT. Inside the main method, I call another method1, and from this I call Method2, both void methods.. Now I have created a NoYes Variable in the ClassDeclaration. So when I call Method2 from Method1, I change the value of the NoYes variable to 'Yes', but when I return tu the Method1, the value of the variable returns to value 'No' Why is the variable declared in ClassDeclaration changing?? How Can I prevent this behaviour?? Thanks.. Regards.. Jack

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    Re: Re: Re: Problem with variables declared in ClassDeclaration

    Use this as an example:

    class test
    {
        NoYes var;
    }

    static void main(Args args)
    {
       test t;
       ;
       t = new test();
       t.method1();

    }

    void method1()
    {
        ;
        this.method2();
       
        info(strfmt("%1", var));

    }

    void method2()
    {
        ;
        var = NoYes::Yes;

    }

  • Ghetz Profile Picture
    Ghetz 2,987 on at
    Re: Re: Problem with variables declared in ClassDeclaration
    Thanks for your answer.. Certainly I'm using the debugger to see the value of the variable in each method.. In method1 the value is 'No'... Then from Method1 I call method2, and inside method2 I change the value to 'yes'.. However, when method2 finishes and returns to method1, the value of the variable is going back to 'Yes' My code is quite simple.. something like void method1() { method2(); if (!var) print 'OK'; } void method2() { var = YesNo::Yes; } During method2, i see in debugger that value of var changes from No to Yes, but when returning to method1, the value is No, even though var is declared in the ClassDeclaration()
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    Re: Problem with variables declared in ClassDeclaration

    Hi,

    Can you paste your code?  It is most likely due to where you are setting the variable in Method1.  It would be really easy to tell if you showed the code.  One thing you can do is output your variable in every single method so you can see the value in each method call.  i.e.:  

    static void NoYesExp(Args _args)
    {
        NoYes var;
        ;
       
        var = NoYes::No;
       
        info(strfmt("%1",var));
    }

    Or you can use the debugger to see the variable values as you step through the code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans