Question Error when trying to make a "dynamic" list

Adde87a

Member
Joined
Jun 17, 2019
Messages
8
Programming Experience
Beginner
Hi!

Im in the a course for absolute beginners, trying to learn C# i have a little exprience from Game maker language, so im not totally stupid in this .. but i have a problem right now with some code i cant figure out what im doing wrong.

this is my code

C#:
        static void Main(string[] args)
        {
            DateTime showTime = DateTime.Now;
            bool ProgramRun = true; // kör programmet
            string input = "";

            // skapar lista och rum för innehåll TITEL och INLÄGG
           string[] post = new string[2];
           post[0] = ""; // TITEL
           post[1] = ""; // INLÄGG      

            List<string[]> postBook = new List<string[]>(); // skapar en ny lista för att spara alla inlägg
            postBook.AddRange(post);

            // setup för meny
            bool option; // boolean för TryParse
            int menuChoose = 0; // variabel för menyval

            {
                while (ProgramRun)
                {                
                    {
                        // Skriv ut meny
                    
                        Console.WriteLine("\n\tVälkommen till loggboken 2019");
                        Thread.Sleep(100);
                        Console.WriteLine("[1] -  Lägg till logg");
                        Thread.Sleep(100);
                        Console.WriteLine("[2] -  Öppna logg");
                        Thread.Sleep(100);
                        Console.WriteLine("[3] -  Sök logg");
                        Thread.Sleep(100);
                        Console.WriteLine("[4] -  Avsluta");

                        option = int.TryParse(Console.ReadLine(), out menuChoose);
                    }
                    switch (menuChoose) // menyval
                    {
                        case 1:
                            Console.Write("Lägg till i loggen");

                            for (int i = 0; i < post.Length; i++)
                            {
                                input = Console.ReadLine() + showTime;
                                post[i] = input;
                            }
                            break;
                        case 2:
                            Console.Clear();
                            Console.WriteLine("Dina inlägg i loggboken är: ");
                            foreach (var i in postBook)
                            {
                                Console.WriteLine(i);
                            }
                            Console.WriteLine("-----");
                            break;
                        case 3:
                            Console.Write("Sök Loggen");
                            break;
                        case 4:
                            Console.WriteLine("Välkommen tillbaka ");
                            Thread.Sleep(1000);
                            Console.WriteLine("Ha en bra dag! ");
                            Thread.Sleep(500);
                            ProgramRun = false;
                            break;
                        default:
                            Console.Write("Please choose ");
                            break;
                    }
                }
            }
        }

Im getting this error ... I sort if understand i does't like the string, but im not that comfortable with strings and converts yet..

Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable<string[]>' ExamProjekt C:\Users\Familjen Svensson\source\repos\ExamProjekt\ExamProjekt\Program.cs 24 Active


Thanks in advance for your time !
 
Last edited by a moderator:
I have removed unnecessary lines from your code to make it more readable but I'm afraid that I didn't take note of which line was highlighted before I did so. Could you please edit your code snippet formatting to highlight the the relevant line again? Please don't include irrelevant code and wads of blank lines in future as it makes reading code that much more difficult.
 
I think you meant to call postBook.Add() instead of postBook.AddRange().

When you declare a List<Foo>, it contains Foos. So when you call Add() on it, it expects you to pass in a single Foo. When you call AddRange(), it expects you to add in multiple Foos, so it tries to see if the parameter that is passed in can be enumerated to get many Foos.

In your code above, when you declared List<string[]>, it contains string arrays. So when you called AddRange() it was expecting to find multiple string arrays, but you only passed in a string array.

As a quick aside, storing your data that way where one array index holds one thing, while another index holds another thing is not the appropriate way to do things in an object oriented language. You would declare a class to hold the book:
C#:
class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
}
and then declare a List<Book>.
 
I have removed unnecessary lines from your code to make it more readable but I'm afraid that I didn't take note of which line was highlighted before I did so. Could you please edit your code snippet formatting to highlight the the relevant line again? Please don't include irrelevant code and wads of blank lines in future as it makes reading code that much more difficult.
It was Line 24 that gave me the error :)

Skickat från min moto g(7) play via Tapatalk
 
It was Line 24 that gave me the error :)
Yeah but, as I said, I removed some lines so line 24 in your post, which was highlighted, was not the line that was actually at fault. Anyway, the issue appears to have been correctly identified by Skydiver so I have highlighted the line that was pointed out, which is now line 13.
 
Back
Top Bottom