Intro to Object Orientated Programming Part 2 - Properties

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
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
Visual Basic:
‘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
C#:
//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
Visual Basic:
    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

C#:
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.
Visual Basic:
    Public ReadOnly Property Balance() As Decimal
        Get
            Return _Balance
        End Get
    End Property
And
C#:
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
 

Attachments

  • BankCS.zip
    9 KB · Views: 21
  • BankVB.zip
    11.5 KB · Views: 26
Last edited:
Top Bottom