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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Object varaible in x++

(0) ShareShare
ReportReport
Posted on by 1,552

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?

I have the same question (0)
  • Suggested answer
    GirishS Profile Picture
    27,829 Moderator on at

    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.

  • junior AX Profile Picture
    1,552 on at

    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?

  • GirishS Profile Picture
    27,829 Moderator on at

    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.

  • Suggested answer
    Mohit Rampal Profile Picture
    12,565 Moderator on at

    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;
        }
    
    }

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…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 679 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 332 Super User 2026 Season 1

#3
Martin Dráb Profile Picture

Martin Dráb 238 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans