Applying What We Have Learned So Far

johnmora's picture

The following code is from a Visual Studio 2008 C# Console application named CreateStruct. The code is from the Program.cs file. It illustrates what we have read until now, in a working application. The output of the Console application when run will be “John Mora (Male), age 32, 1234567890, Home”. If you ran this application and did not have read permissions on C:\boot.ini you would get an exception. The “[Serializable]” attribute allows the Manager class (which derives from the Person class) to be serialized, which will be covered later. I will be posting a second application, a windows forms application, which demonstrates responding to events.

   1: using System;
   2: //The System.Reflection namespace contains types that retrieve information about assemblies, modules, 
   3: //members, parameters, and other entities in managed code by examining their metadata.
   4: using System.Reflection;
   5: using System.Security.Permissions;
   6:  
   7: //attribute from System.Security.Permissions makes sure you have permission to read boot.ini or errors
   8: [assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, Read = @"C:\boot.ini")]
   9: namespace CreateStruct
  10: {
  11:     //Derive a new class from an existing class.
  12:     class Program
  13:     {    
  14:         static void Main(string[] args)
  15:         {
  16:             //Person p = new Person("Tony", "Allen", 32, Person.Genders.Male);
  17:             //Console.WriteLine(p.ToString());
  18:  
  19:             Manager m = new Manager("John", "Mora", 32, Person.Genders.Male, "1234567890", "Home");
  20:             Console.WriteLine(m.ToString());
  21:             Console.ReadKey();
  22:         }
  23:         //[Serializable] is an attribute (one of many) from the System.Reflection namespace
  24:         [Serializable]
  25:         class Manager : Person //Manager derives from Person
  26:         {
  27:             //Add two new Member fields
  28:             public string phoneNumber;
  29:             public string officeLocation;
  30:  
  31:             //Constructor: Overload the constructor to accept the new members.
  32:             public Manager(string _firstName, string _lastName, int _age, Genders _gender,
  33:                            string _phoneNumber, string _officeLocation)
  34:             //To overload, call the base class constructor, seen below (: base...)
  35:                            : base (_firstName, _lastName, _age, _gender)
  36:             {
  37:                 phoneNumber = _phoneNumber;
  38:                 officeLocation = _officeLocation;
  39:             }
  40:             
  41:             //Override the ToString() Method to add the two new member fields (phoneNumber. officeLocation)
  42:             public override string ToString()
  43:             {
  44:                 return base.ToString() + ", " + phoneNumber + ", " + officeLocation;
  45:             }
  46:         }
  47:  
  48:         class Person
  49:         {
  50:             public string firstName;
  51:             public string lastName;
  52:             public int age;
  53:             public Genders gender;
  54:  
  55:             public Person(string _firstName, string _lastName, int _age, Genders _gender)
  56:             {
  57:                 firstName = _firstName;
  58:                 lastName = _lastName;
  59:                 age = _age;
  60:                 gender = _gender;
  61:             }
  62:  
  63:             public override string ToString()
  64:             {
  65:                 return firstName + " " + lastName + " (" + gender + "), age " + age;
  66:             }
  67:  
  68:             public enum Genders : int { Male, Female };
  69:         }
  70:     }
  71: }

Powered by Drupal - Design by artinet