Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2009, 09:11 PM
Registered User
 
Join Date: Nov 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Defining a class member with the public access specifier

Under Chapter 10 > "How the Property Methods Work", the final paragraph in the subsection goes:

"Notice that, if you had defined the class member named current with the public access specifier, you could not do this kind of checking on current before it was used."

I don't remember any previous explicit explanation on this, and can't get my head 'round it. Could someone elaborate, please? I'd be most grateful, and then there'd be nothing so far that I haven't completely understood from the book.

"this kind of checking" refers to returning "current.Month" for a getter.
 
Old December 17th, 2009, 10:51 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Access specifier

It would help a lot if you could give me the page number and then the paragraph where the quote is found...I had a late night last night...
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old December 17th, 2009, 11:22 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Access specifier

Found it! (Page 240, second to last paragraph.)

I think the confusion arises because I refer back to a data definition in Chapter 9. If you look on page 225, you will see the statement:

private DateTime current;

which defined current as a private property within the clsDates object. In Chapter 10, you learn that setter and getter property methods are the proper way to change the value of a property within a class. This forces the programmer to use the code you write to change the state of any clsDates object the programmer instantiates.

If we don't use the private access specifier on current, you have no control over what gets assigned into current. By using the private access specifier on current, any time you change any aspect of the date (e.g., month, day, or year), you have the opportunity of checking what the user is trying to assign into the property. On page 240, for example, if the programmer is trying to assign a value into month, your code has the opportunity to insure that its value is reasonable (i.e., 1 to 12). If you make current a public property, you have no control over what gets assigned into current. The public access specifier turns your pristine piece of data into a prostitute that anyone can have their way with...not good. There are few reasons (none?) for making a class property method public, since you, by definition, lose some ability to check what gets assigned into that property. While there may be a few reasons for public properties, there aren't too many good ones. As a rule, make your property methods private and use getters and setters to gain better control over those properties.

I hope this helps...
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old December 21st, 2009, 12:19 PM
Registered User
 
Join Date: Nov 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

It helped wonders, thanks Dr. Purdum! I'm not even sure anymore why I could not get what you meant the first time through. It makes perfect sense now, I don't think I could even blame it on the wording of the original text, maybe I just needed some time and your help to sink the knowledge in.

Sorry I couldn't provide the page number, it's because I bought the Kindle version (working great on Kindle for PC) so it wouldn't be consistent.

Thanks again!
 
Old December 21st, 2009, 12:40 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Kindle version

Glad things are clearer now. Also, I'm happy to hear that you're doing okay with the Kindle version. About the only bad review my book's received was because Kindle was giving him problems...hopefully that's history now.

Keep plugging away and enjoy the ride!
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining controls from a different class VB josh2009 Beginning VB 6 1 September 15th, 2009 09:54 PM
disadvantages of protected access specifier kishore.dyn Java Basics 2 August 10th, 2006 03:31 PM
Arraylist::Sort() by class member jilly Visual C++ 2005 0 March 22nd, 2006 06:49 PM
How to Assign a Desc. to a Class Member BrianWren VB.NET 2002/2003 Basics 3 June 1st, 2005 02:07 PM
How to make a class public johnjonsson VS.NET 2002/2003 0 October 6th, 2003 02:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.