Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

passing reference parameter to method

Posted on by 1,531

Hi Friends,

in AX, there is no ref parameter like other language, what's alternative then? If I need to pass something to method, and get updated value back.

 

Thanks,

*This post is locked for comments

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: passing reference parameter to method

    Strictly speaking, everything in X is passed by value; it doesn't support passing by reference (with the exception of .NET Interop).

    I guess you're interested in how table buffer behaves and you just got confused by the whole "by reference" thing. Although everything it passed by value, it makes a huge difference if you pass a value type or a reference type.

    If you pass an object reference, you refer to the same object, but it doesn't mean that the reference itself wasn't passed by value. Here "a reference" and "by reference" means two different things. One means that the value is just a reference to the object heap, while the latter means that you would get a pointer to the value and not the value itself.

    Tables are reference types, therefore although references are passed by values, they point to the same object and you can change the same object from any place (where you have the reference). For example, the following code will display "Changed":

    public void run()
    {
        CustGroup cg;
        cg.CustGroup = 'Original';
        
        this.changeBuffer(cg);
        info(cg.CustGroup);
    }
    
    void changeBuffer(CustGroup _cg)
    {
        _cg.CustGroup = 'Changed';
    }

  • maram fraij Profile Picture
    maram fraij 11,873 on at
    RE: passing reference parameter to method

    When I pass a table buffer , is it passed by values or by reference as per my test it seems it is passed by values by I need to confirm my conclusion ?

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: passing reference parameter to method

    X++ is not able to pass value types by reference, because it doesn't support it. Although it may simplify some cases, it's never necessary, therefore you should change your design to something that makes sense in X++.

    By the way it has nothing to do with object-oriented programming. On the contrary, it was important for structured languages such as C and it's not needed in object-oriented programs anymore, because you can use... objects.

  • Ganriver1 Profile Picture
    Ganriver1 1,531 on at
    RE: passing reference parameter to method

    Hi Hariharan,

    Passing parameter by ref is so common in other OOLs, in my case, just want get a simple record count back from method.

    For whatever reason, X++ is not able to pass reference. Thanks everybody's input. I know what to do next.

  • Verified answer
    Hariharans87 Profile Picture
    Hariharans87 4,971 on at
    RE: passing reference parameter to method

    You can use the X++ keyword byref to call .NET methods that take parameters by reference.

    In all X++ methods, all parameters are passed by value. In .NET, parameters can be passed by value or by reference.

    You must understand the parameter passing concepts of by value versus by reference before you can correctly use the byref keyword.

    If you want to return a single value you can simply return a value by using function.

    If you want to pass the parameter by reference, x++ is not supported, you can use .Net methods by calling from x++.

    Could you please explain me that why you want to pass the parameter by reference?

    Please refer this link msdn.microsoft.com/.../cc586700.aspx

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: passing reference parameter to method

    If you "want to return a simple integer from method", just return the integer at the return value, as Bhaskar Roy suggested.

    If you want to return a reference, use a reference type, as I suggested. Whether you consider it as a overkill or not is irrelevant.

    Boxing/unboxing wouldn't help you in any way.

  • 5400 Profile Picture
    5400 7,160 on at
    RE: passing reference parameter to method

    could I get more inside end to end.

  • Ganriver1 Profile Picture
    Ganriver1 1,531 on at
    RE: passing reference parameter to method

    Hi Bhaskar,

    This is not what I want. Just need to pass parameter.

  • 5400 Profile Picture
    5400 7,160 on at
    RE: passing reference parameter to method

    It is very simple please create your method return type integer and return the value. I don't know, I am missing anything or not.

  • Ganriver1 Profile Picture
    Ganriver1 1,531 on at
    RE: passing reference parameter to method

    Hi Martin,

    I just want to return a simple integer from method, using class seems to me is overkill.

    Also, as others said, using Container to do it, is this something like boxing/unboxing in X+?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans