I'm creating a console app to study c# in which I have a buyer class with methods to generate random names and cpf number (kind like a social security number).
In the Main method I create a list of buyers with 10 instances of the buyer class using for loop.
The problem is: when I run the application, 10 identical instances are created and when I debug it step by step 10 different instances are created.
How do I force the app to complete the methods that generate names and numbers before moving on to the next loop?
I can post the buyer class later if needed.
Thanks!
static void Main(string[] args)
{
List<Buyer> buyers = new List<Buyer>();
for (int i = 0; i < 10; i++)
{
buyers.Add(new Buyer());
Console.WriteLine(buyers[i].name.ToString() + " " + buyers[i].cpfNumber.ToString());
}
Console.WriteLine(buyers.Count.ToString());
Console.ReadLine();
}
Buyer Class
class Buyer
{
public string cpfNumber { get; set; }
public string name { get; set; }
public string email { get; set; }
public Buyer()
{
cpfNumber = generateCpf();
name = generateName();
}
public string generateName()
{
Random random = new Random();
string[] names = new string[]
{
"Celestine",
"Nichole",
"Patti",
"Titus",
"Betsy",
"Gabrielle",
"Alisha",
"Elena",
"Shena",
"Ruthann",
"Coletta",
"Sherlyn",
"Wei",
"Donella",
"Eleonor",
"Arden",
"Milford",
"Isaac",
"Evia",
"Shelley"
};
string[] surnames = new string[]
{
"Barnes",
"Harmon",
"Lamb",
"Nicholson",
"Andrade",
"Lara",
"Gillespie",
"Hodge",
"Reynolds",
"Camacho",
"Franco",
"Costa",
"Vasquez",
"Hayden",
"Bowen",
"Hernandez",
"Berg",
"Patterson",
"Robertson",
"Salinas"
};
name = names[random.Next(0, 19)].ToString() + " " + surnames[random.Next(0, 19)].ToString();
return name;
}
public string generateCpf()
{
#region generateCpf_variables
//array of cpf digit generated in loop
int[] cpfDigit;
//cpfDigit * weight to calculate the first verification digit
int[] firstDigitSeed;
int[] secondDigitSeed;
//first verification digit sum
int firstDigitSum = 0;
int secondDigitSum = 0;
//first verification digit
int firstDigit = 0;
int secondDigit = 0;
#endregion
//9 basic digits of cpf + verification digit
cpfDigit = new int[10];
//multiplication of cpfDigit with weight (range between 2 and 10)
firstDigitSeed = new int[8];
//multiplication of cpfDigit with weight (range between 2 and 10)
secondDigitSeed = new int[8];
Random random = new Random();
//loop to generate cpf seeds and firstDigit calculus
for (int i = 0; i < 8; i++)
{
//random cpf digit
cpfDigit[i] = random.Next(0,10);
//product of cpf digit and verification weight
firstDigitSeed[i] = cpfDigit[i] * (10 - i);
//sum of firstDigitSeeds
firstDigitSum += firstDigitSeed[i];
}
if (firstDigitSum % 11 >= 2)
{
firstDigit = 11 - (firstDigitSum % 11);
}
cpfDigit[9] = firstDigit;
for (int i = 0; i < 8; i++)
{
secondDigitSeed[i] = cpfDigit[i] * (11 - i);
secondDigitSum += secondDigitSeed[i];
}
//second verification number calculator
if (secondDigitSum % 11 >= 2)
{
secondDigit = 11 - (firstDigitSum % 11);
}
foreach (int item in cpfDigit)
{
cpfNumber += item.ToString();
}
cpfNumber += secondDigit.ToString();
return cpfNumber;
}
}