Learning C# Part 4: Classes and Objects

Written By: Kevin Jordan

- 14 Oct 2006 -
















Description: This series aims to take a beginning programmer to an intermediate level in C#. Topics covered in Part 4 include: classes and objects, accessibility and scope, methods, constructors, static class members, namespaces, and XML commenting.

  1. Classes and Objects
  2. The Stack and the Heap
  3. Defining a Class and Creating an Object
  4. Defining Accessibility and Scope
  5. Passing Parameters to a Method
  6. Overloading Methods
  7. Using Constructors
  8. Static Class Members
  9. Namespaces and XML Commenting
  10. Aww Man... Not Homework

Defining Accessibility and Scope

I'm sure you noticed that we didn't touch the account number or the balance. And when you think about it, you shouldn't be able to. What if I set the account number and another client already had it, or changed the balance owed to a negative number? Then we have to pay them money! We need a way to protect our properties from putting in bad data. Well of course there's one; otherwise I wouldn't have mentioned it.

Remember how we used public earlier, this is an access modifier. By using these, you can define the scope of class members. Scope refers to the region of code from which an element of the program can be referenced. Here's a list of the access modifiers:

public Access not limited
private Access limited to containing class
protected Access limited to the containing class and to types derived from the containing class
internal Access limited to this program
protected internal Access limited to the containing class, derived classes, or to members of this program

I use public, private, and protected daily. I'm not cool enough to use internal I guess. So let's say we made accountNumber and balance private. How could we get or set the values? There's two ways actually. I'll show you how to use methods to set properties for now, and we'll save the other way for later.

class Client
{
        // Properties to store important information
        public string name;           // stores the name of our client
        private uint accountNumber;   // stores their account number
        private decimal balance;      // shows how much they owe us
 
        // We'll use this to retrieve the account number. Notice the method
        // is public and it returns a uint
        public uint GetAccountNumber()
        {
                // This simply returns the account number
                return accountNumber;
        }
 
        // This is going to set the account number while ensuring that it is unique
        // Notice that it is also a public method but doesn't return anything
        // hence the void
        public void SetAccountNumber()
        {
                // Create a temporary variable while we find a unique value
                uint tempAccount = 1;
 
                // Ignore the CheckIfAccountExists() for now, just know that
                // it will return a boolean true if it exists and false if it doesn't
                while (CheckIfAccountExists(tempAccount) == true)
                {
                        // Increment the temp varialbe if it already exists
                        tempAccount++;
                }
                
                // Now that we have a unique account number, set the
                // real accountNumber to the tempAccount value
                accountNumber = tempAccount;
        }
}

Then on our page we would create an instance of the Client class.

// This is our WinForm
class WinForm
{
        // Main is executed first on winform apps
        static void Main()
        {
                // Create our new client
                Client newClient = new Client();
                newClient.name = "Rox Steady";      // name is now "Rox Steady"
 
                // This sets the account number for us, lets pretend it going to be 6
                newClient.SetAccountNumber();
                Console.Write(newClient.GetAccountNumber());      // Outputs 6
        }
}

<< Previous

Next >>