I need help, and fast :D project running out of time

https://gist.github.com/kazdul/5816bdc919ef1062cf0bae07668d2571

Most of the commented code is in Swedish. But the description of the program is in ENG. Please i have come this far and im so freaking stuck.
Im stuck on the search and add part. Also on the time/date display, it wont show in Case 2…

This is C# and why are you stuck ?
Collections have add method you just didnt create proper classes to hold your data
What you want me to do ?

So if you need Log then create class Logger or smth with List, do not use ArrayList it is obsolete and it holds objects not types. Then make method to control menu entry.
And for adding to Logger make Logger.Add(your_class) method which is fairly simple. For search loop through colllection in Logger and compare properties. This seems like a test or homework :slight_smile:

Hey Mirko, I did manage to get this done myself using the array string as the savings for the list. THank you anyway for helping me and showing interest :):
else
{
//lägger till titel + att den vill att du fortsätter skriver en text till loggboken
Console.WriteLine("\n\tDu har valt titeln: " + logg[0]);
Console.Write("\n\tDu kan nu börja skriva nedan i din loggbok.");
Console.Write("\n\t");
logg[1] = Console.ReadLine();
logg[2] = DateTime.Now.ToLongTimeString(); // lagt in datum för sparande av enskilda loggbok
Console.WriteLine("\n\tDu har lagt till en text den; " + logg[2]); // Logg2 är datum som sparas i List som en Array
addLoggbok.Add(logg); // sparar innehållet

       case 3: // sökning i loggboken, valt att använda mig utav sökning av titel och om ordet finns eller också om innnehållet enbart innehåler en bokstav
                    Console.Write("\n\tSök i loggboken efter den titel du letar efter: ");
                    string search = Console.ReadLine();
                    foreach(string[] titel in addLoggbok)
                    {
                        if(titel[0].Contains(search) || titel[1].Contains(search)) // sökningens ekvation, om inmatningen är sann så skriv ut inehållet.
                        {
                            Console.Write("\n\tTitel: " + titel[0] + "\n\t" + titel[1] + "\n\tSkrivet den: " + titel[2]);
                            break;

@kazdul - I’m sure this is a little late for you, but I just wanted to point out a couple of things that I noticed. I’m not sure how much experience you have so if you have any questions, just let me know and I’ll try to answer them.

It appears that your logbook contains a list of arrays. This may be good in the short term, but it will make your code difficult to read and understand when someone else, possibly future you, comes back in to maintain it. A better option would be to create a class or struct that can hold the information in a more meaningful way, like this:

public class LogbookEntry{
    public string FirstName { get; set;}
    public string LastName { get; set;}
    public DateTime TimeWritten { get; set;}
}

The other thing I noticed is you’re using string.Contains(). The problem is that method written that way is case sensitive so if someone searches for “Name” and “name”, they will get different results. This may or may not be what you want, but could be a potential issue.
To make it case insensitive, you could do something like this:

var logbookEntries = new List<LogbookEntry>();
var entry = new LogbookEntry();
entry.FirstName = "John";
entry.LastName = "Doe";
entry.TimeWritten = DateTime.Now;
logbookEntries.Add(entry);

foreach(var logEntry in logbookEntries)
{
    var foundFirstName = logEntry.FirstName.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) >= 0;
    var foundLastName = logEntry.LastName.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) >= 0;

    if (foundFirstName || foundLastName)
    {
        Console.Write("\n\tName: " + logEntry.FirstName + "\n\t" + logEntry.LastName + "\n\tTime Written: " + logEntry.TimeWritten);
    }
}
1 Like

@kblock-dev made some good points.

I would like to add the following.

On Line 3, using the following syntax to initialize a List List<string> addLoggbok = new List<string>();. List Class (MSDN)

On Line 25, you have while (true). The condition will always be true, so creating an indefinite loop. Use a variable to control code execution.

@jamesperrin - Good catch. I hadn’t opened up the full file to inspect the other code.

@kazdul - Adding onto what @jamesperrin noticed with the while (true) loop, I’d initialize your titel variable prior to the while loop and change the loop to a do…while loop to check that variable for “q” like this:

var titel = "";

do {
 // Do this...
} while (titel != "q")

The ensures that it will run at least once and instead of using an if…else, you can just use a single if statement to check the value of titel. If the user enters “q”, then you can skip the logic to search and exit gracefully at the end of the loop without having to use break statements making it more understandable as to what condition is evaluated for the loop.

I solved itusing this method. And also thanks for the help guys :slight_smile:

case 3: // sökning i loggboken, valt att använda mig utav sökning av titel och om ordet finns eller också om innnehållet enbart innehåler en bokstav
                    Console.Write("\n\tSök i loggboken efter den titel du letar efter: ");
                    string search = Console.ReadLine();
                    foreach(string[] titel in addLoggbok)
                    {
                        if(titel[0].Contains(search) || titel[1].Contains(search)) // sökningens ekvation, om inmatningen är sann så skriv ut inehållet.
                        {
                            Console.Write("\n\tTitel: " + titel[0] + "\n\t" + titel[1] + "\n\tSkrivet den: " + titel[2]);
                            break;
                        }
                    }
                    break;

@kazdul - kblock, that i was talking about. C# is object oriented language so make use of classes to well structure your application. Kazdul, this is not the way you code in any object oriented language, so apply principles kblock stated or move to for example C :wink:
if you are student or learning C# professor will return you work for correction, so learn OOP you will need it if you want to write applications in any popular language.