1

I'm developing a system in C# for communication with a server (custom protocol over Sockets).

Without going into much detail about the context, I have two classes - ServerConnection and ServerMessage. The ServerConnection objects are capable of creating ServerMessage objects, which encapsulate information about the status of a reply.

However, ServerConnection is the class that handles all the data sending/receiving, and then sets the the relevant ServerMessage's properties asynchronously (all messages are held in an internal list until the relevant reply is received, whereupon it is edited and removed).

As ServerConnection needs to fiddle around with the internal workings of the ServerMessage (at the moment, I need it to be able to trigger a ManualResetEvent), it needs more access to that class than I want other classes to have.

I believe that Nested classes give access in a similar way, but I've so far failed to create the desired hierarchy.

*Clarification: I want ServerConnection to be the programmer's interface, and to provide information on operations through the use of ServerMessages.

Any ideas?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • C++ doesn't support the notion of modules at the language level so requires you to be explicit about whom can mess with your private parts. No such problem in C#, simply declare them as *internal*. – Hans Passant Nov 09 '14 at 17:29
  • possible duplicate of [Why does C# not provide the C++ style 'friend' keyword?](http://stackoverflow.com/questions/203616/why-does-c-sharp-not-provide-the-c-style-friend-keyword) – Peter Duniho Nov 09 '14 at 19:06

1 Answers1

0

Maybe what you are looking for is internal - accessible within the same assembly.

Z .
  • 12,657
  • 1
  • 31
  • 56
  • Sorry, my question is quite confusing - I need both `ServerConnection` and other classes to have access to `ServerMessage`, but for `ServerConnection` to have "deeper" access - to be able to access functions etc. that other classes cannot. – hnefatl Nov 09 '14 at 17:14
  • There are no true friend classes in c#. With internal fields or methods you'll be able to access then with your code (in the same assembly) but other code (different assembly) will not be able to. – Z . Nov 09 '14 at 17:16
  • But that requires splitting these two classes into a different assembly :/ – hnefatl Nov 09 '14 at 17:19
  • Realistically, I'm the only person who'll ever use these classes (personal project), but it doesn't feel right have publicly available methods that can break the object's functionality if called externally – hnefatl Nov 09 '14 at 17:20
  • No, you put ServerConnection ands ServerMessage in the same assembly and they'll be able to access each other's internal fields. Other code will not. – Z . Nov 09 '14 at 17:20
  • Sorry, I understood your answer, but given the choice between creating a library for only 2 classes and having objects expose code that can break the object, I'm not sure which to pick :( – hnefatl Nov 09 '14 at 17:24
  • I would expose public methods that do all the manipulations that keep the class consistent. Example if I always have to change two properties together, have a method with two params that sets those two properties. – Z . Nov 09 '14 at 17:29