Jump to content
  • Advertisement
Sign in to follow this  
CodeReaver

[.net] Any alternatives to using friend classes?

This topic is 3445 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

I'm trying to come up with a file management system for use with a model editor I'm working on. I want the file manager class to create instances of texture and effect classes which would contain some additional data such as where the files were loaded from. However I also want the file manager class to be the only thing that controls the data inside those classes. In C++ I'd make the file manager class a friend of the other classes, but in C#, I can't do that so I'm looking for some programming techniques I could use instead. I know can use different assemblies with the extern keyword, but I think that requires dlls or multiple exes or something and I'd prefere to stick with just the one exe. I've also seen some people use nested classes to get around this type of problem. However, nested classes start to look a bit messy in the examples I've seen which have mostly been short snippets on forums and stuff. I guess I'm not leaving myself many options here, but I heard that the need to use friend classes is a sign of poor code design anyway. Does anyone have any tips or examples of how similar probems have been dealt with?

Share this post


Link to post
Share on other sites
Advertisement
This is what the internal access modifier is for.

It makes a certain variable accessible to all code inside that same assembly, but not to external code (like the public modifier would do).

It's not exactly the same as C++'s friend, but considering your case, I think it's a good choice.

Share this post


Link to post
Share on other sites
An assembly's an exe or a dll isn't it? I'd prefere not to have to split the final app into seperate files as it's a fairly small program at the moment and it's easier for people to keep track of that way. Hovever I'm always find myself restarting my projects due to my being too stubborn to do stuff like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodeReaver
I want the file manager class to create instances of texture and effect classes
But it's a file manager, file management is totally different to texture and effect creation.

Speaking of managers, Fayol considers the five functions of management to be:
1) Planning
2) Organising
3) Leading
4) Coordinating
5) Controlling

Combine all those with texture and effect creation and I fear we have a serious neglect of the Single Responsibility Principle here.

Decide which concerns are being dealt with and split them up into separate units.

Quote:
I also want the file manager class to be the only thing that controls the data inside those classes.
If you cannot trust an object to control the data you give it then it should not be given that data in the first place. [smile]

Share this post


Link to post
Share on other sites
I never understood why they made internal work on assembly's instead of namespaces like the java equivalent does. The only solutions I have come up with all involve some variant of having two classes, one provides a nice interface and one contain all the core data and methods that don't need to be accessed from outside the namespace.

Share this post


Link to post
Share on other sites
Just use internal anyway then.

Technically other classes would be able to access those members, but just adding a comment that they shouldn't do that should be fine. It's not like it's completely external code, so you can assume that people that write for that module know at least a bit about the intended structure.

Sometimes being a little pragmatic isn't a bad thing:)

Share this post


Link to post
Share on other sites
Internal access is a good catch-all when you have this type of situation, and that's the one line answer.

However, I agree with dmatter that your design would benefit from a rethink. The file manager (or resource manager, which is what I think you are actually describing) manages the collection of resources and the process of loading new files and extracting the resource(s) from it, but the data inside instances of different types of resource should be controlled by subclasses of Resource (e.g. Texture, SoundEffect, VideoClip etc).

Share this post


Link to post
Share on other sites
Yeah, resource manager sounds more like what I meant. The idea is that I request a resource from the resource manager, the manager searches it's list and then if it finds the resource, it returns that. If the resource isn't in the list it loads the resource from a file, adds it to the list and then returns that. However since each resource can be referenced multiple times, I want the classes referencing it to be able to set the texture/effect as the current one, but not alter it's data.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!