darko
darko

Reputation: 793

Converting to array

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); // <---- :(
    }
}

Upvotes: 1

Views: 646

Answers (3)

Ken Kin
Ken Kin

Reputation: 4683

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

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66389

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.

Upvotes: 3

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

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

Upvotes: 3

Related Questions