[.net] Private & Access Classes

Started by
4 comments, last by gwihlidal 18 years, 1 month ago
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?
Advertisement
use protected

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

-use protected

But how then, to give other class access?
The other class will have access since it's derived from Spaceship.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

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

~Graham

This topic is closed to new replies.

Advertisement