Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Object varaible in x++

(0) ShareShare
ReportReport
Posted on by 1,550

Hi,

Is it good to use object varaible?

For example:

Object classTest;

If(_variable) // assume it's defined

{

  classTest = new ClassChild();

}

else

{

classTest = new ClassParent();

}

Where ClassChild extends ClassParent

Can i use it or shall i use anyType instead or is there another better way to achieve what i want?

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Object varaible in x++

    Another way is to use Switch statement in construct method of Parent class, you can find many example in standard. This is what we have used in our customizations.

    Public static ParentClass construct(VariableType _variable)
    {
        ParentClass class;
    
        switch(_variable)
        {
            case 'A':
            class = new childClass1(_variable);
            break;
            
            case 'B':
            class = new childClass2(_variable);
            break;
        }
    
    }

  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Object varaible in x++

    It's just an additional step. Nothing difference in that. Code I have written, and your code will work in the same way. I just provide another way of creating object for the class.

    Thanks,

    Girish S.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: Object varaible in x++

    Hi Girish,

    My code is already working but i wanted to ask if there is another better way.

    May i know why u think using DictClass and makeObject is better than my way?

    And what is DictClass and what it does?

  • Suggested answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Object varaible in x++

    Hi juniorAx,

    You can use object class but i think it needs to do something like below.

    Object classTest;
    DictClass className;
    If(_variable) // assume it's defined
    {
        className = new DictClass(className2Id('ChildClass'));
        classTest = className.makeObject();
        
    }
    
    else
    {
        className = new DictClass(className2Id('ClassParent'));
        classTest = className.makeObject();
    }

    Thanks,

    Girish S.

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…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans