Sign in to follow this  
XeeRox

[.net] Private & Access Classes

Recommended Posts

Hi guys, i am now readed about "private" in my book, but there isnt showed off one thing.How can give the access to the one class, so it could modify private stuff. :.Example.: ============================================================================ class Spaceship <- CLASS A { private int fuel; <-- private class }; ============================================================================ class Warship : Spaceship <- CLASS B { public void ChangeFuel() { fuel = 10; <-- ERROR, because it dont have access } } ============================================================================ Now, how can i give access to the class B, so he coudl modify the fuel in class A?

Share this post


Link to post
Share on other sites
Members which are declared protected can be accessed from classes which derive from the original class.

Parent -> Child1 -> Child2

Child1 and Child2 derive from Parent; they have access to the protected members of Parent. Child2 derives from Child1; it has access to the protected members of Child1.

If you're looking for the "friend" access, wherein you can specify that a particular class, unrelated to another, can access and change the protected variables, it doesn't exist in C#. The closest there is to friend access is "internal" which allows access to any members within the same assembly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this