web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How do I pass a class instance as a parameter to be used in another class

(0) ShareShare
ReportReport
Posted on by 1,455

In class InventTransferUpdReceive, method: UpdateInventTransferLine I initiate my custom class. I assigned values to the assessor methods (parm methods).

Then in class InventMovement, method UpdateLedgerAdjust I need to call a method on my custom class. I cannot in here use CustomClass A = new CustomClass() because then it is a new instance and all parm-methods will be empty.  

InventTransferUpdReceive.UpdateInventTransferLine calls InventUpd_Financial at one point, wich calls inventUpd_financial.UpdatefinancialIssue which will call movement.UpdateLedgerAdjust.

I cannot pass my custom class as an argument down the line because it is the InventMovement class which is called everywhere in AX. So it is safer to create a parm-method on InventMovement and when it is initiated in  InventTransferUpdReceive.UpdateInventTransferLine, to set the instance of my custom class in the parm-method.

In InventTransferUpdReceive.UpdateInventTransferLine, InventMovement is initiated as follow:

 movementTo          = InventMovement::construct(inventTransferLine,InventMovSubType::TransferOrderTo);

At the top of this method I added the initiation of my custom class.

customClass A = new customClass();

Below the initiation of InventMovement I add code to assign values to parm-methods on my custom class. I also add this code to set the instance of my custom class on InventMovement:

movementTo.parmCustomClass(A);

in InventMovement I have in the classDeclaration:

customClass AA;

The parm-method I created:

public customClass parmClass (customClass _class = AA)

{

AA = _class;

return AA;

}

I added a breakpoint to this method and the moment it hits AA = _class, it gives me the following error:

Error executing code: Wrong argument types in variable assignment.

I made sure that the custom class name is spelled correctly, so it is not a misspelling.

 Does it not like a class to be a parameter or what am I doing wrong here.

Any help is appreciated.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Brandon Wiese Profile Picture
    17,788 on at
    RE: How do I pass a class instance as a parameter to be used in another class

    You are on the right track.  You can definitely pass a class as a parameter to a method, instance or static.

    I think the problem you've encountered might be a result of not doing a Compile forward when modifying a base class but instancing a subclass (InventMov_TransferTransitFrom and InventMov_TransferTransitTo).  Sometimes when you change a base class and do not Compile forward, things act weird, and your error strikes me as weird because it should work.

    Can I suggest that instead of modifying the InventMovement base class at all, you consider modifying the InventMov_TransferTransitFrom and InventMov_TransfertTransitTo subclasses instead?  If the functionality you're trying to implement is specific to inventory transfer orders, then it may not be necessary to touch the InventMovement base class.  Can you not add to the voucher by overriding the updateLedgerAdjust(..) method in the subclass and perform your additional work after the super() call?

  • Retha Profile Picture
    1,455 on at
    RE: How do I pass a class instance as a parameter to be used in another class

    The issue was the compile forward that I didn't do.

    I also move my change in updateLedgerAdjust() to InventMov_TransferTransitFrom as you suggested.

    I unfortunately have to keep the parm_method in InventMovement because I get the error that InventMovement doesn't contain the method if I add it only to InventMov_TransferTransitFrom.

    Thanks for your help.

  • Brandon Wiese Profile Picture
    17,788 on at
    RE: How do I pass a class instance as a parameter to be used in another class

    If you either type the InventMovement variables as the proper subclass or create new variables that are properly typed as the subclasses, you will be able to put your .parm methods on the InventMov_TransferTransit classes also, and not get errors.  There is value in keeping changes to the system compact and closely related rather than scattered across many classes.  The smaller the change, the less trouble you'll have with it later during code merges, debugging, and troubleshooting.

  • Retha Profile Picture
    1,455 on at
    RE: How do I pass a class instance as a parameter to be used in another class

    Sorry Brandon but I have no idea what you mean by "If you either type the InventMovement variables as the proper subclass or create new variables that are properly typed as the subclasses". Can you give me a simple example?  I started with AX in January and had official training, but the latter always covers only the basics, so the rest is learning on the fly :)

  • Brandon Wiese Profile Picture
    17,788 on at
    RE: How do I pass a class instance as a parameter to be used in another class

    In the original UpdateInventTransferLine method both inventory movements are instanced and referenced by variables typed as InventMovement.  That's why you currently have to place your .parm() methods on the base class.  But you should be able to type those variances as their actual subclass types, InventMov_TransferTransitFrom and InventMov_TransferTransitTo.  Then those variables would be able to reference the subclass specific methods, i.e. .parm() methods only visible from the subclass.

    This might be too much complexity if you've just started developing with objects and class inheritance.  It's somewhat a matter of style and best practice.  If you can make it work modifying the base class (without accidentally breaking other subclasses, of course), then kudos to you and you should start with that solution.  The best way to really "get" class inheritance is to see other examples of it being used in different ways.  InventMovement is a poor place to start because it's just so massive.  Maybe InventUpdate, or SalesLineType are better places to learn by example.

    In any case, it sounds like you are back on track.  Good luck!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Community Member Profile Picture

Community Member 2

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans