Hello daniweb!
Yes, I'm another amateur coder who needs a little assistance getting some code to work, I hope it's alright :?:

A little background is necessary I suppose, so the deal is I'm a self taught noob who's working on a pet project of crafting a little program that will maintain a spreadsheet in the simplest way possible. This is entirely unnecessary but I felt like it would be fun to make, so here I am. Right now I'm working with a plain text file, but I plan on eventually learning how to store this somewhere on the web and eventually writing a port to android (I would have just written it in java anyway, but I have no experience with Swing or the AWT and the VS designer is too good to pass up).

Anyway, here's the code I'm having trouble with:

public List<List<string>> tss = new List<List<string>>();
public List<string> tsselement = new List<string>();
public List<string> names = new List<string>();
public string thunderboxtext;
public thunderbox()
        {
            try
            {
                TextReader tr = new StreamReader("thunderboxdata.txt");
            }
            catch (FileNotFoundException)
            {
                TextWriter tw = new StreamWriter("thunderboxdata.txt");
                tw.Close();
            }
            TextReader treader = new StreamReader("thunderboxdata.txt");
            string line = treader.ReadLine();
            tsselement = initializenames(line);
            names = initializenames(line);
            if (tsselement == null)
            {
                thunderboxtext = "No names found: list unitialized.";
                return;
            }
            tss.Add(tsselement);
            List<string> temp = new List<string>();
            while (true)
            {
                temp.Clear();
                line = treader.ReadLine();
                
                if (line == null)
                    break;

                for (int j = 0; j < line.Length; )
                {
                    if (line[j] != ' ')
                    {
                        temp.Add(line.Substring(j, line.IndexOf(' ', j) - j));
                        j = line.IndexOf(' ', j);
                    }
                    else j++;
                }
                tss.Add(temp);
            } /* This is where it messes with me T.T*/
            thunderboxtext = refresh_thunderbox();
        }

Basically I'm using a text file as a ghetto spreadsheet because it seemed simpler than figuring out how to get a spreadsheet working, and this is represented in a 2d list tss. The problem is, tss seems to be adding values normally right up until the while loop is over. I've tried some debugging and break points, and it adds the temp list into tss fine until the iterations are over, but before the line

thunderboxtext = refresh_thunderbox();

And I have no idea why this happens. If I have a sample text file

null Joe Jim John Jared Jack Joel Jay Jackie J 
Joe - 0 0 0 0 0 0 0 0 
Jim 0 - 0 0 0 0 0 0 0 
John 0 0 - 0 0 0 0 0 0 
Jared 0 0 0 - 0 0 0 0 0 
Jack 0 0 0 0 - 0 0 0 0 
Joel 0 0 0 0 0 - 0 0 0 
Jay 0 0 0 0 0 0 - 0 0 
Jackie 0 0 0 0 0 0 0 - 0 
J 0 0 0 0 0 0 0 0 -

The tss list will take every line without missing a beat, but again when it ends tss just has 9 empty lists stored in it, but it remembers the first line (I can't imagine it's a scope problem?). Anyways, if any of you could help me out I would be greatly appreciative, and I hope everyone has a good day regardless! If there's anything else I need to add, please let me know!

EDIT: If you see anything that can be improved in the code (I'm sure that's just about everything), I am certainly willing to learn, so have no fear of crushing my dreams!

Move line 26. List<string> temp = new List<string>(); to line 29. and drop temp.Clear(); HTH

commented: Fixed my code! +0

Move line 26. List<string> temp = new List<string>(); to line 29. and drop temp.Clear(); HTH

It just worked! Thanks very much for the reply, super karma to you :D
Do you know why that works? How was clearing temp cleaning out tss?

In your original code you were creating one list (temp) and adding multiple references to it (tss.Add(temp)). These are all the same list as you don't create a new one and Add just adds a reference, it doesn't copy the list. Since your first line in the loop cleared the list, all the references in tss (which were all the same) would appear cleared.

By moving the new List<string>() statement inside the loop you are creating new lists each time.

In your original code you were creating one list (temp) and adding multiple references to it (tss.Add(temp)). These are all the same list as you don't create a new one and Add just adds a reference, it doesn't copy the list. Since your first line in the loop cleared the list, all the references in tss (which were all the same) would appear cleared.

By moving the new List<string>() statement inside the loop you are creating new lists each time.

Thanks for the explanation!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.