Classes Interactions |
|
A class can be created inside of another class. A class created inside of another is referred to as nested. To nest a class, simply create it as you would any other. Here is an example of a class called Inside that is nested in a class called Outside: public class Outside { public class Inside { } } In the same way, you can nest as many classes as you wish in another class and you can nest as many classes inside of other nested classes if you judge it necessary. Just as you would manage any other class so can you exercise control on a nested class. For example, you can declare all necessary fields, properties, or methods in the nested class or in the nesting class. When you create one class inside of another, there is no special programmatic relationship between both classes: just because a class is nested does not mean that the nested class has immediate access to the members of the nesting class. They are two different classes and they can be used separately as you judge it necessary. The name of a nested class is not "visible" outside of the nesting class. To access a nested class outside of the nesting class, you must qualify the name of the nested anywhere you want to use it. For example, if you want to declare an Inside variable somewhere in the program but outside of Outside, you must qualify its name. Here is an example: using System; public class Outside { public class Inside { public Inside() { Console.WriteLine(" -= Inside =-"); } } public Outside() { Console.WriteLine(" =- Outside -="); } } class Exercise { static void Main() { Outside Recto = new Outside(); Outside.Inside Ins = new Outside.Inside(); } } This would produce: =- Outside -= -= Inside =- Because there is no programmatically privileged relationship between a nested class and its "container" class, if you want to access the nested class in the nesting class, you can use its static members. In other words, if you want, you can declare static all members of the nested class that you want to access in the nesting class. Here is an example: using System; public class Outside { public class Inside { public static string InMessage; public Inside() { Console.WriteLine(" -= Insider =-"); InMessage = "Sitting inside while it's raining"; } public static void Show() { Console.WriteLine("Show me the wonderful world of C# Programming"); } } public Outside() { Console.WriteLine(" =- The Parent -="); } public void Display() { Console.WriteLine(Inside.InMessage); Inside.Show(); } } class Exercise { static void Main() { Outside Recto = new Outside(); Outside.Inside Ins = new Outside.Inside(); Recto.Display(); } } In the same way, if you want to access the nesting class in the nested class, you can go through the static members of the nesting class. To do this, you can declare static all members of the nesting class that you want to access in the nested class. Here is an example: using System; public class Outside { public class Inside { public static string InMessage; public Inside() { Console.WriteLine(" -= Insider =-"); InMessage = "Sitting inside while it's raining"; } public static void Show() { Console.WriteLine("Show me the wonderful world of C# Programming"); } public void FieldFromOutside() { Console.WriteLine(Outside.OutMessage); } } private static string OutMessage; public Outside() { Console.WriteLine(" =- The Parent -="); OutMessage = "Standing outside! It's cold and raining!!"; } public void Display() { Console.WriteLine(Inside.InMessage); Inside.Show(); } } class Exercise { static void Main() { Outside Recto = new Outside(); Outside.Inside Ins = new Outside.Inside(); Recto.Display(); Console.WriteLine(); Ins.FieldFromOutside(); } } This would produce: =- The Parent -= -= Insider =- Sitting inside while it's raining Show me the wonderful world of C# Programming Standing outside! It's cold and raining!! Instead of static members, if you want to access members of a nested class in the nesting class, you can first declare a variable of the nested class in the nesting class. In the same way, if you want to access members of a nesting class in the nested class, you can first declare a variable of the nesting class in the nested class. Here is an example: using System; public class Outside { // A member of the nesting class private string OutMessage; // The nested class public class Inside { // A field in the nested class public string InMessage; // A constructor of the nested class public Inside() { Console.WriteLine(" -= Insider =-"); this.InMessage = "Sitting inside while it's raining"; } // A method of the nested class public void Show() { // Declare a variable to access the nesting class Outside outsider = new Outside(); Console.WriteLine(outsider.OutMessage); } } // End of the nested class // A constructor of the nesting class public Outside() { this.OutMessage = "Standing outside! It's cold and raining!!"; Console.WriteLine(" =- The Parent -="); } // A method of the nesting class public void Display() { Console.WriteLine(insider.InMessage); } // Declare a variable to access the nested class Inside insider = new Inside(); } class Exercise { static void Main() { Outside Recto = new Outside(); Outside.Inside Ins = new Outside.Inside(); Ins.Show(); Recto.Display(); } } This would produce: -= Insider =- =- The Parent -= -= Insider =- -= Insider =- =- The Parent -= Standing outside! It's cold and raining!! Sitting inside while it's raining
Just like any of the variables we have used so far, you can make a class or a structure a member variable, also called a field, of another class. To use a class in your own class, of course you must have that class. You can use one of the classes already available in C# or you can first create your own class. Here is an example of a class: public class MVADate { private int DayHired; private int MonthHired; private int YearHired; } A field is a member variable created from another class instead of primitive types. To use a class as a member of your class, simply declare its variable as you would proceed with any of the member variables we have declared so far. Here is an example: using System; public class MVADate { private int DayHired; private int MonthHired; private int YearHired; } public class MotorVehicleAdministration { MVADate MDate; static int Main() { return 0; } } After a class has been declared as a member variable of another class, it can be used regularly. Because the member is a class, declared as a reference (in (Managed) C++, it would be declared as a pointer) instead of a value type, there are some rules you must follow to use it. After declaring the member variable, you must make sure you have allocated memory for it on the heap. You must also make sure that the variable is initialized appropriately before it can be used; otherwise you would receive an error when compiling the file. Imagine you declare a variable of class A as a member of class B. If you declare a variable of class B in an external function, as we have done so far in the Main() function, to access a member of class A, you must fully qualify it. |
|
Practical Learning: Declaring a Class as a Member Variable |
using System; public class MVADate { private int dayOfBirth; private int monthOfBirth; private int yearOfBirth; public void SetDate(int d, int m, int y) { dayOfBirth = d; monthOfBirth = m; yearOfBirth = y; } public string ProduceDate() { string result = dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth; return result; } } public class MotorVehicleAdministration { public MotorVehicleAdministration() { birthdate = new MVADate(); } private string fullName; private MVADate birthdate; private bool isAnOrganDonor; static int Main() { MotorVehicleAdministration MVA = new MotorVehicleAdministration(); Console.WriteLine("To process a registration, enter the information"); Console.Write("Full Name: "); MVA.fullName = Console.ReadLine(); Console.Write("Day of Birth: "); int d = int.Parse(Console.ReadLine()); Console.Write("Month of Birth: "); int m = int.Parse(Console.ReadLine()); Console.Write("Year of Birth: "); int y = int.Parse(Console.ReadLine()); Console.Write("Is the application an organ donor (0=No/1=Yes)? "); string ans = Console.ReadLine(); if( ans == "0" ) MVA.isAnOrganDonor = false; else MVA.isAnOrganDonor = true; MVA.birthdate.SetDate(d, m, y); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); Console.WriteLine(" -=- Driver's License Application -=-"); Console.WriteLine("Full Name: {0}", MVA.fullName); Console.WriteLine("Dateof Birth: {0}", MVA.birthdate.ProduceDate()); Console.WriteLine("Organ Donor? {0}", MVA.isAnOrganDonor); Console.WriteLine(); return 0; } } |
To process a registration, enter the information Full Name: Ernestine Mvouama Day of Birth: 8 Month of Birth: 14 Year of Birth: 1984 Is the application an organ donor (0=No/1=Yes)? 1 -=- Motor Vehicle Administration -=- -=- Driver's License Application -=- Full Name: Ernestine Mvouama Dateof Birth: 8/14/1984 Organ Donor? True |
Classes and External Methods |
Returning a Class From a Method |
Like a value from regular type, you can return a class value from a method of a function. To do this, you can first declare the member function and specify the class as the return type. Here is an example: using System; public class MVADate { } public class MotorVehicleAdministration { private static MVADate RequestDateOfBirth() { } static int Main() { MotorVehicleAdministration MVA = new MotorVehicleAdministration(); Console.WriteLine(); return 0; } } After implementing the function, you must return a value that is conform to the class, otherwise you would receive an error when compiling the application. You can proceed by declaring a variable of the class in the body of the function, initializing the variable, and then returning it. Once a function has returned a value of a class, the value can be used as normally as possible.
Once a class has been created, it can be used like any other variable. For example, its variable can be passed as argument to a method of another class. When a class is passed as argument, its public members are available to the function that uses it. Here is an example: class CarRegistration { public void DisplayDriversLicense(MotorVehicleAdministration mva) { Console.WriteLine("Full Name: {0}", mva.fullName); Console.WriteLine("Dateof Birth: {0}", mva.birthdate.ProduceDate()); Console.WriteLine("Organ Donor? {0}", mva.isAnOrganDonor); } } public class MotorVehicleAdministration { public string fullName; public MVADate birthdate; public bool isAnOrganDonor; } In the same way, you can pass more than one class as arguments to a function. Because classes are always used by reference, when passing a class as argument, it is implied to be passed by reference. To reinforce this, you can type the ref keyword to the left of the argument. Here is an example: |
class CarRegistration { public void DisplayDriversLicense(ref MotorVehicleAdministration mva) { Console.WriteLine("Full Name: {0}", mva.fullName); Console.WriteLine("Dateof Birth: {0}", mva.birthdate.ProduceDate()); Console.WriteLine("Organ Donor? {0}", mva.isAnOrganDonor); } }
Practical Learning: Passing a Class as Argument |
using System; namespace ConsoleApplication1 { public class MVADate { private int dayOfBirth; private int monthOfBirth; private int yearOfBirth; public void SetDate(int d, int m, int y) { dayOfBirth = d; monthOfBirth = m; yearOfBirth = y; } public string ProduceDate() { string result = dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth; return result; } } class Car { public string Make; public string Model; public int CarYear; } class CarRegistration { public void DisplayDriversLicense(MotorVehicleAdministration mva) { Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); Console.WriteLine(" -=- Car Registration -=-"); Console.WriteLine("Full Name: {0}", mva.fullName); Console.WriteLine("Dateof Birth: {0}", mva.birthdate.ProduceDate()); Console.WriteLine("Organ Donor? {0}", mva.isAnOrganDonor); } public void DisplayCarInformation(Car v) { Console.WriteLine("Car: {0} {1}", v.Make, v.Model); Console.WriteLine("Year: {0}", v.CarYear); } } public class MotorVehicleAdministration { public MotorVehicleAdministration() { birthdate = new MVADate(); } public string fullName; public MVADate birthdate; public bool isAnOrganDonor; public Car NewCar; private static MVADate RequestDateOfBirth() { MVADate date = new MVADate(); Console.Write("Day of Birth: "); int d = int.Parse(Console.ReadLine()); Console.Write("Month of Birth: "); int m = int.Parse(Console.ReadLine()); Console.Write("Year of Birth: "); int y = int.Parse(Console.ReadLine()); date.SetDate(d, m, y); return date; } private static Car RegisterCar() { Car c = new Car(); Console.Write("Make: "); c.Make = Console.ReadLine(); Console.Write("Model: "); c.Model = Console.ReadLine(); Console.Write("Year: "); c.CarYear = int.Parse(Console.ReadLine()); return c; } static void Main() { MotorVehicleAdministration MVA = new MotorVehicleAdministration(); CarRegistration regist = new CarRegistration(); Car vehicle = new Car(); Console.WriteLine("To process a registration, enter the information"); Console.Write("Full Name: "); MVA.fullName = Console.ReadLine(); MVA.birthdate = RequestDateOfBirth(); Console.Write("Is the application an organ donor (0=No/1=Yes)? "); string ans = Console.ReadLine(); if( ans == "0" ) MVA.isAnOrganDonor = false; else MVA.isAnOrganDonor = true; Console.WriteLine("Enter the following information for Car Registration"); MVA.NewCar = RegisterCar(); regist.DisplayDriversLicense(MVA); regist.DisplayCarInformation(MVA.NewCar); Console.WriteLine(); } } } |
To process a registration, enter the information Full Name: Alan Scott Day of Birth: 2 Month of Birth: 10 Year of Birth: 1974 Is the application an organ donor (0=No/1=Yes)? 0 Enter the following information for Car Registration Make: Ford Model: Crown Victoria Year: 1998 -=- Motor Vehicle Administration -=- -=- Car Registration -=- Full Name: Alan Scott Dateof Birth: 2/10/1974 Organ Donor? False Car: Ford Crown Victoria Year: 1998 |
|
||
Previous | Copyright © 2004-2012, FunctionX | Next |
|