Custom Class Visibility

Posted on 2013-09-12
Medium Priority
Last Modified: 2013-09-12
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

Question by:AndyH79
LVL 75

Accepted Solution

käµfm³d   👽 earned 1500 total points
ID: 39488692
This is very quick and off the top of my head, but try protected internal.


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

Author Comment

ID: 39488801
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question