Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have a Random method:

C#
private string Random(Random random)
{
    if (random.Next(animals.Count) && randomAnimal.Name != null)
    {
        txtName.Text = randomAnimal.Name;
        return randomAnimal.Name;
    }
    else if (randomAnimal.Specie != null)
    {
        txtSpecie.Text = randomAnimal.Specie;
        return randomAnimal.Specie;
    }
    else
    {
        return MessageBox.Show("Failure").ToString();
    }
}


What I have tried:

As you can see this basically displays a random item from a List 'animals'

I also make that whatever is displayed in the textbox it then is being set to null.

Is it possible to skip random iteration and start the Random method all over again to generate not null values in the text boxes?

The point is that it sometimes add the random item only to txtName or txtSpecie. If, for example it is displayed in txtName it make the txtName value null in the list. I want to make sure that when random is being generated and the next random value is null it will not try to display but instead start to generate the random all over again.

Thanks in advance
Posted
Updated 4-Nov-17 19:11pm
v4

1 solution

Why not just remove it once that item has been assigned, in this way, there is no need for unnecessary iterations. [^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900