Pages

Sunday, 14 June 2015

Do Something to Class

Do Something to Class

Let's take a look at following Main code
class Program
{
 static void Main(string[] args)
 {
  account myAccount;         //instantiating account to myAccount variable
  myAccount = new account(20, "Yang", "Sopiana");  //allocating memory to myAccount

  myAccount.checkBalance();       //invoke checkBalance
  myAccount.store(20);        //invoke store
  myAccount.checkBalance();
  myAccount.withdraw(10);        //invoke withdraw
  myAccount.checkBalance();
  Console.WriteLine("press any key to close");
  Console.Read();
 }
}
Compile and run the code, you'll see:
Yang Sopiana has 0
Yang Sopiana store :20
Yang Sopiana has 20
Yang Sopiana withdraw :10
Yang Sopiana has 10
press any key to close

Instantiating Class

As I mentioned before, class is kind of predifined type. To instantiate the class to new object use following syntax:
[Access Modifier:Optional] [Class Name] [Object Name]
Example from precious code:
account myAccount;         //instantiating account to myAccount variable

Allocating Memory to new Object

Class is reference type, when instatiating the class to an object, the object's value will be nothing. To allocate memory for the actual data, you use the new operator.
  • The new operator allocates and initializes memory for an instance of the specified type. It allocates the memory from either the stack or the heap, depending on the type.
  • Use the new operator to form an object-creation expression, which consists of the following:
    • The keyword new.
    • The name of the type of the instance for which memory is to be allocated.
    • Matching parentheses, which might or might not include parameters.
Example:
myAccount = new account(20, "Yang", "Sopiana");  //allocating memory to myAccount

You can also make an object with initializer. For previous code, you can simplify:
account myAccount;         //instantiating account to myAccount variable
myAccount = new account(20, "Yang", "Sopiana");  //allocating memory to myAccount

to:
account myAccount = new account(20, "Yang", "Sopiana");  //declaring and allocating memory to myAccount

Accessing Class Member

If you work inside the class, you can access all member of class whatever the access modifier is. Simply just variable, like following:
public void withdraw(decimal ammount) //method withdraw
{
 Console.WriteLine(accountFirstName + " " + accountLastName + " withdraw :" + ammount);   //accessing members inside class
 accountBalance -= ammount;                                                               //accessing accountBalance inside class
}
But if you work outside the class, you can not access private members. You just can access public member, and in some condition you can access protected, internal members. Use [Object Name].[Class Member] to access class member outside the class, just like following commands
myAccount.checkBalance();       //invoke checkBalance
myAccount.store(20);        //invoke store
myAccount.checkBalance();
myAccount.withdraw(10);        //invoke withdraw
myAccount.checkBalance();

Putting It All Together

Here's the complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prog3
{
    public class account
    { 
        private uint accoundID;     //field accountID
        private string accountFirstName;  //field accountFirstName
        private string accountLastName;   //field accountLastName
        private decimal accountBalance;   //field accountBalance

        public account(uint ID, string firstName, string LastName) //Class's Constructor
        {
            accoundID = ID;
            accountFirstName = firstName;
            accountLastName = LastName;
        }
        
        public void store(decimal ammount)  //method store
        {
            Console.WriteLine(accountFirstName + " " + accountLastName + " store :" + ammount);
            accountBalance += ammount;
        }

        public void withdraw(decimal ammount) //method withdraw
        {
            Console.WriteLine(accountFirstName + " " + accountLastName + " withdraw :" + ammount);   //accessing members inside class
            accountBalance -= ammount;                                                               //accessing accountBalance inside class
        }

        public void checkBalance()    //method checkBalance
        {
            Console.WriteLine(accountFirstName + " " + accountLastName + " has " + accountBalance);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            account myAccount;         //instantiating account to myAccount variable
            myAccount = new account(20, "Yang", "Sopiana");  //allocating memory to myAccount

            myAccount.checkBalance();       //invoke checkBalance
            myAccount.store(20);        //invoke store
            myAccount.checkBalance();
            myAccount.withdraw(10);        //invoke withdraw
            myAccount.checkBalance();
            Console.WriteLine("press any key to close");
            Console.Read();
        }
    }
}

0 comments:

Post a Comment