Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I access values in another class inherited from public class C#?

I get an error when inheriting from public class I can’t access values of another class.

AppEngine.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestEngine
{
    public class AppEngine : Appstrings
    {
        public async Task<string> Testing()
        {
            return wrongUserName;
        }
    }
}

Appstrings.cs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestEngine
{
    public class Appstrings
    {
        string wrongUserName = "Wrong username. Please check and try again..";
    }
}

I want to access the values that I defined in the Appstrings class by inheriting them from the AppEngine class. This is the code I wrote, but the error I get is 'Appstrings.wrongUserName' is inaccessible due to its protection level. It is said that it cannot be inherited due to protection, but the class construction is public. Where do you think I am doing wrong?

>Solution :

Base on C# language reference

Class members can have any of the five kinds of declared accessibility and default to private declared accessibility.

So, the wrongUserName has the private accessibility inside the Appstrings class. You must specify the accessibility explicitly of the member to internal, protected, or public (not recommended).

namespace TestEngine
{
    public class Appstrings
    {
        protected string wrongUserName = "Wrong username. Please check and try again..";
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading