Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Suggested answer

Getters, setters and default values.

Posted on by 3,850

Hello

I have wrote a Class with method who get and set values to a variable like this.

Public SerialNumber serialNumber(SerialNumber _serialNumber = serialNumber)

{

serialNumber = _serialNumber;

return serialNumber;

}

But I want to set a default value "999999" to the variable: serialNumber.

What is the best way to do that?

Should I implement the default value to this get-set method, and if so how do I do that?

But should I write another method where I assign "999999" to the serialNumber variable before i use the above method?

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 287,914 Super User on at
    RE: Getters, setters and default values.

    Hi Sandri,

    You can use Args, but that is useful when you have a (temporary) table. If you want to communicate some values only (no table), you can better construct the class from the form and use some 'parm...'- methods which you can create on the class.

    There is a nice sample within the form and class both named: 'UnitOfMeasureCalculator'.

  • Sandri Profile Picture
    Sandri 3,850 on at
    RE: Getters, setters and default values.

    Hi Andre.

    The answer about default values is yes, it's always same values and user can change is trough form if he want or he can leave it unchanged.

    -So I thought about the solution by writing a class, assign default values in methods, and use Args to communicate between Classes and Forms where the default values is displayed.

    -Do you have any comments on that?

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 287,914 Super User on at
    RE: Getters, setters and default values.

    Hi Sandri,

    The answer of Nitesh will work when the default is really always the same value. If you have a process where the last user values needs to be retrieved and only defaulted when there are no user values found, you can add an override method called 'initParmDefault' on you class.

    See a sample at the class 'ProdParmStartup'.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getters, setters and default values.

    actually for this kind of Getters, setters methods, the variable serialNumber use to be global variable and defined in Classdeclaration

    best practice to initialize the global variable use to be when class object is initialized,

    any class when it get initialized it calls the method called new()

    so in new() method og the class.. write following statement

    serialNumber = "999999";

    hopefully this will solve your problem.

    Please validate.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,914 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,549 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans