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.

194 x CHAPTER 5 PROGRAMMING YOUR <strong>ASP</strong>.<strong>NET</strong> WEB PAGES<br />

Read-only or write-only properties <strong>in</strong> C# are simple: Just leave out the setter (for a read-only property)<br />

or the getter (for a write-only property). <strong>VB</strong>.<strong>NET</strong> is a bit more verbose <strong>and</strong> wants you to specify<br />

the keyword ReadOnly or WriteOnly explicitly. The follow<strong>in</strong>g code snippet shows a read-only<br />

FullName property <strong>in</strong> both <strong>VB</strong>.<strong>NET</strong> <strong>and</strong> C#:<br />

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

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

Get<br />

Return _firstName & " " & _lastName<br />

End Get<br />

End Property<br />

C#<br />

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

{<br />

get { return _firstName + " " + _lastName; }<br />

}<br />

When you try to assign a value to a read-only property, you’ll get a compilation error <strong>in</strong> VS.<br />

Similar to properties, objects can also have methods.<br />

Methods<br />

If properties are the th<strong>in</strong>gs that a class has (its characteristics), then methods are the th<strong>in</strong>gs a class<br />

can do or the operations it can perform. A Car class, for example, has properties such as Br<strong>and</strong>,<br />

Model, <strong>and</strong> Color. Its methods could be Drive(), Brake(), <strong>and</strong> OpenDoors(). Methods give objects<br />

the behavior that enables them to do someth<strong>in</strong>g.<br />

You have already seen methods at work earlier, when this chapter discussed some ways to write<br />

organized code us<strong>in</strong>g subs <strong>and</strong> functions. You simply add methods to a class by writ<strong>in</strong>g a function<br />

or a sub between the start <strong>and</strong> end elements of the class. For example, imag<strong>in</strong>e the Person class has<br />

a Save method that enables the object to persist itself <strong>in</strong> the database. The method’s signature could<br />

look like this:<br />

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

Public Class Person<br />

Public Sub Save()<br />

' Implementation goes here<br />

End Sub<br />

End Class<br />

C#<br />

public class Person<br />

{<br />

public void Save()<br />

{<br />

// Implementation goes here<br />

}<br />

}<br />

If you want to call the Save method to have the Person object save itself to the database, you create<br />

an <strong>in</strong>stance of it, set the relevant properties such as FirstName, <strong>and</strong> then call Save:

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

Saved successfully!

Ooh no, something went wrong!