Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Benny Ogidan
Benny Ogidan
17,948 Points

Help with a C# assignment (Unique identifier)

I am having problems with an assignment of mine .I was instructed create a dummy grid world for a Party app (2D point array with coordinates for X,Y). From my understanding each coordinate has to have a maximum of 1 party assigned to them which has to be unique. At the moment, i am having a problem with my logic as i have passed a method to generate Parties which will return a list of Parties but i feel my logic as it flawed (see below). My plan is to return the results to the Cordinate class and loop over them so each cordinate has a random party id assigned to it

I already have a Coordinate Class which produces a Constructor Coordinate(x,y). this is fed into a Party World Class which constructs a 2D array for my World Grid

At the moment for my Party class i have

public class Party { static Random unique = new Random(); public List<int> Party { get; set; }

    public Party(int _count)
    {
        Party = Generate_EventId(_count);

    }

   public List<int> Generate_EventId(int count)
    {
        HashSet<int> partygen = new HashSet<int>();
        while (partygen.Count < count)
        {
            partygen.Add(unique.Next());
        }

       List<int> result = new List<int>();
        result.AddRange(partygen);
        return result;

    }

}

} The Assignment question specifically says; *Each party has a unique numeric identifier (e.g. 1, 2, 3) and should assume that each co-ordinate can hold a maximum of one event * [hence me using HashSet and Random]. However, i think i should be using a dictionary to generate the numbers using keys. Can someone help me with this please

1 Answer

Steven Parker
Steven Parker
231,124 Points

It's not obvious to me that you need any of this complexity.

From the quote of the instructions, I would assume that a simple integer is all that is needed to store the unique identifier (which would be sequentially assigned), and none of the random stuff or hashing is needed at all. Or did I miss something?