none
how to save new arrays in lists RRS feed

  • Question

  • I have a task to create arrays within a List: List<string[]> name = new List<string[]>();

                                                                          string[] info = new string[5];

    There is no problem in adding information at the onset of the program, but when i do name.Add(info) and put in new information, it doesn't retain the first information i put in, but rather show the new information i put in TWICE.  How do i add new information but still kept the previous input i did?

    Sunday, December 22, 2019 4:28 AM

Answers

  • using System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace prov_2
    {
        class Program
        {
                static void Main(string[] args)
            {
                DateTime datum = DateTime.UtcNow;
                Console.WriteLine(datum.ToLongDateString());
                string number = ""; 
                string title = "";
                string text = "";
                //sring datum;
    
                List<string[]> logBok = new List<string[]>();
                string[] post = new string[] { number, title, text };
                Console.ReadLine();
                       
                Console.WriteLine("\tWelcome to the logbook program\n");
    
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]print all the logg-ins\t");
                    Console.WriteLine("\t[2]Write new entry");
                    Console.WriteLine("\t[3]Sort all the log-ins");
                    Console.WriteLine("\t[4]Search a log entry");
                    Console.WriteLine("\t[5]end");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                         
                            post[0] = (number);
                            post[1] = (title);
                            post[2] = (text);
                            //post[3] = (DateTime datum = DateTime.UtcNow;
                            Console.WriteLine(datum.ToLongDateString());
                            int max = loggBok.Count;
                           
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " " 
                                        + "Inlägg nummer: " + post[0] 
                                        + "\n\tTitel: " + post[1]
                                        + "\n\tText: " + post[2]);
                        
                                }
                            }
    
                            else
                                Console.WriteLine("\tLogbook is empty");
                            Console.ReadLine();
                            break;
                        case 2:      
                            loggBok.Add(post);
                            post = new string[3];
                           // DateTime datum = DateTime.UtcNow;
                            //Console.WriteLine(datum.ToLongDateString());
                            //Array.Copy(post);
                            Console.Write("\n\tWrite the entry number: ");
                            Convert.ToInt32(Console.ReadLine());
                            Console.Write("\n\tWrite the title: ");
                            titel = Console.ReadLine(); 
                            Console.Write("\n\tWrite in text: ");
                            text = Console.ReadLine();
                            break;
                        case 3:
    
                            break; 
                                  
                        case 4:
                            Console.Write("Search a titel: ");
                            string sökTitel = Console.ReadLine();
    
    
                            break;
                        case 5:
                            Console.WriteLine("Thank you for usignthe program");
                            kör = false;
                            break;
                        default:
                            break;
                    }              
                }
            }
        }
    }


    You need to rethink your logic completely. The code for a menu selection
    should do everything related to that task. You appear to have parts of a 
    task split across more than one menu selection code.

    (1) Why is the *first* line of code in menu item 2 adding an array to the list?
        It seems to be trying to "catch up" to prior actions. Menu item 2 code
        should create an array AND THEN add that array to the list.
        
    (2) The first lines of menu item 1 are altering the array. But that menu item
        should only be displaying the contents of the List.

    The way you have written the code for the menu items it appears that you
    expect the menu choices to always be done in a specific sequence.
            
    - Wayne

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 9:47 AM
    Sunday, December 22, 2019 9:09 AM
  •             bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]print all the logg-ins\t");
                    Console.WriteLine("\t[2]Write new entry");
                    Console.WriteLine("\t[3]Sort all the log-ins");
                    Console.WriteLine("\t[4]Search a log entry");
                    Console.WriteLine("\t[5]end");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                         
                            post[0] = (number);
                            post[1] = (title);
                            post[2] = (text);
                            //post[3] = (DateTime datum = DateTime.UtcNow;
                            Console.WriteLine(datum.ToLongDateString());
                            int max = loggBok.Count;
                           
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " " 
                                        + "Inlägg nummer: " + post[0] 
                                        + "\n\tTitel: " + post[1]
                                        + "\n\tText: " + post[2]);
                        
                                }
                            }
    

    Menu choice 1 should be displaying all of the items in the List, But it isn't.
    On every iteration of the loop you are displaying the same contents from the
    array "post".

    - Wayne

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 9:47 AM
    Sunday, December 22, 2019 9:16 AM
  • Hi MorenainHanoi,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 9:47 AM
    Monday, December 23, 2019 1:18 AM

