Sign in to follow this  

[.net] Private & Access Classes

This topic is 4323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
You can make assemblies "friends" of each other and private otherwise in .NET 2.0, but nothing really at the class level.

~Graham

Share this post


Link to post
Share on other sites
Sign in to follow this