473,395 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# App: Data Hiding question

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
.....Game
.......Piece
.........Location
.........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
5 2228
Plater
7,872 Expert 4TB
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
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
TRScheel
638 Expert 512MB
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.

EDIT:

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
Plater
7,872 Expert 4TB
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
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

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

Similar topics

11
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give...
2
by: coolwarrior | last post by:
Hi, 1_I want to know the difference between "data hiding" , "steganography" ,"watermarking" ,"capsulation" related to DSP. 2_There r plenty of informaion about data hiding for images on the web...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
1
by: Shahzad | last post by:
Dear Respected Gurus/Sirs, I would like to share your ideas and get reviews will be highly appreciated in anticipation Question No.1 I had developed a seprate audit/security database that...
5
by: Mart | last post by:
Hi everybody, I plan to have a LOT of SQL string in my app. So I need your advice for this. Is it a good idea to store all my SQL string in the app.config file? For a maintenance point of...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
2
Frinavale
by: Frinavale | last post by:
This question may seem a bit newbie-ish but I'm new to desktop applications...please bear with me. I have a function that populates a ComboBox with a bunch of names. To populate it I have to make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.