By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,077 Members | 780 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,077 IT Pros & Developers. It's quick & easy.

C# App: Data Hiding question

P: 3
I have 3 classes: a Windows Form, a Game, and a Piece. The Game has a Piece, and the Windows Form has a Game. The Windows Form needs to be able to "get" the game's Piece, and view its data, but not have access to the functions of Piece that modify that data.
For Example:
..Windows Form
.........public location {get {return Location;} }
.........public Move()
.......public piece {get { return Piece; } }
The Form has to be able to look at the Piece and see its location (to draw it), but it should not be able to tell it to move, only the Game should be able to do this. The way I have it, If I use "get" to let the Windows Form see the piece, it can also both see and use the Move() function. If I were to pass back a copy of the Piece, and let the Form modify the copy, but not the original, then I'm duplicating large amounts of data. In short, Everything should be able to see Piece's data, but only the owning class should be able to modify its public data members or use its public functions. Is there an elegant way to do this?
Jul 9 '07 #1
Share this Question
Share on Google+
5 Replies

Expert 5K+
P: 7,872
I would suggest having a function in Game such that it takes an index number of Piece (based on the collection of Pieces stored in Game) and returns the Location of that piece.
That way, only Game actually has acces to Piece's functions, but the Windows Form can still see the locations by using a Public function of Game.

That way Windows Form need only have a way to identify which Piece it wants in order to perform all allowed manipulations of Piece.
Jul 9 '07 #2

P: 3
Thanks for your help.
I considered this, but, since each piece has a lot more data than just the location, it would mean writing a new function in Game for every property I add to piece. Alternatively, if I send all the data in one function, I'm creating duplicates of all of the data, since C# doesn't have support for constant reference. Also, doing so for only one piece at a time, by index of the piece, requires the Form to know about the internal structure of the Game, so I'd rather send the whole list of players. Similarly, I could write a class to interface with Piece (by casting into the interface class), that prevents access to certain data and functions, but then, again, if I decide to change anything in Piece, I have to also change the interface class (and this also amounts to duplicates of large amounts of data).
I was hoping that perhaps there was some way to use the access modifiers to accomplish this, since it amounts to significantly less code, and less duplication of data. My issue is really that while "get" (like similar solutions) allows only read access to the object itself, it still allows access to anything public in the object for modification.
Jul 9 '07 #3

Expert 100+
P: 638
Create a Library just for the game / piece. Have the form reference the library. Within the library, every item that you want only visible to the game / piece, declare internal. Everything you want the form to have access to, declare public.


Example: Have two projects within your solution. The main project is your windows form project. The child project is your Class Library for the game / piece. Reference that library from the main project. Now use the above instructions.
Jul 9 '07 #4

Expert 5K+
P: 7,872
Well what kind of access modifier were you hoping for?
One that "allows access to anything who isn't the owner?" vs Public vs Private?

That being said, the "Protected" keyword might be useful?
Jul 9 '07 #5

P: 3
Well what kind of access modifier were you hoping for?
One that "allows access to anything who isn't the owner?" vs Public vs Private?
I need to control read/write access at different levels, not visibility. "get and "set" do essentially this. The problem is that "get" (or sending back references to private data) essentially breaks the "private", because it gives write access to any public members of the object that you "get" (even if there is no "set" for the object). I need to make the Piece's public members read-only to anything outside of the Game, but read/write to the Game. The private members aren't even visible outside of the class they're declared in, and, as I said, once you give write access to someone outside the class, you give write access to everyone who can see the class.
Protected is for for allowing access to inherited classes. I need to provide less access (but not no access) to things outside the class.

I think TRScheel's suggestion will work for my purposes, though.
Thank you both for your help.
Jul 9 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.