0

I'm a student in a C# class and this is my introductory assignment to Classes, so please bear with me. When the New button is pressed, a CPerson object will be created using the name and phone values and the object will be added to a List<>.

enter image description here

class CPerson 
{ 

    private string m_sName;  
    private string m_sPhone;

    public string Name
    {
        get { return this.m_sName; }
        set 
        { 

            this.m_sName = value; 
        }
    }
    public string Phone
    {
        get { return this.m_sPhone; }
        set 
        {
            this.m_sPhone = value; 
        }
    }


}

public partial class Form1 : Form
{
    private List<CPerson> PhoneNum = new List<CPerson>(); //<CPerson> or <string>?
    public Form1()
    {
        InitializeComponent();
        newbutton.Enabled = false;
        changebutton.Enabled = false;
        savebutton.Enabled = false;
    }



    private void newbutton_Click(object sender, EventArgs e)
    {
        changebutton.Enabled = true;
        savebutton.Enabled = true;
        PhoneNum.Add(new CPerson { Name = Namebox.Text + " : ", Phone = phonebox.Text });
        listBox1.Items.Add(PhoneNum); //text = "Collection"

    }

The assignment says "The CPerson ToString() override will be used to display the name and phone number in the listbox" as shown in the above image, which I don't necessarily understand, but I'm guessing I have to use something like this?

        CPerson data = new CPerson();
        data.ToString();

Either way, as the code is now, all I get in my listbox is "(Collection)". Any help would be appreciated!

2
  • listBox1.DataSource = PhoneNum; A BindingList<T> would work better. Commented Feb 14, 2017 at 19:39
  • thanks! works good so far Commented Feb 16, 2017 at 17:13

2 Answers 2

2

That is asking to override the ToString() method. You can do it like this:

class CPerson 
{ 

private string m_sName;  
private string m_sPhone;

public string Name
{
    get { return this.m_sName; }
    set 
    { 

        this.m_sName = value; 
    }
}
public string Phone
{
    get { return this.m_sPhone; }
    set 
    {
        this.m_sPhone = value; 
    }
}

public override string ToString()
{
    return Name + ": " + Phone;
}

I did not get right the part of adding to the list, but I assume you can do the following using ToString():

listBox1.Items.Add(data.ToString());
Sign up to request clarification or add additional context in comments.

Comments

0

Close...

    class CPerson
    {
        private string m_sName;
        private string m_sPhone;

        public string Name
        {
            get { return this.m_sName; }
            set
            {

                this.m_sName = value;
            }
        }
        public string Phone
        {
            get { return this.m_sPhone; }
            set
            {
                this.m_sPhone = value;
            }
        }

        public override string ToString()
        {
            return Name + ": " + Phone;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.