0

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;

    }

}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    What does the code that generates the `Buyer` look like? This is probably a problem with your random seed (or rather, you are probably using the same random seed for each instance). – Matt Burland Aug 22 '16 at 14:12
  • 1
    You can probably find your answer here: http://stackoverflow.com/questions/767999 – Dennis_E Aug 22 '16 at 14:12
  • You are settling on the "default buy" details oncreate, you havent shown any code that helps us explain it – BugFinder Aug 22 '16 at 14:12
  • How the class `Buyer` looks like – sujith karivelil Aug 22 '16 at 14:14
  • @un-lucky: _"I have a buyer class with methods to generate random names and cpf number"_ and _"when I run the application, 10 identical instances are created and when I debug it step by step 10 different instances are created."_, classic symptoms of using the same seed to generate the random classes as per the dupe. – Matt Burland Aug 22 '16 at 14:15
  • I just posted the buyer class. I generate the random instance each time the method is called, is that my error? I will look into the other question Dennis/Daniel posted to see if I can solve it. Thanks – Pedro Monastero Aug 22 '16 at 14:18
  • @PedroMonastero - Once you solve the issue of where to declare your `Random` instance you'll need to discover why the last item in your arrays never gets picked. You find that `random.Next(0, 19)` produces vales from `0 .. 18`, not `0 .. 19`. The second param is an **exclusive** upper-bound. – Enigmativity Aug 22 '16 at 14:25
  • @Enigmativity - Thank you for the heads up! I would probably take a while to figure out that the last item wasn't being picked! – Pedro Monastero Aug 22 '16 at 14:35
  • @PedroMonastero - Ideally you should write it as `names[random.Next(0, names.Length)]`. Also, as another hint, `.ToString()` isn't necessary here as the `names` array is of type string already. – Enigmativity Aug 22 '16 at 23:27
  • @Enigmativity - Both suggestions make sense, I'm going to use them. Once again, thank you for your help! – Pedro Monastero Aug 23 '16 at 13:38

0 Answers0