Classes' Fields |
|
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. Imagine you want to write a (console-based) program for a department store and the customer has given you a preliminary catalog as follows:
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.
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; }
|
|
||
Previous | Copyright © 2006-2007 FunctionX, Inc. | Next |
|