gencay
gencay

Reputation: 609

C# adding list into list

I have a DocumentList.c as implemented below. And when I try to add a list into the instance of DocumentList object it adds but the others be the same

 class DocumentList
    {
        public static List wordList;
        public static string type;
        public static string path;
        public static double cos;
        public static double dice;
        public static double jaccard;
        //public static string title;

        public DocumentList(List wordListt, string typee, string pathh, double sm11, double sm22, double sm33)
        {
            type = typee;
            wordList = wordListt;
            path = pathh;
            cos = sm11;
            dice = sm22;
            jaccard = sm33;
        }
    }

in main c#code fragment

public partial class Window1 : System.Windows.Window
    {

        static private List documentList = new List();

...

in a method I use as below.

DocumentList dt = new DocumentList(para1, para2, para3, para4, para5, para6);
                documentList.Add(dt);

Now, When i add the first list it is ok it seems 1 item in documentList, but for the second one I get a list with 2 items but both the same..

I mean I cannot keep previous list item..

Upvotes: 0

Views: 540

Answers (3)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104811

Use a generic List<T> instead, it will be easier to read and understand, and will cost much less performance (Read this for more on generics):

class DocumentList
{
    //via a (non-static - read link below) field:
    public List<string> wordList;

    //via a property (read link below):
    public List<string> WordList { get; set; }
    //----------
    public DocumentList(List<string> wordList, .....
    {
        //Sets property
        this.WordList = wordList;

        //Sets field
        this.wordList = wordList;
        //etc.
    }
}


private partial class Window1
{
    private List<DocumentList> documentList = new List<DocumentList>();
}

The this keyword refers to current instance (i.e. members declared in the DocumentList class).

What I don't understand is, why would you want to declare all your members as static, and if it's immutable, than you should declare it as readonly as well, once you mark your fields as static, their shared between all the instances, and therefore the values of the varioues items in the list actually refer to the same value as it's shared, and once you update a member, it updates the "shared" field and you see the same value in all the fields. I would highly recommend you to read this.

Another good idea would be wrapping your fields with properties.

Upvotes: 4

Mark Byers
Mark Byers

Reputation: 839044

The problem is that you have declared all your class members as static. This means that all your instances will share the same values and modifications to one object will affect all the others too. Remove the static keyword so that each instance has its own data.

class DocumentList
{
    public List wordList;
    // etc...

Also as a matter of style you should not have public fields. Either make them private or turn them into properties. You may also want to consider making only the getter public.

Upvotes: 2

Brook
Brook

Reputation: 6019

The reason you are overwriting the values in the first instance is that you have marked all of your fields as static. Static fields, properties, and methods are shared among all instances of the class.

Beyond that, use List<string> and save yourself the headache of having to cast back and forth.

Upvotes: 3

Related Questions