Jump to content
Xtreme .Net Talk

Recommended Posts

  • Administrators
Posted (edited)

Intro to OOP Part 2

 

Guide to Object Orientated code in .Net part 2

This post is a continuation of the one found here

 

Introduction

In the previous article we looked at how to create a simple class and saw how to then use the functionality it provided. In this article we will look at a feature that can make our coding life a little bit easier and our code a little bit cleaner.

 

Properties

One nice syntax feature .Net offers is the concept of a property � these can be accessed as if they were a simple public variable, but really are a function call. This means we get the best of both worlds � cleaner code syntax and the power of re-usable code.

 

In the previous article we could retrieve the current balance through the GetBalance() method, this however can result in potentially ugly syntax (and in certain languages like Java is the only way of doing so):

Imagine we needed to be able to set the balance without going through the Credit / Debit methods a simple concept like adding 10 to the balance would involve code like the following

�First we need a function to set the balance
   Public Function SetBalance(ByVal newBalance As Decimal) As Decimal
       _Balance = newBalance
   End Function

�to call it we end up with code like
Acc.SetBalance(acc.GetBalance() +10)

or

//new function to set balance
public decimal SetBalance(decimal newBalance)
{
_Balance = newBalance;
}
//would be called like
Acc.SetBalance(acc.GetBalance()+10);

Although not difficult code it can look messy and is quite hard to skim over.

Properties simplify this syntax by exposing the functions in a slightly different form

   Public Property Balance() As Decimal
       Get
           Return _Balance
       End Get
       Set(ByVal Value As Decimal)
           _Balance = Value
       End Set
   End Property
�would now be called like
Acc.Balnce = acc.Balnce+10

 

public decimal Balance
{
get
{
return _Balance;
}
set
{
_Balance=value;
}
}
//would be called like
Acc.Balnce = acc.Balnce+10;

Note that in the C# version the value to be assigned is implicitly passed as a parameter called value, even though this is never apparent from just looking at the code.

This results in a much cleaner and easier to read syntax, one problem this has introduced is that the balance can now be modified outside of the Credit and Debit methods., this is easily fixed by removing the set portions of the code (and marking it readonly in VB) like so.


   Public ReadOnly Property Balance() As Decimal
       Get
           Return _Balance
       End Get
   End Property

And

public decimal Balance
{
get
{
return _Balance;
}
}

 

Now we can only retrieve the value of the balance through this property and not assign values to it directly.

 

Although in this case the functionality is trivial we are introducing an extra function call by using properties rather than a public variable; despite this performance issue there are three good reasons to go with properties:

1) We get compile time detection of attempts to update the value direct � these are really a breach of our business logic and shouldn�t occur. Using properties will prevent a successful compile occurring and prevent people bypassing our methods.

2) If we do a release build the Just In Time (JIT) compiler will very probably inline the call � this basically removes the function call and at runtime access the variable direct anyway (but still read-only as the compiler would have prevented any other kind of access)

3) In a more complex scenario we could do much more complex validation in the set portion of the property and reject values that do not meet our business rules and we could also perform complex calculations in the get portion � allowing us to either calculate values on the fly if the internal store is in a different format (remember the idea of a date using UTC or local time) or is physically stored elsewhere e.g. a Database.

 

Playtime

Have a look at the attached samples and see how using properties works in code

 

Next

Methods

BankCS.zip

BankVB.zip

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...