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)

Calling protected constructor from class extension

(0) ShareShare
ReportReport
Posted on by

Hi,
I created an extension class according to all rules, but I get a bunch of errors, saying that the parent class (HcmWorkerTransition) is not visible due to protection level (by default it is declared as just a class, so I suppose it is public). The same is said about the parent class methods, although they are declared as protected. What could possibly cause it? Thank you.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    236,019 Most Valuable Professional on at
    RE: Calling protected constructor from class extension

    In that case, what you need can't be done by class extensions.

    Not only you can use normal object-oriented concepts such as inheritance - it's what you should be using in the first place. And yes, using object-oriented programming is definitely a good practice.

    You're not the only one who got distracted by this new feature and forgot about normal object-oriented design.

  • Community Member Profile Picture
    on at
    RE: Calling protected constructor from class extension

    The thing is that when I customize the class, the method I add is a copy of a method but a bit edited to receive one more variable. So the instance has to be created in this method (which is impossible if it doesn't inherit) and other methods from the HcmWorkerTransition class must be used, which is also impossible I suppose. What if I just created a class that inherits from HcmWorkerTransition and use all those methods? would it be something like extension, a good practice?

  • Martin Dráb Profile Picture
    236,019 Most Valuable Professional on at
    RE: Calling protected constructor from class extension

    You're still missing the point that we're talking about class extensions, not extension methods. Please look at the last example (Common_Extension) in my blog post about class extensions; I explicitly use "this" there. I also explain there how class extensions work.

    Note that I wasn't referring explicitly to ascNewCreateHcmWorker() - I wrote my answer before Dsatkus shared this method. If I'm right about what he's trying to achieve, the fact that the method is static is just another bug.

  • Basheer17 Profile Picture
    2,720 on at
    RE: Calling protected constructor from class extension

    If your new method is static you have to pass the first parameter as base class object. Since "this" keyword can't be used in static methods.

  • Suggested answer
    Martin Dráb Profile Picture
    236,019 Most Valuable Professional on at
    RE: Class extension

    That's the correct behavior - you can't access protected constructors from classes outside the inheritance hierarchy. Extension classes are completely separate classes and therefore they can't access protected members.

    Why would you create a new instance anyway? Don't you rather want to refer to the instance you're extending? If so, simply use the variable called this.

  • Community Member Profile Picture
    on at
    RE: Class extension

    not to get you confused but this is the real piece of code:

    [ExtensionOf(classStr(HcmWorkerTransition))]

    final class ASCHcmWorkerTransition_Extension

    {

       ASCWorkerCategoryEnum        hcmWorkerCategory;

       static HcmWorkerRecId ascNewCreateHcmWorker(

           ASCWorkerCategoryEnum       _hcmWorkerCategory,

           DirPersonName               _dirPersonName,

           HcmPersonnelNumberId        _personnelNumber,

           CompanyInfoRecId            _legalEntityRecId = CompanyInfo::find().RecId,

           HcmEmploymentType           _hcmEmploymentType = HcmEmploymentType::Employee,

           validFromDateTime           _validFrom = DateTimeUtil::minValue(),

           validToDateTime             _validTo = DateTimeUtil::maxValue()

           , EmployeeId_RU               _employeeId = ''      

       )

       {

           HcmWorkerTransition hcmWorkerTransition = new HcmWorkerTransition();

           hcmWorkerTransition.parmLegalEntityRecId(_legalEntityRecId);

           hcmWorkerTransition.parmHcmEmploymentType(_hcmEmploymentType);

           HcmWorkerTransition.ascParmHcmWorkerCategory(_hcmWorkerCategory);

           hcmWorkerTransition.parmValidFrom(_validFrom);

           hcmWorkerTransition.parmValidTo(_validTo);

           }

  • Martin Dráb Profile Picture
    236,019 Most Valuable Professional on at
    RE: Class extension

    Basheeruddin, you seem to be talking about extension methods, not class extensions. Class extensions requires an additional attribute (ExtensionOf), but then you don't have to hande the object reference by yourself as the first parameter (and it has several extra features).

  • Community Member Profile Picture
    on at
    RE: Class extension

    sure :

    [ExtensionOf(classStr(HcmWorkerTransition))]

    final class ASCHcmWorkerTransition_Extension

    {

       HcmWorkerTransition hcmWorkerTransition = new HcmWorkerTransition(); // I cant access the class constructor

    }

  • Martin Dráb Profile Picture
    236,019 Most Valuable Professional on at
    RE: Class extension

    May I ask you again to show us your actual code? We can't just guess what you've written.

  • Suggested answer
    Basheer17 Profile Picture
    2,720 on at
    RE: Class extension

    You can write your new method in the _extension class. Keep first parameter as your base class, and whenever your new method is called pass the base class object. In this way you can access other methods of your base class in your extension method using object reference.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

#1
Community Member Profile Picture

Community Member 2

#1
Mea_ Profile Picture

Mea_ 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans