• 0

c# object oriented contact book


Question

ok so i have to make a phone book using constructors properties and an array of objects and have been doing it for 12 hours straight i want to confirm im going down the right lines as i am unsure as to how i can test this at this time.

its a windows application with 3 input fields name, phone number and email address.

the person class contains the properties

the phone book should hold the array of objects but as far as i can tell doesnt and im not sure why.

private void addBtn_Click(object sender, EventArgs e)

{

string forename;

string phoneNumber;

string emailAddress;

forename = forenameTxt.Text;

phoneNumber = phoneNumTxt.Text;

emailAddress = emailAddressTxt.Text;

if (forenameTxt.Text != "" && phoneNumTxt.Text != "" && emailAddressTxt.Text != "")

{

person create = new person(forename, phoneNumber, emailAddress);

}

else

{

MessageBox.Show("Error: Not all values added!");

}

}

public person(string forename, string phoneNumber, string emailAddress)

{

_Forename = forename;

_PhoneNumber = phoneNumber;

_EmailAddress = emailAddress;

phonebook addContact = new phonebook(_Forename, _PhoneNumber, _EmailAddress);

}

public string Forename

{

get

{

return _Forename;

}

set

{

_Forename = value;

}

}

public string phoneNumber

{

get

{

return _PhoneNumber;

}

set

{

_PhoneNumber = value;

}

}

public string emailAddress

{

get

{

return _EmailAddress;

}

set

{

_EmailAddress = value;

}

}

}

public class phonebook

{

private string Forname;

private string PhoneNumber;

private string EmailAddress;

int contactCount = 0;

public phonebook(string forename, string phoneNumber, string emailAddress)

{

Forname = forename;

PhoneNumber = phoneNumber;

EmailAddress = emailAddress;

person[] addContact = new person[100];

for (int i = 0; i < 101; i++)

{

addContact[this.contactCount] = new person(Forname, PhoneNumber, EmailAddress);

}

}

}

}

Edited by lost and confused
Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Hi there,

After looking at the code for the phonebook class, I can see a few problems that you may run into.

Firstly, each time that you add a contact, you are creating an array, appending the new contact 100 times, and then finishing the function. This will not store the new contact, as the array is only visible within the addContact function.

A different way to implement this, would be to use a List (System.Collections.Generic.List). So where you have your variables at the beginning of the phonebook class, you would need to add a new one such as:

private List&lt;person&gt; contacts;

You will need to initialise this in your constructor, and when your addContact method is called, you will take a person object as a parameter instead of contactCount, and then call

contacts.add(thePerson);

This will add a contact to the list, and as such your phonebook will behave properly and contain all contacts you give it.

Secondly, you need to redefine that data you store in your Phone Book. I would imagine that the whole point is to have a phone book that contains lots of "person" objects. In your current version, you are storing the Forename, Phone Number and Email. Try just keeping a list of "person" objects (as above) and having one instance of the Phone Book class on your windows form. Then, whenever the "addBtn" is clicked, you just need to grab the data from the text boxes, as you are already doing, create a "person" object, and then add that to the existing Phone Book class using:

person p = new person(Forename, Phone, Email);
myPhoneBook.addContact(p);

Hope this helps,

Chris

Link to comment
Share on other sites

  • 0

A few things to keep your code tidy...

Rather than creating a new instance of the Person object and passing the property values in the constructor, try this:

public class Person
{
public string ForeName {get;set;}
public string LastName {get;set;}
public string EmailAddress {get;set;}
}

Then to create a new instance:

var person = new Person
{
ForeName = "first name...",
LastName = "last name...",
EmailAddress = "email@domain.com"
}

Just an idea, because a phone book can have MANY people (person) in:

public class Phonebook
{
public List&lt;Person&gt; People {get;set;}
public void AddPersonToList(Person person)
{
People.Add(person);
}
}

public class Person
{
public string ForeName {get;set;}
public string LastName {get;set;}
public string EmailAddress {get;set;}
}

GE

Link to comment
Share on other sites

  • 0

The phonebook class seems entirely useless to me, since you only ever need one instance of it and it only contains one object, your array.

If you really need to use an array, you need to maintain a count for your Persons in the array. An array is really not the right container for this job. A List<> or a Dictionary<> could do this much more easily.

OK let's say you use an array. In that case you would need a count.

Put the array to your Form1 class and initialize it in the Form1 constructor (or when declaring it).

	public partial class Form1 : Form
	{

		public Person[] PhoneBook;
		public int PeopleCount;

		public Form1()
		{
			InitializeComponent();
			PhoneBook = new Person[100];
		}

In the addBtn_Click() method, you would create a new instance of Person with the values in the text box and add it to your array (let's name it PhoneBook). Whenever you add a new person to the array, you need to increment the PeopleCount field.

		private void addBtn_Click(object sender, EventArgs e)
		{

			if (forenameTxt.Text != "" &amp;&amp; phoneNumTxt.Text != "" &amp;&amp; emailAddressTxt.Text != "")
			{

				 Person person = new person(forenameTxt.Text, phoneNumTxt.Text, emailAddressTxt.Text);

				 // We set the value to an empty 'slot'
				 PhoneBook[PeopleCount] = person;

				 // We increment the count so that next time when we add a person,
								 // we won't override the previous person.
				 PeopleCount++;

			}
			else
			{
				MessageBox.Show("Error: Not all values added!");

			}

		}

The current code is not secure whenever you create more people than your array can hold. Can be easily fixed with a check before adding to the array though.

It would be nice if your public properties, classes and fields are written PascalCase.

Link to comment
Share on other sites

  • 0

the phonebook class will eventually hold alot more methods but im taking them one at a time as im fairly knew to c# and trying to get my head round what ive been asked to make.

http://www.pastie.org/735605

this is the new code ive added a search function and button to test the array but thats not displaying any value either so im not sure if its my search method or my add method causing the problem.

Link to comment
Share on other sites

  • 0

phonebook search = new phonebook(forenameTxt.Text,phoneNumTxt.Text, emailAddressTxt.Text);

This is absolutely 100% wrong. You create a new phonebook, containing a single entry, which makes no sense.

What is this line of code supposed to do in your mind?

Link to comment
Share on other sites

  • 0

all i wanted to do was send the text from a text box down to the phonebook class so that i could use my search method but the constrctor takes 3 arguments so i sent them down regardless but i fear i may have miss understood the function i am using.

Link to comment
Share on other sites

  • 0

Just a quick one:

Based on your last code sample from pastie, I would recommend the following:

Remove the parameters from the phonebook constructor - you don't need them. Also remove the private variables (Forname, PhoneNumber, EmailAddress).

Change addContact to accept a Person object as a parameter, then:

public void addContact(Person p)
{
	Contact[contactCount] = p;
}

Your form should have an instance of Phone Book, you shouldn't be creating one each time you create a person in the person class. Create a Phone Book when the form loads, then each time the add button is clicked, create a person object, and pass it into the addContact method above.

Chris

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.