Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:43:21+00:00 2026-06-18T08:43:21+00:00

I have addres book app, and want to write the final result into a

  • 0

I have addres book app, and want to write the final result into a text file. I am having hard time to convert my list to an array so that I could execute the WriteAllLines command.

Interface:

abstract class PhoneBookCore {
    protected string _group;

    public PhoneBookCore(string group) {
        this._group=group;
    }

    public abstract void Add(PhoneBookCore d);
}

class Contect: PhoneBookCore {
    private string _firstName;
    private string _lastName;
    private string _phoneNumber;
    private string _addres;

    public Contect(string group, string firstName, string lastName, string phoneNumber, string addres)
        : base(group) {
        this._firstName=firstName;
        this._addres=addres;
        this._lastName=lastName;
        this._phoneNumber=phoneNumber;
    }
}

class Group: PhoneBookCore {
    private List<PhoneBookCore> elements=new List<PhoneBookCore>();

    public List<PhoneBookCore> elementsList {
        get;
        set;
    }

    public Group(string name)
        : base(name) {

    }

    public override void Add(PhoneBookCore d) {
        elements.Add(d);
    }
}

This is where I stuck

class DataOptins {
    public string Save(Group g) {
        foreach(var item in g) {
            string[] arr=g.elementsList.ToArray();  // <---- :(

        }
        System.IO.File.WriteAllLines(Path, arr); // <---- :(
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:43:22+00:00Added an answer on June 18, 2026 at 8:43 am

    Your code is flawed in many ways, but I will focus on your specific question for now.

    First, you have to override the ToString() method in the class Contect:

    public override string ToString()
    {
        return string.Format("{0}{1}{2}{1}{3}{1}{4}", _firstName, "\t", _lastName, _phoneNumber, _addres);
    }
    

    (This is just an example, have your own format of course)

    And now have such code to make the list into array of strings:

    public string Save(Group g)
    {
        string[] lines = g.elementsList.ConvertAll(p => p.ToString()).ToArray();
        System.IO.File.WriteAllLines(Path, lines );
    }
    

    Now with this and with your current code you will get an exception since g.elementsList will always be null. Why? Because you never assign it. Having other private member is all good, but the compiler can’t know that when you call to elementsList you actually want the private member elements.

    Change the code to:

    private List<PhoneBookCore> elements = new List<PhoneBookCore>();
    public List<PhoneBookCore> elementsList { get { return new List<PhoneBookCore>(elements); } }
    

    And you won’t have “null exception” anymore. Note that I made the public property return a copy of the list so that the calling code won’t be able to change your private member.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple address book app that I want to make searchable. The
In 3.0 they have Auto-Fill for Safari. You go into the settings app to
I have a custom addressbook built into a c#.net app. My users have gone
I want to copy all iPhone address book contacts into an array and then
I have to send a text from my app to the contacts present in
I'm adding contact details to the address book from my app. And I have
I have an app very similar to address book of iphone. The entire search
I have an app that uses address book. I am trying to display sorted
I have an app that uses the Address Book. When running in iOS 6
I have the sample address book project loaded up and it is failing to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.