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 :
Finance | Project Operations, Human Resources, ...
Answered

accessing protected method in extension class

(4) ShareShare
ReportReport
Posted on by 913
Hello,
 
I wrote an extension for the standard class A, method A. Then there is class C that inherits from class B, which inherits from class A. Class C is instantiated and at some point in time C.methodA() is called. Now according to debugger i'm in ClassA.methodA. Can I call somehow protected method ClassC.methodD() from classA.methodA() extension? VS tells me there is no methodD on classA. However, in debugger when i'm in classA.methodA() "this" variable is class C.
 
Thanks.
I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    accessing protected method in extension class
    You can't do it without casting. If the type of variable is ClassA, the static type control doesn't allow you to call method that the class doesn't have. If you want to use methods of ClassC, you need to cast the object to ClassC.
     
    Why do you want to avoid casting and use some other trick? Casting is exactly what you need, isn't it?
     
    Alternatively you can add methodD() to ClassA and override it in ClassC. Then ClassA (and all its children) has methodD() and you call it on variables of type ClassA. If the ClassA variable happen to contain ClassC object, the ClassC implementation of methodD() will be called. That's polymorphism.
  • dark_knight Profile Picture
    913 on at
    accessing protected method in extension class
    Sohaib, hi
     
     
    protected methods can be wrapped so I don't see any problems here
     
    also
     
    When you wrap a method, you can also access public and protected methods, and variables of the base class. 
  • dark_knight Profile Picture
    913 on at
    accessing protected method in extension class
    Hi Martin,
     
    Yes. I know about access modifier and casting. but is it possible to do without casting somehow? some trick or anything like that. because the idea was to use existing method on class C to check the context table
  • Sohaib Cheema Profile Picture
    47,936 User Group Leader on at
    accessing protected method in extension class
    + Know the basics about access providers
    Public
    Private 
    Internal
    Protected
    Protected Internal 
    these are basics of Object Oriented Programming 
     
  • Suggested answer
    Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    accessing protected method in extension class
    The key concepts you need to learn about object-oriented programming are casting and polymorphism.
     
    It seems that you have a variable of type ClassA, which - at runtime - contains an instance of ClassC. But the compiler doesn't know that (if) there will be ClassC instance; it works with the declared type, which is ClassA. And it doesn't have any methodD(), therefore the compiler correctly rejects such a call.
     
    If you want to use members of ClassC, you need to cast the variable to ClassC. For example:
    ClassC c = objA;
    c.methodD();
  • André Arnaud de Calavon Profile Picture
    297,785 Super User 2025 Season 2 on at
    accessing protected method in extension class
    Hi Dark knight,

    It is hard to follow your question. Are you able to mention the standard classes and methods and provide the functional business requirement?
    Initially, I think you need to extend class C, but not sure.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 844 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 752 Super User 2025 Season 2

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 553

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans