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

Notifications

Announcements

No record found.

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)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    It's not easy to comment on your code if you don't show it. You said you've build an extension class, but then you switch top a problem about inheritance, which isn't related to extension classes.

    Or, if you're trying to inherit your extension class from HcmWorkerTransition, you're doing something wrong. Use either inheritance or extensions.

    Please tell us what you did, why and what exact errors you've got there.

  • Basheer17 Profile Picture
    2,720 on at

    If no access modifier is mentioned, by default its protected. If you want to access protected members you'll have to go for extension(Using inheritance). To avoid overlayering combination of Derived class(Inheritance) and _Extension class will be sufficient in most cases.

  • Community Member Profile Picture
    on at

    I want to create an extension class and as I understand that class inherits from HcmWorkerTransition. I need to create an additional method in that extension class, that uses some method from HcmWorkerTransition.

  • Suggested answer
    Basheer17 Profile Picture
    2,720 on at

    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.

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

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

  • Community Member Profile Picture
    on at

    sure :

    [ExtensionOf(classStr(HcmWorkerTransition))]

    final class ASCHcmWorkerTransition_Extension

    {

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

    }

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    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

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

           }

  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    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.

  • Basheer17 Profile Picture
    2,720 on at

    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.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans