Personal tools
Navigation
Log in


Forgot your password?
 
Document Actions

Array of People

by Dr. William L. Honig — last modified 2014-10-20 06:30 PM

A first example of a class (People) and using it in an array (so you have many people)

Click here to get the file

Size 1.3 kB - File type text/x-csharp

File 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());
			}
		}
	}
}
« October 2027
Su Mo Tu We Th Fr Sa
12
3456789
10111213141516
17181920212223
24252627282930
31
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: