1

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); // <---- :(
    }
}
2
  • You can use: List.ToArray(); To convert your list to array Commented Feb 5, 2013 at 8:18
  • g.elementsList is of type public List<PhoneBookCore>. Why are you casting that to string[] . Should it not be PhoneBookCore[] ?? Commented Feb 5, 2013 at 8:19

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

it comes out with: "Object reference not set to an instance of an object." how should i resolve this? my call in the main is : DataOptins l = new DataOptins(); l.Save(groupA);
string[] lines = g.elementsList.ConvertAll(phoneBookCore => phoneBookCore.ToString()).ToArray();
Yeah, that's due to "flawed in many ways" I mentioned. I will edit my post in a short while.
@Shadow Wizard: I'm so wondering how can you make an answer in amazingly speed.
@Ken cheers, will delete mine too shortly. 8 minutes is ubber slow by the way!
|
2

Don't foreach. Just call ToArray on your list. However, before doing that, you need to select the actual string property in the elements of the group. Group.elementsList is a list of PhoneBookCore objects. You can't convert them to string - at least not the way your class currently looks like. So, either select that string property you actually want to have:

public string Save(Group g)
{
    string[] arr = g.elementsList.Select(x => x.StringProperty).ToArray();
    System.IO.File.WriteAllLines(Path,arr);
}

Or override ToString in PhoneBookCore and use that:

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

At last, you can actually drop the call to ToArray altogether, because there exists an overload of WriteAllLines that accepts an IEnumerable<string>:

public string Save(Group g)
{
    System.IO.File.WriteAllLines(Path, g.elementsList.Select(x => x.ToString()));
}

1 Comment

Error Cannot implicitly convert type 'PhoneBook.PhoneBookCore[]' to 'string[]'
1

1) If Add method is not a requirement for all derived class, rather declare it as virtual than abstract.

2) Override ToString() of your Contect class. If it's necessary, override for PhoneBookCore also. In the Contect class it would be like:

public override String ToString() {
    return
        (new[] { _firstName, _lastName, _phoneNumber, _addres }).Aggregate((a, b) => a+"\t"+b);
}

the rule to aggregate them depends on your requirement.

3) Make you Group class implement IEnumerable<String> for semantics meaningful thus you don't need to expose elementsList. You'll need to implement GetEnumerator() for this, but it's simple to:

partial class Group: PhoneBookCore, IEnumerable<String> {
    IEnumerator IEnumerable.GetEnumerator() {
        return this.GetEnumerator();
    }

    public IEnumerator<String> GetEnumerator() {
        return elements.Select(x => x.ToString()).GetEnumerator();
    }
}

When you've done all of these three things, then you can simply implement your Save method like:

public string Save(Group g) {
    string[] arr=g.ToArray(); // <---- :(
    System.IO.File.WriteAllLines(Path, arr); // <---- :(

    // notice: you did not show what to return in original code
}

4) Suggesting that correct your class name DataOptins to DataOptions, and Contect to Contact.

Okay, following would be the completion of code(except what Save returns):

abstract class PhoneBookCore {
    protected string _group;

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

    public virtual void Add(PhoneBookCore d) {
    }
}

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

    public override String ToString() {
        return
            (new[] { _firstName, _lastName, _phoneNumber, _addres }).Aggregate((a, b) => a+"\t"+b);
    }

    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, IEnumerable<String> {
    IEnumerator IEnumerable.GetEnumerator() {
        return this.GetEnumerator();
    }

    public IEnumerator<String> GetEnumerator() {
        return elements.Select(x => x.ToString()).GetEnumerator();
    }

    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);
    }
}

class DataOptins {
    public string Save(Group g) {
        string[] arr=g.ToArray(); // <---- :(
        System.IO.File.WriteAllLines(Path, arr); // <---- :(

        // notice: you did not show what to return in original code
    }
}

1 Comment

@darko: Not at all. You have an accepting answer, but just I thought that seems you need more help on this.

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.