Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I'm trying to make a phonebook program. I want to add a contact by this line:

C#
Contact person = new Contact("Bob", "Smith", "0500399");


The problem is that I get an error saying
C#
'Contact' does not contain a constructor that takes 3 arguments


My 'Contact' class looks like this:
C#
class Contact
    {
        public string FristName { get; private set; }
        public string LastName  { get; private set; }
        public string PhoneNr   { get; private set; }

        public void CreateContact (string firstname, string lastname, string phonenr)
        {
            this.FristName = firstname;
            this.LastName = lastname;
            this.PhoneNr = phonenr;
        }
    }


I know that I could set these values by doing
C#
person.CreateContact("Bob", "Smith", "0500399")

This is not what I want. I want to add it the same time I make the instance.

I'm new to programming, so please bear with me. Thank you in advance :-)
Posted
Updated 21-Nov-15 2:29am
v3

To fix Bills code a bit (he didn't actually add a constructor!), your Contact class would look more like this:
public class Contact
{
    public string FirstName { get; private set; }
    public string LastName  { get; private set; }
    public string PhoneNr   { get; private set; }

    // A public constructor
    public Contact(string firstName, string lastName, string phoneNr)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.PhoneNr = phoneNr;
    }

    // CreateContact should return a new Contact object.
    // That's what Create... implies.
    // You'd use it like this:
    //     Contact myContact = Contact.CreateContact(f, l, p);
    public static Contact CreateContact(string firstName, string lastName, string phoneNr)
    {
        Contact newContact = new Contact(firstname, lastname, phonenr);
        return newContact;
    }
}
 
Share this answer
 
v2
Comments
BillWoodruff 21-Nov-15 10:34am    
+5 for covering my back :) (my answer edited)
Dave Kreskowiak 21-Nov-15 10:46am    
No problem! ;)
Member 12039355 21-Nov-15 11:39am    
Thanks! +5 :)
Krunal Rohit 21-Nov-15 11:20am    
5!
-KR
Why don't you just make the Constructor of the Class take parameters:
C#
public Contact(string firstname, string lastname, string phonenr)
{
    this.FristName = firstname;
    this.LastName = lastname;
    this.PhoneNr = phonenr;
}
Of course, if you do that, you really don't need a separate 'CreateContact method.

If you expect that some contacts will not have a phone number, you can make that parameter optional:
C#
public Contact(string firstname, string lastname, string phonenr = "")
{
    this.FristName = firstname;
    this.LastName = lastname;
    this.PhoneNr = phonenr;
}
 
Share this answer
 
v2
Comments
Krunal Rohit 21-Nov-15 11:20am    
5!
-KR
Member 12039355 21-Nov-15 11:39am    
Thanks! +5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900