I have a class Server
which talks to a server connection for IRC. It holds a list of known User
s, and creates them as necessary.
I have two problems involving the User
class:
User
. I only want the Server
class to be able to do this.User
describes) changes his/her name (or other info, like joined channels), the Server
class can change it itself. However, other classes can, too! I want to disallow other classes from touching this information (making it read-only to them).How can I solve these two problems? In C++, it can be solved by using the friend
keyword and making the ctor and setName
(and such) private.
Is there a C# keyword which can allow a certain method be accessable by a specified class? This would solve my problem.
Honestly I find the friend
access that originated from C++ to be symptomatic of bad design. You're better off fixing your design.
For a start, who really cares if someone creates a User? Does it really matter? I ask this because it seems that sometimes we programmers get caught up worrying about things that simply won't happen or, if they do, it doesn't matter.
If you really do care then do one of the following:
Visibility hacks (of which friends in C++ are one and package access in Java are both good examples) are simply asking for trouble and not a good idea.
The closest in the .NET world to friend
is the internal
visibility.
Note that if your two classes are in separate assemblies, you can use the InternalsVisibleTo attribute to allow one assembly visibility of the internals of the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With