locked
Need help with an assigment on Classes RRS feed

  • Question

  • Hi!

    I am learning C# and I am honestly quite bad at it still. I am currently stuck on an assignment in the text book and there are no concrete examples in the book on how to solve the issue, nor are there any answers to the assigments in the back of the book. The teacher can't really help either and no one in class knows the answer, so I thought that I'd try my luck on here. The assigment is as follows:

    Write a Contact class that contain one person’s contact information. It has to at least contain the person’s first name, last name and phone number. These variables have to be private, and in order to get and set their values there has to be a property for each one of them. There also needs to be a constructor that initializes the variables.

    The closest thing to a Contact class that I've been able to write, which is based on code in the text book is as follows:

    namespace WindowsFormsApp1
    {
        class Contact
        {
            private string firstName = "";
            private string lastName = "";
            private int phoneNumber = 0;
    
            public Contact (string firstName, string lastName, int phoneNumber)
            {
                this.firstName = firstName;
                this.lastName = lastName;
                this.phoneNumber = phoneNumber;
            }
    
            public string FirstName
            {
                get { return firstName; }
                set { firstName = value; }
            }
    
            public string LastName
            {
                get { return lastName; }
                set { lastName = value; }
            }
    
            public int PhoneNumber
            {
                get { return phoneNumber; }
                set { phoneNumber = value; }
            }
    
            public override string ToString()
            {
             
                return firstName + " " + lastName + " " + phoneNumber;
             
            }
        }
    }

    But from here I am at a complete loss. I am not too sure on how to enter the values or how to display them when I try to compile the code. Sorry if this is asking a lot, but I am really confused about the whole assignment.

    Thanks in advance!

    Monday, April 1, 2019 7:21 PM

Answers

  • Hello,

    Your Contact class although functional could use improvement but is just fine for learning purposes.

    Now I will take you so far as giving you the answer will not have you learn anything.

    You need three text box controls, see names in code below and a button.

    Before a contact can be added minimal validation needs to happen, is there a first and last name enter? Is the phone number an integer (we can go real deep into phone number but I'm keeping it simple).

    The reason for the contact variable as private is I assume at a later point you want to peek at it. When doing so you want to make sure it's not null e.g. if(_contact != null) ...

    here is the code and as mentioned before I'm not giving you everything else you will not learn.

    namespace WindowsFormsApp1
    {
        public partial class Form2 : Form
        {
            private Contact _contact;
            public Form2()
            {
                InitializeComponent();
            }
    
            private void CreateContactButton_Click(object sender, EventArgs e)
            {
    
                if (!string.IsNullOrWhiteSpace(firstnameTextBox.Text) && !string.IsNullOrWhiteSpace(lastNameTextBox.Text) && int.TryParse(phoneTextBox.Text, out var phone))
                {
                    _contact = new Contact(firstnameTextBox.Text,lastNameTextBox.Text, phone);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 1, 2019 9:15 PM