Link to home
Start Free TrialLog in
Avatar of AndyH79
AndyH79Flag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
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
Avatar of AndyH79

ASKER

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.