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

Announcements

No record found.

News and Announcements icon
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,833 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,833 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

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... 583 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 378

#3
Diego Mancassola Profile Picture

Diego Mancassola 255

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans