Search code examples
c#while-loopwhitespaceisnullorempty

C# item isn't in List, and not null or whitespace


Hello!
We're working on a program where you can create new 'folders' and upload files to them.
The problem is, I want to make that inside a Folder, it is not allowed to create two subfolder with the same name.
Here's my code.

while (string.IsNullOrEmpty(folderName) || !foglalt || !string.IsNullOrWhiteSpace(folderName))
{
    folderName = Console.ReadLine();

    if (string.IsNullOrEmpty(folderName) || string.IsNullOrWhiteSpace(folderName))
    {
        Console.WriteLine("Ez a mező nem lehet üres. \nÚj mappa neve:");
        //(Can't be null. \nNew foldername:)
        break;
    }

    if (p.FolderList.Contains(folderName))
    {
        foglalt = true;
        Console.WriteLine(
            "Ez a mappanév egyszer már szerepel ebben a környezetben. Kérlek válassz újat!\nÚj mappa neve:");
        //(That name is already taken. \nNew foldername:)
    }
    else {foglalt = false;}
}

("foglalt" is a bool, means taken.)
There must be something wrong in the while condition, but i just don't know what.
Could you please help me fix this?


Solution

  • while (true)
    {
        folderName = Console.ReadLine();
    
        // if folder name is null or empty or whitespace, ask for a new folder name
        if (string.IsNullOrEmpty(folderName) || string.IsNullOrWhiteSpace(folderName))
        {
            Console.WriteLine("Ez a mező nem lehet üres. \nÚj mappa neve:");
            //(Can't be null. \nNew foldername:)
        }
        //if folder name already exists, ask for a new one
        else if (p.FolderList.Contains(folderName))
        {
            Console.WriteLine("Ez a mappanév egyszer már szerepel ebben a környezetben. Kérlek válassz újat!\nÚj mappa neve:");
            //(That name is already taken. \nNew foldername:)
        }
        else //Folder Name is valid
            break; //proceed to do stuff with the folder name
    }
    //Do my stuff with the valid folder name