03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Orientation Basics x 191<br />

Properties<br />

Properties of an object are the characteristics the object has. Consider a Person object. What k<strong>in</strong>d<br />

of properties does a Person have? It’s easy to come up with many different characteristics, but the<br />

most common are:<br />

‰ First name<br />

‰ Last name<br />

‰ Date of birth<br />

You def<strong>in</strong>e a property <strong>in</strong> a class with the Property keyword (<strong>in</strong> <strong>VB</strong>.<strong>NET</strong>) or with a property header<br />

similar to a method <strong>in</strong> C#. In both languages, you use a Get block (get <strong>in</strong> C#) <strong>and</strong> a Set block<br />

(set <strong>in</strong> C#) to def<strong>in</strong>e the so-called getters <strong>and</strong> setters of the property. The getter is accessed when<br />

an object is asked for the value of a specific property, <strong>and</strong> the setter is used to assign a value to the<br />

property. Properties only provide access to underly<strong>in</strong>g data stored <strong>in</strong> the object; they don’t conta<strong>in</strong><br />

the actual data. To store the data, you need what is called a back<strong>in</strong>g variable. This is often a simple<br />

field def<strong>in</strong>ed <strong>in</strong> the class that is able to store the value for the external property. In the follow<strong>in</strong>g<br />

example, the variable _firstName is the back<strong>in</strong>g variable for the FirstName property:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Public Class Person<br />

Private _firstName As Str<strong>in</strong>g<br />

Public Property FirstName() As Str<strong>in</strong>g<br />

Get<br />

Return _firstName<br />

End Get<br />

Set(value As Str<strong>in</strong>g)<br />

_firstName = value<br />

End Set<br />

End Property<br />

End Class<br />

C#<br />

public class Person<br />

{<br />

private str<strong>in</strong>g _firstName;<br />

public str<strong>in</strong>g FirstName<br />

{<br />

get { return _firstName; }<br />

set { _firstName = value; }<br />

}<br />

}<br />

It is common to prefix the private back<strong>in</strong>g variables with an underscore, followed by the first word <strong>in</strong><br />

all lowercase, optionally followed by more words that start with a capital aga<strong>in</strong>. So the FirstName<br />

property has a back<strong>in</strong>g variable called _firstName, LastName has one called _lastName, <strong>and</strong> so on.<br />

This way, all variables that apply to the entire class are nicely packed together <strong>in</strong> the IntelliSense list.<br />

Simply type an underscore <strong>in</strong> your code <strong>and</strong> you’ll get the full list of private variables. Note that the<br />

underscore is typically not used when def<strong>in</strong><strong>in</strong>g variables <strong>in</strong>side a function or a subrout<strong>in</strong>e.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!