I am making a simple c# web service that takes in a name and returns the corresponding phone number.
So I created a contact class with a name field and a number field
Here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class Contact
{
String name;
int number;
//constructor to make new contact
public Contact(String name, int number)
{
this.name = name;
this.number = number;
}
}
}
And here is my web service class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class PhoneBook : IPhoneBook
{
Contact[] contactList = new Contact[4];
//constructor
public PhoneBook ()
{
contactList[0] = new Contact("Mary Jones", 1800252525);
contactList[1] = new Contact("Bob Smith", 1800343434);
contactList[2] = new Contact("Martin Dunne", 1800797979);
contactList[3] = new Contact("Sarah Mitchel", 1800898989);
}
//method to look up name
public string lookUpNumberByName(string name)
{
//variable to hold name entered
String nameEntered = name; ;
/**
code to compare the name entered with the
the names of the contact objects.
If the name is a match with a contact name then return
the associated number
**/
//return number corresponding to the name
return null;
}
}
}
So I am trying to make 4 simple contact objects stored in an array then when I enter a name I can check if it matches one of them and return the relevant phone number.
The problem is with the constructor
My assignment says- "Have the service maintain a list of names and phone numbers in a collection in memory which is initialised in the constructor for the service class."
So I don't know what parts I should have in the constructor and what should be outside in the main class.Hopefully someone can let me know the best way I can go ahead with this. Thanks in advance
UPDATE
I have redone my code and made it a bit more simple by removing the contact class. Here it is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class PhoneBook : IPhoneBook
{
static String[] nameList = new String[4];
static String[] numberList = new String[4];
//constructor
public PhoneBook()
{
nameList[0] = ("Mary Jones");
nameList[1] = ("Bob Smith");
nameList[2] = ("Martin Dunne");
nameList[3] = ("Martin Dunne");
numberList[0] = ("1800252525");
numberList[1] = ("1800343434");
numberList[2] = ("1800797979");
numberList[3] = ("1800898989");
}
//method to look up name
public String lookUpNumberByName(String name)
{
try
{
if (string.Equals(name, nameList[0], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[0];
}
if (string.Equals(name, nameList[1], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[1];
}
if (string.Equals(name, nameList[2], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[2];
}
if (string.Equals(name, nameList[3], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[3];
}
}
catch (System.Exception)
{
return ("That name is not found");
}
}//end of lookUpNumberByName method
}//end of class phonebook
}// end of phoneservice
My lookUpNumberByName method is getting an error saying not all code paths return a value
-
1you just need to create a new static list property in your service and initialize in the service constructor this will mantenin the list in memory which you can accomplish by using the static accessor. @blG_aLOvermachine– Overmachine2014年03月17日 01:34:58 +00:00Commented Mar 17, 2014 at 1:34
-
Thanks the reply what do you mean by a list?bIG_aL– bIG_aL2014年03月17日 01:47:08 +00:00Commented Mar 17, 2014 at 1:47
2 Answers 2
You're basically asking about encapsulation. Generally, you want to think of what parts of the class you want to expose kind of like an engineer thinks about what controls of a car to expose to a driver. Basically, if no one on the outside will need to use this property of this method, don't expose it, just leave it be.
What your assignment is effectively asking you to do is not to expose a property but to set a contact list via a service constructor. So if you need to set your contact list, shouldn't you just pass a contact list into the constructor for your phone book? And remember that you can have multiple constructors and if you don't specify one that takes no arguments, you won't be able to simply declare an instance of that class without passing arguments.
You mean something like this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class PhoneBook : IPhoneBook
{
private static List<Contact> _contactList = new List<Contact>();
//constructor
public PhoneBook (List<Contact> contactList)
{
_contactList = contactList;
}
public PhoneBooks()
{
}
}
}
you can use it like this
using System;
class Program
{
static void Main()
{
var contactList = new List<Contact>();
contactList.Add(new Contact("Mary Jones", 1800252525));
contactList.Add(new Contact("Bob Smith", 1800343434));
contactList.Add(new Contact("Martin Dunne", 1800797979));
contactList.Add(new Contact("Sarah Mitchel", 1800898989));
var phoneBook = new PhoneBook(contactList);
}
}
Comments
Explore related questions
See similar questions with these tags.