All replies

  • I have a task to create arrays within a List: List<string[]> name = new List<string[]>();

                                                                          string[] info = new string[5];

    There is no problem in adding information at the onset of the program, but when i do name.Add(info) and put in new information, it doesn't retain the first information i put in, but rather show the new information i put in TWICE.  How do i add new information but still kept the previous input i did?

    You need to show more of the *actual* code that you are using. In what you
    have posted so far there is no indication of how you are adding to the List
    after the first time.

    Also describe or show how you are checking the contents of the List.

    Use Windows Copy and Paste to post the *actual* code from the VS IDE, and when 
    posting it here use the "Insert code block" feature of the forums editor. It
    should be the second button from the right on the editor menu/button bar.

    It sounds like you are doing something like this:

    string[] info = new string[5];
    info[0] = "Line 1";
    name.Add(info);
    info[0] = "Line 1A";
    name.Add(info);
    

    When you should be doing something like this:

    string[] info = new string[5];
    info[0] = "Line 1";
    name.Add(info);
    info = new string[5];
    info[0] = "Line 1A";
    name.Add(info);
    

    - Wayne

    Sunday, December 22, 2019 6:28 AM
  • using System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace prov_2
    {
        class Program
        {
                static void Main(string[] args)
            {
                DateTime datum = DateTime.UtcNow;
                Console.WriteLine(datum.ToLongDateString());
                string number = ""; 
                string title = "";
                string text = "";
                //sring datum;
    
                List<string[]> logBok = new List<string[]>();
                string[] post = new string[] { number, title, text };
                Console.ReadLine();
                       
                Console.WriteLine("\tWelcome to the logbook program\n");
    
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]print all the logg-ins\t");
                    Console.WriteLine("\t[2]Write new entry");
                    Console.WriteLine("\t[3]Sort all the log-ins");
                    Console.WriteLine("\t[4]Search a log entry");
                    Console.WriteLine("\t[5]end");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                         
                            post[0] = (number);
                            post[1] = (title);
                            post[2] = (text);
                            //post[3] = (DateTime datum = DateTime.UtcNow;
                            Console.WriteLine(datum.ToLongDateString());
                            int max = loggBok.Count;
                           
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " " 
                                        + "Inlägg nummer: " + post[0] 
                                        + "\n\tTitel: " + post[1]
                                        + "\n\tText: " + post[2]);
                        
                                }
                            }
    
                            else
                                Console.WriteLine("\tLogbook is empty");
                            Console.ReadLine();
                            break;
                        case 2:      
                            loggBok.Add(post);
                            post = new string[3];
                           // DateTime datum = DateTime.UtcNow;
                            //Console.WriteLine(datum.ToLongDateString());
                            //Array.Copy(post);
                            Console.Write("\n\tWrite the entry number: ");
                            Convert.ToInt32(Console.ReadLine());
                            Console.Write("\n\tWrite the title: ");
                            titel = Console.ReadLine(); 
                            Console.Write("\n\tWrite in text: ");
                            text = Console.ReadLine();
                            break;
                        case 3:
    
                            break; 
                                  
                        case 4:
                            Console.Write("Search a titel: ");
                            string sökTitel = Console.ReadLine();
    
    
                            break;
                        case 5:
                            Console.WriteLine("Thank you for usignthe program");
                            kör = false;
                            break;
                        default:
                            break;
                    }              
                }
            }
        }
    }

    Hey Wayne,

    Thanks for the reply, this is the program i'm working with and it's still a work in progress.  I tried doing what you suggested but it still shows the same result.  Hope you can help me with this.

    Morena



    Sunday, December 22, 2019 7:33 AM
  • Are we supposed to guess what input you used?

    What was the sequence of events? What was typed in?

    - Wayne

    Sunday, December 22, 2019 7:51 AM
  • The input is a string...the user should be able to put in new information so it can be whatever..
    Sunday, December 22, 2019 7:56 AM
  •                 Console.WriteLine("\n\t[1]Skriv ut alla loggar\t");
                    Console.WriteLine("\t[2]Skriv nytt inlägg i loggboken");
                    Console.WriteLine("\t[3]Sortera alla loggar");
                    Console.WriteLine("\t[4]Sök inlägg i loggboken");
                    Console.WriteLine("\t[5]Avsluta programmet");
                    Console.Write("\tVälj: ");
                    int val;
    

    Note that these are English language forums, so prompts, etc. in other 
    languages will usually be meaningless gibberish to most of us. Don't expect
    us to translate so we can understand what the prompts are saying.

    - Wayne

    Sunday, December 22, 2019 7:57 AM
  • i apologize, i edited them to english

    Sunday, December 22, 2019 8:13 AM
  • The input is a string...the user should be able to put in new information so it can be whatever..

    That doesn't show us *exactly what* sequence of input you are using in your
    tests. We shouldn't have to guess at what values to enter to reproduce the
    results you are seeing.

    You need to understand *why* you see the same value more than once after you 
    make a change to one. Arrays are reference types derived from the abstract 
    base type Array, not value types. So when you create an instance of an array
    the variable contains a reference to the actual array.

    Reference types (C# Reference)
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types

    - Wayne

    Sunday, December 22, 2019 8:23 AM
  •  i edited them to english

    The changes you made by editing your prior post introduced multiple errors
    into the code. Correct them so that the example will compile without errors.

    - Wayne

    Sunday, December 22, 2019 8:28 AM
  • using System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace prov_2
    {
        class Program
        {
                static void Main(string[] args)
            {
                DateTime datum = DateTime.UtcNow;
                Console.WriteLine(datum.ToLongDateString());
                string number = ""; 
                string title = "";
                string text = "";
                //sring datum;
    
                List<string[]> logBok = new List<string[]>();
                string[] post = new string[] { number, title, text };
                Console.ReadLine();
                       
                Console.WriteLine("\tWelcome to the logbook program\n");
    
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]print all the logg-ins\t");
                    Console.WriteLine("\t[2]Write new entry");
                    Console.WriteLine("\t[3]Sort all the log-ins");
                    Console.WriteLine("\t[4]Search a log entry");
                    Console.WriteLine("\t[5]end");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                         
                            post[0] = (number);
                            post[1] = (title);
                            post[2] = (text);
                            //post[3] = (DateTime datum = DateTime.UtcNow;
                            Console.WriteLine(datum.ToLongDateString());
                            int max = loggBok.Count;
                           
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " " 
                                        + "Inlägg nummer: " + post[0] 
                                        + "\n\tTitel: " + post[1]
                                        + "\n\tText: " + post[2]);
                        
                                }
                            }
    
                            else
                                Console.WriteLine("\tLogbook is empty");
                            Console.ReadLine();
                            break;
                        case 2:      
                            loggBok.Add(post);
                            post = new string[3];
                           // DateTime datum = DateTime.UtcNow;
                            //Console.WriteLine(datum.ToLongDateString());
                            //Array.Copy(post);
                            Console.Write("\n\tWrite the entry number: ");
                            Convert.ToInt32(Console.ReadLine());
                            Console.Write("\n\tWrite the title: ");
                            titel = Console.ReadLine(); 
                            Console.Write("\n\tWrite in text: ");
                            text = Console.ReadLine();
                            break;
                        case 3:
    
                            break; 
                                  
                        case 4:
                            Console.Write("Search a titel: ");
                            string sökTitel = Console.ReadLine();
    
    
                            break;
                        case 5:
                            Console.WriteLine("Thank you for usignthe program");
                            kör = false;
                            break;
                        default:
                            break;
                    }              
                }
            }
        }
    }


    You need to rethink your logic completely. The code for a menu selection
    should do everything related to that task. You appear to have parts of a 
    task split across more than one menu selection code.

    (1) Why is the *first* line of code in menu item 2 adding an array to the list?
        It seems to be trying to "catch up" to prior actions. Menu item 2 code
        should create an array AND THEN add that array to the list.
        
    (2) The first lines of menu item 1 are altering the array. But that menu item
        should only be displaying the contents of the List.

    The way you have written the code for the menu items it appears that you
    expect the menu choices to always be done in a specific sequence.
            
    - Wayne

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 9:47 AM
    Sunday, December 22, 2019 9:09 AM
  •             bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]print all the logg-ins\t");
                    Console.WriteLine("\t[2]Write new entry");
                    Console.WriteLine("\t[3]Sort all the log-ins");
                    Console.WriteLine("\t[4]Search a log entry");
                    Console.WriteLine("\t[5]end");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                         
                            post[0] = (number);
                            post[1] = (title);
                            post[2] = (text);
                            //post[3] = (DateTime datum = DateTime.UtcNow;
                            Console.WriteLine(datum.ToLongDateString());
                            int max = loggBok.Count;
                           
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " " 
                                        + "Inlägg nummer: " + post[0] 
                                        + "\n\tTitel: " + post[1]
                                        + "\n\tText: " + post[2]);
                        
                                }
                            }
    

    Menu choice 1 should be displaying all of the items in the List, But it isn't.
    On every iteration of the loop you are displaying the same contents from the
    array "post".

    - Wayne

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 9:47 AM
    Sunday, December 22, 2019 9:16 AM
  • sing System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace prov_2
    {
        class Program
        {
                static void Main(string[] args)
            {
                DateTime datum = DateTime.UtcNow;
                Console.WriteLine(datum.ToLongDateString());
                string number = ""; 
                string title = "";
                string text = "";
                //sring datum;
    
                List<string[]> logBook = new List<string[]>();
                string[] post = new string[] { number, title, text };
                Console.ReadLine();
                       
                Console.WriteLine("\tWelcome to the Logbook\n");
    
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]Print all the log-ins\t");
                    Console.WriteLine("\t[2]Write a new entry");
                    Console.WriteLine("\t[3]Sort all the log-ins");
                    Console.WriteLine("\t[4]Search an entry");
                    Console.WriteLine("\t[5]End the program");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                         
                            post[0] = (number);
                            post[1] = (title);
                            post[2] = (text);
                            //post[3] = (DateTime datum = DateTime.UtcNow;
                            Console.WriteLine(datum.ToLongDateString());
                            int max = logBook.Count;
                            //int sum = 0;
    
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " " 
                                        + "Entry no: " + post[0] 
                                        + "\n\tTitle: " + post[1]
                                        + "\n\tText: " + post[2]);
                                }
                            }
    
                            else
                                Console.WriteLine("\tThe logbook is empty");
                            Console.ReadLine();
                            break;
                        case 2:      
                            logBook.Add(post);
                            post = new string[3];
                            Console.Write("\n\tSkriv in inlägg nummer: ");
                            Convert.ToInt32(Console.ReadLine());
                            Console.Write("\n\tSkriv in titel: ");
                            title = Console.ReadLine(); 
                            Console.Write("\n\tSkriv in text: ");
                            text = Console.ReadLine();
                           
                            break;
                        case 3:
                            //unfinished but there shouldnt be any problem running
                            break; 
                                  
                        case 4:
                            Console.Write("Search an entry: ");
                            string sökTitel = Console.ReadLine();
    
    
                            break;
                        case 5:
                            Console.WriteLine("Thank you for using the program");
                            kör = false;
                            break;
                        default:
                            break;
                    }              
                }
            }
        }
    }
    

    Sunday, December 22, 2019 9:26 AM
  • This should run okay.  My most concern is adding new entries and keeping the previous at the same time when I print all entries.  Thank you so much for helping

    -Morena

    Sunday, December 22, 2019 9:27 AM
  • This should run okay.  

    As I suggested before, you need to rethink how and where you are doing tasks.
    In menu item 2 you accept input for a new array. But after accepting that input
    it isn't immediately added to the list. It won't get added at all if menu item
    2 is never used again.

    As to my earlier remarks about the menu item 1 display, instead of this:

    Console.WriteLine("\t" + i + " "
        + "Entry no: " + post[0]
        + "\n\tTitle: " + post[1]
        + "\n\tText: " + post[2]);
    

    you need this:

    Console.WriteLine("\t" + i + " "
        + "Entry no: " + logBook[i][0]
        + "\n\tTitle: " + logBook[i][1]
        + "\n\tText: " + logBook[i][2]);
    

    - Wayne

    Sunday, December 22, 2019 9:54 AM
  • Thank you for this.

    This really fixes the saving part.

    Sunday, December 22, 2019 10:24 AM
  • Hi MorenainHanoi,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 9:47 AM
    Monday, December 23, 2019 1:18 AM