I want to add a variable to a list but set it too a random string out of a set of three strings, i want to do this in a foreach loop. I am new to programming.
This is just an example:
foreach (List li in list)
{
if (li.name == "name")
{
rand = RandomNumber(1, 4); // Random is called in
different method
if (rand == 1)
{
newName = "bob";
}
else if (rand == 2)
{
newName = "ben";
}
else if (rand == 3)
{
newName = "dave";
}
class2 obj = new class2(newName, middleName, surname);
List2.Add(obj);
}
}
Every time i print out each object in the list it will set newName to the same string, I want newName to be different
Aucun commentaire:
Enregistrer un commentaire