c# - Protected method access from derived class -


i have protected method in base class :

public class baseclass {   protected virtual void foo(){} }     

the method overrided 1 of derived classes:

public class derived1 : baseclass {    protected override void foo()     {      //some code...     } }     

another derived class has instance of first derived class.
when try access foo method (existing in base class, mentiond) error :

 public class derivedclass2 : baseclass     {      baseclass instance = new derivedclass1();      instance.foo(); // here error     } 

the error get:

error cs1540: cannot access protected member 'baseclass.foo' via qualifier of type 'baseclass';    qualifier must of type 'derivedclass2' (or derived it) 

i understand protected members should not give value other instance, instance derived same type,
there way not modify method public?

you can make foo method declaration protected internal....

public class baseclass {   protected internal virtual void foo(){} }   public class derived1 : baseclass {    protected internal override void foo()     {      //some code...     } } 

here "protected internal" means member visible class inheriting base class, whether it's in same assembly or not. member visible via object declared of type anywhere in same assembly.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -