Home

Classes' Fields

 

Introduction

Consider a class named House:

public class House
{
}

The section between the curly brackets, { and }, of a class is referred to as its body. In the body of a class, you can create a list of the parts that make up the class. Each of these parts must be a complete variable with a name and a data type. For example, here are the characteristics that make up a house, declared as the parts of the above Book class and each declared as a variable:

public class House
{
    string PropertyNumber;
    char PropertyType;
    byte Stories;
    uint bedrooms;
    decimal Value;
}

The variables declared in the body of a class are referred to as its member variables and each member variable is referred to as a field. The fields can be any type we have seen in the previous lesson. When creating a class, it is your job to decide what your object is made of.

 

HomePractical Learning: Introducing Class Members

Imagine you want to write a (console-based) program for a department store and the customer has given you a preliminary catalog as follows:

Dress
Stock #: 437876
Women Scoop Neck Dress
Unit Price: $148.00
Stock #: 792475
Men Lightweight Jacket
Unit Price: $185
Stock #: 740797
Girls Velour Active Skirt
Unit Price: $22.85
Stock: 681432
Women Python Print Leather Bag
$75.00
Stock #: 759470
Men Escalade Slip-On Shoes
$89.85
Stock #: 482746
Boys Leather Bomber Jacket 
$255.50

Each item in this catalog is represented by its Stock number, its name or description, and its price. Based on this, you can create a class that represents each item.

  1. Change the DepartmentStore class as follows:
     
    using System;
    
    public class DepartmentStore
    {
        long StockNumber;
        char Category;
        string ItemName;
        decimal UnitPrice;
    }
  2. Save the file

Accessing Class Members

The parts of an object fall into two main categories: those you can touch and those you don't have access to. For example, for a car parked at the mall, you can see or touch its doors and its tires but you don't see its engine or its spare tire, you don't even know if it has one. The parts of an object that you have access to are referred to as public. Those you can't see or touch are referred to as private.

A C# class also recognizes that some parts of a class can be made available to other classes and some other parts can be hidden from other classes. A part that must be hidden from other classes is private and it can be declared starting with the private keyword. If you declare a member variable and want to make it available to other classes, you must start its name with the public keyword. The public and private keywords are referred to as access level.

By default, if you declare a member variable (or anything else) in a class but don't specify its access level, the member is considered private and cannot be accessed from outside, that is by a non-member, of that class. Therefore, to make a member accessible by other classes, you must declare it as public.

You can use a mix of public and private members in a class and there is no rule on which access level should be listed first or last. Here are examples:

public class House
{
    string PropertyNumber;
    public char PropertyType;
    byte Stories;
    public uint bedrooms;
    private decimal Value;
}

Just keep in mind that if you omit or forget the access level of a member of a class, the member is automatically made private.

After declaring a member of a class, to access it from another class, first declare a variable from its class as we saw earlier. To actually access the member, use the period operator "." as follows:

using System;

public class House
{
    string PropertyNumber;
    public char PropertyType;
    byte Stories;
    public uint Bedrooms;
    private decimal Value;
}

class Program
{
    static void Main()
    {
        House property = new House();

        property.PropertyType = 'S';
        property.Bedrooms = 4;

        Console.WriteLine("=//= Altair Realty =//=");
        Console.WriteLine("Properties Inventory");;
        Console.Write("Property Type:  ");
        Console.WriteLine(property.PropertyType);
        Console.Write("Bedrooms:       ");
        Console.WriteLine(property.Bedrooms);
    }
}

To reduce confusion as to what member is public or private, we will always specify the access level of a member variable.

public class House
{
    public string PropertyNumber;
    public char PropertyType;
    public byte Stories;
    public uint Bedrooms;
    public decimal Value;
}

Practical Learning Practical Learning: Using a Class' Fields

  1. To make public the members of the DepartmentStore class, type the public keyword to their left:
     
    using System;
    
    class DepartmentStore
    {
        public long StockNumber;
        public char Category;
        public string ItemName;
        public decimal UnitPrice;
    }
  2. To access the Program.cs file, click its tab above the Code Editor
  3. Change Main() as follows:
     
    using System;
    
    class Exercise
    {
        static void Main()
        {
            DepartmentStore dptStore = new DepartmentStore();
    
            dptStore.StockNumber = 437876;
            dptStore.Category = 'W';
            dptStore.ItemName = "Scoop Neck Dress";
            dptStore.UnitPrice = 148.00;
    
            Console.WriteLine("Department Store");
            Console.Write("Stock #:    ");
    	Console.WriteLine(dptStore.StockNumber);
            Console.Write("Category:   ");
    	Console.WriteLine(dptStore.Category);
            Console.Write("Name:       ");
    	Console.WriteLine(dptStore.ItemName);
            Console.Write("Unit Price: ");
    	Console.WriteLine(dptStore.UnitPrice);
    	Console.WriteLine();
        }
    }
  4. Execute the application. This would produce:
     
    Department Store
    Stock #:    437876
    Category:   W
    Name:       Scoop Neck Dress
    Unit Price: 148.00
    
    Press any key to continue . . .
  5. Close the DOS window
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next