Custom Class Visibility

I have a couple different classes in a base class library that application objects will inherit or implement from.

DLL Code
namespace BaseClass
{
    public class Facility
    {
        
    }
}
namespace BaseClass.IDR
{
    public class Meter
    {
       
    }

    public class TouchRecord
    {
        public Nullable<DateTime> idrHarvestFirst { get; set; }
	public Nullable<DateTime> idrHarvestLast { get; set; }
	public Nullable<DateTime> idrConversionLast { get; set; }
	public Nullable<DateTime> idrImportLast { get; set; }
	public Nullable<DateTime> idrValidationLast { get; set; }
    }
}

Open in new window


I'm wondering if there is a way to limit the TouchRecord class so that it can only be implemented within a class that inherits from BaseClass.IDR.Meter. The calls from App.Facility methods should still be able to access the fields of the TouchRecord class within declared App.Meter object, they just should not be able to declare TouchRecord properties within the facility class. Is it possible to constrain classes in this way?

Application Code

namespace App
{
    public class Facility : BaseClass.Facility
        {
            public App.Meter meter;
            public BassClass.IDR.TouchRecord touchRecord;  //This should cause an error
            
            public void newMeter(DateTime dateTime)
            {
            meter = new App.Meter();
            meter.touchRecord = new BaseClass.IDR.TouchRecord(); //This should be ok
            meter.touchRecord.idrHarvestFirst = dateTime; //This should be ok
            BaseClass.IDR.TouchRecord facilityTouchRecord = new BaseClass.IDR.TouchRecord //This should cause an error
            }
        }
    public class Meter : BaseClass.IDR.Meter
        {
            BassClass.IDR.TouchRecord touchRecord; //This is ok
        }

}

Open in new window

AndyH79Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
This is very quick and off the top of my head, but try protected internal.

http://msdn.microsoft.com/en-us/library/ms173121.aspx

If that doesn't work for you, I'll check back and read a bit more thoroughly  = )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyH79Author Commented:
When listing the class as protected internal I get an error "Elements defined in a namespace cannot be explicitly declared as private, protected or protected internal." It seems like my only option is to implicitly declare a class as internal or explicitly declare a class as public.

While protected internal doesn't work, the article you linked to has a note that says this:

"The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected."

When I list the class as internal and the members as protected, it does limit things as I would like, so I'm going to accept your response as a solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.