Link to home
Start Free TrialLog in
Avatar of jdana
jdanaFlag for United States of America

asked on

VB.NET - Trying to get a handle on "Protected"

I have a class named DataAccess (Image 1).  DataAccess contains the protected function GetDataSetBasedOnSprocAndParams (Image 2).

I have another class named PagePrepper.  PagePrepper inherits DataAccess (Image 3).  

When I call GetDataSetBasedOnSprocAndParams from PagePrepper I get the following error: GetDataSetBasedOnSprocAndParams is not accessible in this context because it is ‘protected’ (Image 4).  Clearly I’m missing the boat here.  If a child class (PagePrepper) inherits a parent class (DataAccess), aren’t protected properties and methods in the parent supposed to be available to the child?

Img1---Data-Access-Class.jpg
Img2---Protected-Function.jpg
Img3---Page-Prepper-Class.jpg
Img4---Error-Message.jpg
Avatar of kaufmed
kaufmed
Flag of United States of America image

The short answer to your question is, "yes, protected members should be accessible in a child class."

The long answer depends on your implementation. Is it possible to post more of the class definitions?
ASKER CERTIFIED SOLUTION
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good catch, kaufmed. You're right.

Still, the reason the OP's code failed is because it's not being called from within the child.
Agreed  :)
Hang on...  take a look at the example more closely.  You are NOT calling GetDataSetBasedOnSprocAndParams from PagePrepper... you're calling it from some other class called DataAccess.
Depending on your scenario, it might make sense to mark the DataAccess class with the MustInherit keyword (as a reminder to use the PagePrepper class instead)
Yeah, we pointed that out. He's calling an object of the base class type.
Avatar of jdana

ASKER

Thanks!  I get it.  Protected objects in the base class are available directly to child classes.