Changes

Jump to: navigation, search

C Sharp Object Oriented Programming

4,196 bytes added, 17:22, 18 January 2008
Instantiating an Object from a C# Class
Now that we have an instance of our BankAccount class the next step is learn how to access the members of the class.
 
== Accessing C# Object Members ==
 
Now that we know how to write a class and instantiate objects from the class, we now need to know how to access the memebers of the object. Firstly, you will recall that we declared some members as being ''public'' and others as being private. The ''public'' methods are fully accessible from outside the object. This is achieved using something called ''dot notation''. Dot notation is a mechanism by which object members may be accessed specifying the object and member names separated by a dot (.). For example, to access the ''accountName'' member of an object named ''custAccount'' we would reference this member using ''custAccount.accountName'':
 
<pre>
using System;
 
class Hello
{
public class BankAccount
{
 
public string accountName;
public int accountFee;
private int accountBalance;
private int accountNumber;
}
 
static void Main()
{
 
BankAccount custAccount = new BankAccount();
 
custAccount.accountName = "John Smith";
 
custAccount.accountFee = 5;
 
Console.WriteLine ("Customer Name is " + custAccount.accountName);
 
Console.WriteLine ("Account Fee = $" + custAccount.accountFee);
}
}
</pre>
 
The above code assigns values to the accountName and accountFee memebrs of our object. It then references the properties in order to display text which reads:
 
<pre>
Customer Name is John Smith
Account Fee = $5
</pre>
 
This approach works well for ''public'' class members, but not for ''private'' members. An attempt, for example, to use dot notation to access the private ''accountNumber'' member will result in a compilation error along the the lines of:
 
error CS0122: 'Hello.BankAccount.accountNumber' is inaccessible due to its protection level
 
In order to access ''private'' members we need to provide access methods in the class.
 
== Adding Methods to a C# Class ==
 
Class method members are sections of code contained in a class which can be called from outside an object to perform specific tasks. A common types of methods are getter and setter methods which are used to access ''private'' data members of a class. For example, we can declare a getter and a setter method to get and set the protected ''accountNumber'' member:
 
<pre>
public class BankAccount
{
 
public string accountName;
public int accountFee;
private int accountBalance;
private int accountNumber;
 
public int getAccountNumber()
{
return accountNumber;
}
 
public void setAccountNumber(int newNumber)
{
accountNumber = newNumber;
}
 
}
 
Class methods are called using dot notation. For example, to set the value of the accountNumber:
 
<pre>
BankAccount custAccount = new BankAccount();
 
custAccount.setAccountNumber( 12345 );
 
Console.WriteLine ("Account Number = " + custAccount.getAccountNumber() );
</pre>
 
The above code sets the account number using the setter method and then displays the account number using the ''getter'' method.
 
Now that we have looked at method class members the next task is to look at two special class methods, the consructor and finalizers.
 
== C# Constructors and Finalizers ==
 
Despite the grand sounding names, C# class fonstructors and finalizers are nothing more than mehtods which get called when an object is instantiated and destroyed. The constructor is particularly useful for allowing initialization values to be passed through to an object at creation time. Lets say that we would like to be able to initialize the accountName and accountNumber members at the point that we initialize the custAccount object. To do so we need to declare a constructor.
 
Constructors are declared the same way as other methods with the exception that the name of the mentod must match the class name:
 
<pre>
public class BankAccount
{
 
public string accountName;
public int accountFee;
private int accountBalance;
private int accountNumber;
 
public BankAccount(string acctName, int acctNumber)
{
accountName = acctName;
accountNumber = acctNumber;
}
 
// ....
 
}
</pre>
 
We can now use the constructor to initialize these members at object creation:
 
<pre>
BankAccount custAccount = new BankAccount("Fred Wilson", 123456);
</pre>
== Defining Class Methods ==

Navigation menu