C# 'as' Keyword and Derived Classes
<p>I am working my way through "Sams Teach Yourself C# in 21 Days", obviously, this makes me a novice programmer. Please don't be too harsh. So far, being objective, I've feel I've done quite well understanding the topics. I am not looking for the answer, but hopefully be pointed in the right direction. :)</p>

<p>Anyway, I have this code:</p>

<pre><code>// EXERCISE 11.4

using System;

public class Person
{
    public string Name;

    public Person()
    {
    }

    public Person( string nm )
    {
        Name = nm;
    }

    public virtual void displayFullName()
    {
        Console.WriteLine( "Person {0}", Name );
    }
}

class Employee : Person
{
    //public ushort hireYear;

    public Employee()
        : base()
    {
    }

    public Employee( string nm )
        : base( nm )
    {
    }

    public override void displayFullName()
    {
        Console.WriteLine( "Employee: {0}", Name );
    }
}

class Contractor : Person
{
    //public string Company;

    public Contractor()
        : base()
    {
    }

    public Contractor( string nm )
        : base( nm )
    {
    }

    public override void displayFullName()
    {
        Console.WriteLine( "Contractor: {0}", Name );
    }

    public void DisplayCompany()
    {
        Console.WriteLine( "Company: {0}", Company );
    }
}

class MyApplication
{
    public static void Main()
    {
        Person [ ] myCompany = new Person[5];
        int Counter = 0;
        string Buffer, Buffer2;

        do
        {
            do
            {
                Console.Write( "\nEnter \'c\' for Contractor, \'e\' for Employee then press ENTER: " );
                Buffer = Console.ReadLine();
            } while( Buffer == "" );

            if( Buffer[0] == 'c' || Buffer[0] == 'C' )
            {
                Console.Write( "\nEnter the contractor\'s name: " );
                Buffer = Console.ReadLine();
                // DO OTHER CONTRACTOR STUFF
                Contractor Contr = new Contractor( Buffer );
                myCompany[Counter] = Contr as Person;
            }
            else if( Buffer[0] == 'e' || Buffer[0] == 'E' )
            {
                Console.Write( "\nEnter the employee\'s name: " );
                Buffer = Console.ReadLine();
                // DO OTHER EMPLOYEE STUFF
                Employee emp = new Employee( Buffer );
                myCompany[Counter] = emp as Person;
            }
            else
            {
                Person pers = new Person( "Not an Employee or Contractor" );
                myCompany[Counter] = pers;
            }

            Counter++;

        } while( Counter &lt; 5 );

        Console.WriteLine( "\n\n\n===========================" );

        for( Counter = 0; Counter &lt; 5; Counter++ )
        {
            if( myCompany[Counter] is Employee )
            {
                Console.WriteLine( "Employee: {0}", myCompany[Counter].Name );
            }
            else if( myCompany[Counter] is Contractor )
            {
                Console.WriteLine( "Contractor: {0}.", myCompany[Counter].Name );
            }
            else
            {
                Console.WriteLine( "Person: {0}", myCompany[Counter].Name );
            }
        }

        Console.WriteLine( "===========================" );

        Console.Read();
    }
}
</code></pre>

<p>In the excercise I've to modify the Contractor or Employee class and add either a hireYear or Company data member respectively.</p>

<p>Which would look like this:</p>

<pre><code>class Employee : Person
{
    public ushort hireYear;

    public Employee()
        : base()
    {
    }

    public Employee( string nm )
        : base( nm )
    {
    }

    public Employee( string nm, ushort hy )
        : base( nm )
    {
        hireYear = hy;
    }

    public override void displayFullName()
    {
        Console.WriteLine( "Employee: {0}", Name );
    }
}
</code></pre>

<p>OR</p>

<pre><code>class Contractor : Person
{
    public string Company;

    public Contractor()
        : base()
    {
    }

    public Contractor( string nm )
        : base( nm )
    {
    }

    public Contractor( string nm, string c )
        : base( nm )
    {
        Company = c;
    }

    public override void displayFullName()
    {
        Console.WriteLine( "Contractor: {0}", Name );
    }

    public void DisplayCompany()
    {
        Console.WriteLine( "Company: {0}", Company );
    }
}
</code></pre>

<p>The changes to the MainApplication would be:</p>

<pre><code>            if( Buffer[0] == 'c' || Buffer[0] == 'C' )
            {
                string Buffer2;

                Console.Write( "\nEnter the contractor\'s name: " );
                Buffer = Console.ReadLine();

                Console.Write( "\nEnter the contractor\'s company: " );
                Buffer2 = Console.ReadLine();

                Contractor Contr = new Contractor( Buffer, Buffer2 );
                myCompany[Counter] = Contr as Person;
            }
</code></pre>

<p>OR</p>

<pre><code>            else if( Buffer[0] == 'e' || Buffer[0] == 'E' )
            {
                string BufferHireYear;

                Console.Write( "\nEnter the employee\'s name: " );
                Buffer = Console.ReadLine();

                Console.Write( "\nEnter the year employee was hired: " );
                BufferHireYear = Console.ReadLine();

                Employee emp = new Employee( Buffer, BufferHireYear );
                myCompany[Counter] = emp as Person;
            }
</code></pre>

<p>I am quite happy until this point, I think. My confusion starts when I need to print out the results. My thinking is that when the objects are "cast" back into the myCompany array, they are added as objects of type person. The class for Person does not contain the data members Company or HireYear, so how can I access those data members?</p>

<p>Thank-you for reading through this posting, I am sure someone will be able to help.</p>

<p>I've got to learn.</p>

<p>Matt</p>
