Limiting Accessibility of Multi-Class Classes

  • Thread starter Thread starter Joe Keller
  • Start date Start date
J

Joe Keller

Hello,

I have a class that allows me to traverse the directory of a data store on
my PPC. The directory structure is setup as follows:

Root
Customer
Processed
System
Processed

I want the developer to be able to easily traverse the directory by using
calls like "Root.Customer.Processed.Value" to get the value/path of the
"Root\Customer\Processed" directory. However, I only want to give the
developer access to the "Root" class - I do not want to let them instantiate
the "Customer", "System", or "Processed" classes directly. How do I limit
the scope of the "Customer", "System", and "Processed" classes such that
they cannot be instantiated directly? I've tried using the various access
modifiers (protected, internal, etc.) to no avail.

The class definition is shown below:

public class Dir

{

public MyRoot Root;



public Dir()

{

this.Root = new MyRoot();

}



public class MyRoot

{

public MyRoot()

{

this.System = new MySystem();

this.Customer = new MyCustomer();

}



public string Value = "\";

public MySystem System;

public MyCustomer Customer;

}



public class MySystem

{

public MySystem()

{

this.Working = new MyWorking();

this.Working.Value = @"\System\Working";

}

public string Value = "\System";

public MyWorking Working;

}



public class MyCustomer

{

public MyCustomer()

{

this.Working = new MyWorking();

this.Working.Value = @"\Customer\Working";

}

public string Value = "\Customer";

public MyWorking Working;

}



public class MyWorking

{

public string Value = "Working";

}

}


Thanks!

Joe
 
if you don't provide a public constructor, then they can't create one. make
all of your ctors "internal" except the one for Root.

-Chris
 
Mark the constructors for your child classes as internal - then they can
only be called from within your assembly. This will mean you can create a
Customer object from your Root class, but a developer cannot simply call:-

Customer mycust = new Customer();

e.g.

public class MyCustomer
{
internal MyCustomer()
{
//your code here
}

//properties methods etc
}

Peter

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org
 
Back
Top