6 Replies - 7634 Views - Last Post: 08 August 2007 - 05:14 PM Rate Topic: -----

#1 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

data hiding

Posted 26 July 2007 - 03:15 PM

i just started learning c# and finding it quite difficult to deal with data hiding.even though i understand what public ,protected etc means when i'm coding i'm confused as to what to use, i feel when i use public i make alteration very easy and with protected very restrictive. i just understand their definitions please help with understanding data hiding on the practical side.
Is This A Good Question/Topic? 1
  • +

Replies To: data hiding

#2 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: data hiding

Posted 27 July 2007 - 02:48 PM

waiting......
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: data hiding

Posted 28 July 2007 - 01:07 PM

Scope is one of the most tricky things for most programmers. How specific do you get? How much do you hide? One rule I have always followed that seems to work really well is make everything private and "promote" it if you definitely need to access it "directly".

How does this work? If I define a variable in a class' method, I make it private. If I find I need that variable to hold a value beyond a method, I "promote" it to a class variable. Do I need to access the variable directly? I then "promote it again to public.

When passing variables I ask the question "Is it necessary or can I figure out the information in the function itself?"

If you have one big problem with this whole scoping process, I recommend one book called "Code Complete" by Steve McConnell (Microsoft Press). The book is aged a bit, but the idea of scoping and variable lifetime chapters it includes is still good even today. He provides examples in Java, C++, VB, and even PHP/Python etc. It will tell you about variable scope, lifespan, localization etc. Great book.

Below is a link to the book. Enjoy!

Code Complete by Steve McConnell (Amazon.com)
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: data hiding

Posted 28 July 2007 - 01:10 PM

Great info Martyr2! Thanks for sharing!
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: data hiding

Posted 28 July 2007 - 01:15 PM

You are welcome. :)
Was This Post Helpful? 0
  • +
  • -

#6 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: data hiding

Posted 08 August 2007 - 02:58 PM

thanks for th info martyr.
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: data hiding

Posted 08 August 2007 - 05:14 PM

Martyr2 thats some excellent advice! Thats almost how I do it, with the exception of a couple changes.

/me bookmarks that post
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1