Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-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 March 6th, 2010, 11:02 AM
Banned
 
Join Date: Jan 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default Chapter 8 Public Property

Imar,

I've been programming legacy systems for years and at times it is difficult to grasp all the oop concepts. I don't quite understand how to explain the VB.NET property code at the bottom of page 279. How can you define a Private variable called _displayDirection of type Direction and also have a Property called DisplayDirection as type Direction? I understand that Direction is a class or type that you create and then you define the variable as that type, but I don't get how it can be a property too.

Thanks,

Chuck
 
Old March 6th, 2010, 11:19 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Chuck,

A Property is of a certain type. Consider this property to hold someone's first name:

Code:
 
Private _firstName As String = "Chuck"
 
Public Property FirstName() As String
  Get
    Return _firstName
  End Get
  Set(ByVal Value As String)
    _firstName = Value
  End Set
End Property
Here, the name of the property is FirstName. It's type is a String (or System.String to be exact). As the property's *backing store* the private variable _firstName is used. The underscore gives this variable a different name than the public property DisplayDirection. The type of that private variable is also a string. As a default value, it gets assigned the text "Chuck" (this is not required though).

When the property is asked for its value (when its Getter is accessed) it returns the value stored in _firstName. When the property gets assigned a value (its Setter is accessed) the value is stored in _firstName. In other words, properties don't contain their own state, but use a backing store (such as a private variable, but in later examples you see how to use ViewState for this purpose) to store their data.

The same principle is used for the DisplayDirection property:
Code:
 
Private _displayDirection As Direction = Direction.Vertical
 
Public Property DisplayDirection() As Direction Get Return _displayDirection End Get
Set(ByVal value As Direction)
_displayDirection = value End Set
End Property
Here, the property is called DisplayDirection. Its type is Direction which is the Enum you defined earlier. As the backing store, the private variable _displayDirection is used. This variable gets the Enum value of Direction.Vertical assigned as its default value.

Does this clarify things? If not, feel free to post a follow message asking for clarification.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 6th, 2010, 11:46 AM
Banned
 
Join Date: Jan 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default Thanks

Yes, this makes sense now.
It's just odd, from the point of view of some one who has written in 3rd generation code, to go through all this just to set a variable to "Horizontal or "Vertical". I guess this has to be done to conform to all the oop concepts.

Thanks again,

Chuck
 
Old March 6th, 2010, 12:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You don't *have* to use a propeprty. This would work as well:

Code:
 
Public DisplayDirection As Direction = Direction.Vertical
However, with a public field you lose control over the values that are set. A property is about encapsulation and hiding the implementation. Your external code just accesses the property, and how it behaves internally is up to that property alone. This enables you to, for example, switch from using a private variable as the backing store to ViewState without modifying any external / calling code.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
ChuckASP (March 6th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
exposing server control from masterpage as public property robbaralla BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 February 16th, 2010 12:55 PM
Chapter 4: Page 120 - Property value not valid. VeganMan BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 April 3rd, 2008 12:26 AM
public article - public articledetails _keysersoze_ BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 8th, 2007 08:38 AM
Saving a Public Property created in App_Code striker9 ASP.NET 2.0 Basics 1 June 8th, 2006 02:18 PM
Chapter 13, can not visit the public property Artistry BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 August 25th, 2005 02:16 AM





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