How can I check if an object exists in the dictionary and return it

Asked

Viewed 1,794 times

4

I am working on a solution in which I must enter on the same function with a given name of a Park. The parks are implemented in a list and each park has a name. It happens that I intend and already tried to develop a function in which when researching a park the same should be returned (written) or should appear a message similar to "It was not found".

I’m working layered in C# (which is where I’m developing the app) and in DL (Data Layer) I have a dictionary of Park, class that has on it a List of Automoveis, and a List of ParkedAutowhich corresponds to a set of cars parked in a car park.

Here’s the method I have at DL:

 public static bool DevolveParque(string fileName, string nomeParque)
        {
            if (File.Exists(fileName))
            {
                try
                {
                    Stream stream = File.Open(fileName, FileMode.Open);
                    BinaryFormatter bin = new BinaryFormatter();
                    parques = (Dictionary<string, Park>)bin.Deserialize(stream);
                    stream.Close();
                    return true;


                }
                catch (IOException e)
                {
                    Console.Write("ERRO:" + e.Message);

                }


                foreach (KeyValuePair<string, Park> x in parques)
                {

                    if (parques.ContainsKey(nomeParque))
                    {
                         Console.WriteLine("Nome: " + x.Key);
                    }
                }
            }
            return false;

        }

If you have noticed me here, I test with a decisive instruction, (a if), if my parks have a key that actually matches the name of the park, existing in the Parks Dictionary (static Dictionary<string, Park> parques = new Dictionary<string, Park>();)

When debugging the program that is running finds the key (which by definition corresponds to the name of the park) but does not demonstrate the park, nor does it tell me that it was not found.

How can I change that.

Note: I am working with files (archives), they have Parks and also have several vehicles inside the parks

1 answer

2


Change the passage:

foreach (KeyValuePair<string, Park> x in parques)
{
    if (parques.ContainsKey(nomeParque))
    {
         Console.WriteLine("Nome: " + x.Key);
    }
}

To

if(parques.ContainsKey(nomeParque)) {
    Park park = parques[nomeParque];
    Console.WriteLine(/* Imprimir aqui o que quer do objeto park*/);
}   
else {
    Console.WriteLine("Não encontrado!");
}

There is no reason to iterate through the items in the dictionary, since the key is unique, just check if it exists and get its value, in this case the object Park. But this judging that your dictionary has been loaded correctly.

  • Thanks for the help Marcusvinicius ;)

  • There is my mania to use cycles for and foreach ... I have to be more calm and careful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.