Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I add a new item to a List<List<string>> each item of the parent list get the same values

Apologies if the answer to this is obvious, I'm fairly new to C# and OOP. I've stepped though my code and spent quite some time on Google but I can't find the answer to my question (quite possibly because I am using the wrong search terms!).

I have the following class that creates a static List<List<string>> and has a method to add items to that list:

public static class WordList
    {
    static List<List<string>> _WordList; // Static List instance

    static WordList()
    {
        //
        // Allocate the list.
        //
        _WordList = new List<List<string>>();
    }

    public static void Record(List<string> Words)
    {
        //
        // Record this value in the list.
        //
        _WordList.Add(Words);
    }
}

Else where I create a List<string> which I pass into the Record() method to be added to _WordList. The problem is when I add items to WordList it gives every item in that list the same value. e.g.:

1st item added contains "Foo" and "bar"

2nd item added contains "Not","Foo" and "bar"

So instead of a list that looks like:

1: "Foo","bar"
2: "Not","Foo","bar"

I end up with:

1: "Not","Foo","bar"
2: "Not","Foo","bar"

I haven't used a List<string[]> instead of a List<List<string>> because the way I am getting the List<string> to add is by reading a text file line by line with a delimiter saying when I should add the List<string> and clear it so I can start again. Therefore I don't know how long an array I need to declare.

Hope this makes some kind of sense! If you need anymore of the code posting to help let me know.

Thanks, in advance.

EDIT

Here is the code for the creation of the List<string> that is passed to the Record() method. I think I see what people are saying about not creating a new instance of the List<string> but I'm not sure how to remedy this in regards to my code. I will have a think about it and post an answer if I come up with one!

public static void LoadWordList(string path)
    {
        string line;
        List<string> WordsToAdd = new List<string>();

        StreamReader file = new System.IO.StreamReader(path);
        while ((line = file.ReadLine()) != null)
        {
            if (line.Substring(0, 1) == "$")
            {
                    WordList.Record(WordsToAdd);
                    WordsToAdd.Clear();
                    WordsToAdd.Add(line.Replace("$", ""));
            }
            else
            {
                WordsToAdd.Add(line.Replace("_"," "));
            }
        }
        file.Close();
    }
like image 464
John Avatar asked Dec 21 '25 23:12

John


1 Answers

Instead of

WordList.Record(WordsToAdd);
WordsToAdd.Clear();
WordsToAdd.Add(line.Replace("$", ""));

do

WordList.Record(WordsToAdd);
WordsToAdd = new List<string>();
WordsToAdd.Add(line.Replace("$", ""));
like image 94
Jan Sverre Avatar answered Dec 24 '25 12:12

Jan Sverre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!