Im trying to do something i thought could be relatively simple but im missing something here.
-
I would like to generate a name.
-
Check if this name exists in a database
-
If it does re-generate a new name until a unique one is found and return that value
I decided to go with a do loop
as below
var uniqueName;
uniqueName = GetUniqueName(countryId);
var nameExists = nameService.GetName(uniqueName);
do
{
if (nameExists == null)
{
return uniqueName;
}
} while (nameExists != null);
return uniqueName;
I then run the app and all works as expected.
I now deliberately create a duplicate name and it keeps looping the if statement. If adding the nameExists
inside the do loop then i get an error.
What have i missed?
Ive used a few articles as reference as this one which i thought im carrying out the instructions correctly https://www.tutorialsteacher.com/csharp/csharp-while-loop