Array of People
A first example of a class (People) and using it in an array (so you have many people)
Size 1.3 kB - File type text/x-csharpFile contents
using System;
namespace PersonInArray
{
class Person
{
private string name;
private string address;
public Person()
{
name = "None";
address = "Unknown 00000";
}
public Person( string theName, string theAddress )
{
name = theName;
address = theAddress.Trim ();
}
public string getZipCode()
{
return address.Substring (address.Length - 5, 5); //not good programming
}
public override string ToString()
{
return name + "\n" + address;
}
}
class PersonTester
{
public static void Main (string[] args)
{
Person p1;
Person p2;
Person[] pArray;
p1 = new Person ();
p2 = new Person ("The Boss", "820 N Michigan Ave, Chicago, IL 60611");
pArray = new Person[3];
Console.WriteLine (pArray[1] == null ? "it is null" : "something else");
pArray[0] = new Person ();
pArray[1] = new Person ("The President", "The White House, Washington, DC 20500");
pArray[2] = p2;
Console.WriteLine (p1.ToString());
Console.WriteLine ("extracted ZIP is " + p1.getZipCode() + "\n");
Console.WriteLine (p2.ToString());
Console.WriteLine ("extracted ZIP is " + p2.getZipCode() + "\n");
foreach (Person a in pArray)
{
Console.WriteLine (a.ToString());
}
}
}
}
Click here to get the file