Mantas
Mantas

Reputation: 191

How to add more items to my list

I have created list. And it compiles and shows only one item. I need to make it show all my inserted items. For example I try insert one more. How should i do that?

    namespace ConsoleApplication5
{
    public class DocConfig
    {
        public string Description { get; set; }
        public List<DocPart> Parts;

        public class DocPart
        {
            public string Title { get; set; }
            public string TexLine { get; set; }

            public class Program
            {
                public static int Main()
                {

                    List<DocPart> Parts = new List<DocPart>();
                    var doc = new DocConfig();
                    doc.Description = "bla bla";
                    doc.Parts = new List<DocPart>();
                    doc.Parts.Add(new DocPart { Title = "aaa", TexLine = @"\include{aaa.tex}" });
                    doc.Parts.Add(new DocPart { Title = "bbb ", TexLine = @"\include{bbb.tex}" });

                    foreach (DocPart part in doc.Parts)
                    {
                        Console.WriteLine(part.Title);
                        Console.ReadLine();
                        Console.ReadKey();
                        {
                            return 0;
                        }
                    }
                    return -1;
                }

            }
        }
    }

Upvotes: 0

Views: 85

Answers (1)

Sayse
Sayse

Reputation: 43300

The reason it is only showing one is because your loop over parts is returning from the method and therefore it never gets chance to complete any other iterations.

The solution is to not return early (Remove the return 0;) from this method and instead let it run to the end.

Some other changes I made below were:

  • Removed the read keys from the for loop. You probably don't want the user to have to press a key every iteration either.
  • Un-nested classes. Your nested classes also seemed wierd to me and will just lead you to messy code.
namespace ConsoleApplication5
{
    public class DocConfig
    {
        public string Description { get; set; }
        public List<DocPart> Parts;
    }

    public class DocPart
    {
        public string Title { get; set; }
        public string TexLine { get; set; }
    }

    public class Program
    {
        public static int Main()
        {

            List<DocPart> Parts = new List<DocPart>();
            var doc = new DocConfig();
            doc.Description = "bla bla";
            doc.Parts = new List<DocPart>();
            doc.Parts.Add(new DocPart { Title = "aaa", TexLine = @"\include{aaa.tex}" });
            doc.Parts.Add(new DocPart { Title = "bbb ", TexLine = @"\include{bbb.tex}" });

            foreach (DocPart part in doc.Parts)
            {
                Console.WriteLine(part.Title);
            }
            Console.ReadKey();
            return -1;
        }

    }
}

Upvotes: 1

Related Questions