简体   繁体   中英

Adding new strings to string array in a class constructor

I'm doing an assignment about working with structured / semi-structured / unstructured data and I'm doing a word count of Shakespeare plays (to see how language changes over time) by importing txt files of each play and an xml index file which stores key information about each play like the year it was written, character list etc.. Then I will remove character names, settings, punctuation and common words (and, but, or, if etc...) from the txt file ready for the word count - all in a console script run in C#. I'm writing a class for which each play's data will be stored and it currently looks like this:

    class PlayImport
{
    public string Title;
    public DateTime Year;
    public string location;
    public string[] Cast;
    public Counter[] WordCount;

    public PlayImport(string location, int Num)
    {
        XmlDocument Reader = new XmlDocument();
        Reader.Load(location);
        this.Title = Convert.ToString(Reader.DocumentElement.ChildNodes[Num].Attributes["Title"].Value);
        this.Year = Convert.ToDateTime(Reader.DocumentElement.ChildNodes[Num].Attributes["Year"].Value);
        this.location = Convert.ToString(Reader.DocumentElement.ChildNodes[Num].Attributes["Location"].Value);
        foreach (XmlNode xmlNode in Reader.DocumentElement.ChildNodes[Num].ChildNodes[0].ChildNodes)
            this.Cast += Convert.ToString(xmlNode.Attributes["Name"].Value);
    }
}

However, the final line (Cast +=) gives off an error cannot convert string to string[]. How can I get around this so that the character list gets bundled together into the Cast string array?

public string[] Cast;

The line above is a declaration of an array and this array hasn't been initialized anywhere. So you cannot add anything here until you inform the compiler that you want to initialize it with the space to store a certain number of strings.

....
this.Cast += Convert.ToString(xmlNode.Attributes["Name"].Value);

This line instead tries to execute a += operation on the previous array.
This is not possible because there is no operator defined for an array that is capable to do that operation, thus you get the error

A very simple and better approach is to declare your Cast field as a List<string>

public List<string> Cast = new List<string>();

then inside the foreach you just Add a new string to the existing string collection

foreach (XmlNode xmlNode in Reader.DocumentElement.ChildNodes[Num].ChildNodes[0].ChildNodes)
   this.Cast.Add(Convert.ToString(xmlNode.Attributes["Name"].Value));

The advantage of using a List instead of an array is basically in the fact that you don't need to know in advance how many strings you want to store in the array, instead the list dinamically expand its internal storage to accomodate for new entries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM