1

I have 2 classes:

public class Access
{
    public class Job
    {
        public int Id { get; set; }
        protected string JobName { get; set; }
    }
}

Class2.cs

public class Full: Access.Job
{

}

Full ful = new Full();

Why I'm not able to access the ful.JobName member?

emerson.marini
  • 9,331
  • 2
  • 29
  • 46
  • 7
    You can access JobName in your Full class but not through a a Full reference like ful – o_weisman Jan 24 '17 at 12:20
  • 1
    You can access this property only in the Full class, not outside of the class. When you add "protected string Job { get { return this.JobName; } }" in the Full class, you will be able to use it even outside of the Full class. – Julo Jan 24 '17 at 12:21
  • No i'm not Able to Acces JobName But Id I'm able to access –  Jan 24 '17 at 12:21
  • *"No i'm not Able to Acces JobName But Id I'm able to access"* **What?** Sorry, this doesn't make any sense. – Manfred Radlwimmer Jan 24 '17 at 12:24
  • Sorry I'm Able to accessing Id –  Jan 24 '17 at 12:30
  • 1
    Md DastageerJob: What they meant is that you can use JobName INSIDE the Full class (that is between the { } after the declaration of the class) but, since JobName is protected, you cant access it outside those brackets, even when you instanciate the Full class, or even the Job Class. – Ricardo Olivo Poletti Jan 24 '17 at 12:32

1 Answers1

4

Because You are trying to access protected method from outside the class. Only public methods are available. You can access the property/variably/method that is protected, only in the inherited class, but not from outer code:

public class Full: Access.Job
{
    public void mVoid()
    {
        Console.WriteLine(this.JobName);
    }

    protected void mProtVoid()
    {
        Console.WriteLine(this.JobName);
    }

    private void mPrivateVoid()
    {
        Console.WriteLine("Hey");
    }
}

Full myFull = new Full();
myFull.mVoid();  //will work
myFull.mProtVoid(); //Will not work
myFull.mPrivateVoid(); //Will not work

If You need to get to the protected property, there are 2 ways (3 actually, but Reflection is the dirty way and should be avoided):

1. Make it public

If it will be set to public, it will be stil inherit and You can directly access it:

Full nFull = new Full();
Console.Write(nFull.JobName);

2. Make a "wrapper"/"facade"

Create new property or method, that will just access the hidden property and return it in expected format.

public class Full: Access.Job
{
    public string WrappedJobName { get { return this.JobName; } }
    public string WrappedJobName => this.JobName; //C# 6.0 syntax
}

Full mFull = new Full();
Console.WriteLine(mFull.WrappedJobName);
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • its Fine But Protected is work only which inherit from that class. Here when i create Full myFull = new Full(); myFull.JobName --->Why its not working –  Jan 24 '17 at 12:29
  • 2
    @MdDastageerJob Tatranskymedved already answered why it does not work, read a little – Camilo Terevinto Jan 24 '17 at 12:30
  • Because You are creating new instance -> You are sitting next to the object and You are allowed to access it's public methods and properties. If You try to access protected/private methods, it won't allow You. Because they are protected/private. Definition of language. http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing – Tatranskymedved Jan 24 '17 at 12:31