[.net] Private & Access Classes
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?
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.
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.
You can make assemblies "friends" of each other and private otherwise in .NET 2.0, but nothing really at the class level.
~Graham
~Graham
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement