0

I have just started learning c# and am having a slight problem with a program I'm creating.

I'm creating a program which will create a Number of lotto tickets each with an ID and six randomly generated numbers.

Here is my code for creating the tickets and adding them to a List.

Random random = new Random();// Random number generator
for (int i = 0; i < 1000; i++)
{
    do
    {
    int randomNumber = random.Next(1, 42);// Create randoms numbers between 1 and 42
    ticketNumbers.Add(randomNumber);// Add the random numbers to the ticketNumbers SortedSet                  
    }
    while (ticketNumbers.Count< 6);// Stop Adding once the six numbers is reached

    CTicket firstTicket;
    firstTicket = new CTicket(i, ticketNumbers);// Create ticket object pass i's current value as the ticket id and pass the ticketNumbers sorted set as the ticket numbers
    ticketList.Add(firstTicket);// Add the firstTicket object to the ticketList
    ticketNumbers.Clear();// clear the ticketNumbers list to start again for the next ticket
}

CTicket.Printlist(ticketList);

Here is my function to print the list:

public static void Printlist(List<CTicket> ticketList)
{
    foreach (CTicket mytick in ticketList)
    {
        Console.WriteLine("Ticket ID = {0} ", mytick.m_ticketID);

        foreach (int val in mytick.m_ticketNumbers)
        {
            Console.WriteLine(val);
        }                            
    }
}

My problem is when I create a ticket object and add it to the list the print function will only display the id and not the numbers. I have traced the issue to the following:

ticketNumbers.Clear();

It appears that the list is also being cleared for all previous ticketNumbers which are used in creating the objects

Could someone please explain to me why this is happening?

1
  • 1
    This really has not much to do with your question, but I suspect there's still a flaw in your ticket generator logic. I imagine that these are lottery-style six-part numbers. It's very likely that some of these tickets will contain duplicate numbers within them. You'll probably want to revisit your number generator to be sure you're generating 6 unique numbers in each ticket... Commented Feb 3, 2016 at 16:56

2 Answers 2

6

When you create your CTicket instance,

firstTicket = new CTicket(i, ticketNumbers);

the ticketNumbers is a reference to a SortedSet instance which is being passed to the CTicket class. All your CTicket instances contain a copy of a reference to this same SortedSet instance. When you clear that, all CTickets will reference the same empty set.

The solution is to create a new ticketNumbers set for each CTicket, then each CTicket will contain a reference to a different set.

Random random = new Random();// Random number generator
for (int i = 0; i < 1000; i++)
{
    SortedSet<int> ticketNumbers = new SortedSet<int>();
    do
    {
    int randomNumber = random.Next(1, 42);// Create randoms numbers between 1 and 42
    ticketNumbers.Add(randomNumber);// Add the random numbers to the ticketNumbers SortedSet                  
    }
    while (ticketNumbers.Count< 6);// Stop Adding once the six numbers is reached

    CTicket firstTicket;

    firstTicket = new CTicket(i, ticketNumbers);// Create ticket object pass i's current value as the ticket id and pass the ticketNumbers sorted set as the ticket numbers
    ticketList.Add(firstTicket);// Add the firstTicket object to the ticketList
    ticketNumbers.Clear();// clear the ticketNumbers list to start again for the next ticket
}

CTicket.Printlist(ticketList);

See here for more information on passing reference type parameters in C#.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for explaining. I have come from c++ so I'm not used to passing by reference without the use of &.
Actually it is passed by value but ticketNumbers is a reference to the SortedSet so you are passing the reference by value. I've edited the answer to make this clear.
Just an observation off the original topic but if you want 42 to be a number that can be randomly created, you need to change 42 to 43 in the Random.Next as the upper bound is exclusive.
0

@sean explained you have to re intialize list variable instead of clear

ticketNumbers = new List<int>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.