[C#] Generating a unique ID for each object instance

Started by
16 comments, last by Spa8nky 14 years, 9 months ago
If I wanted a quick and simple way of generating a unique string ID for each instance of a class would the following method suffice:


this.iD = this.GetType().Name.ToString() + this.GetHashCode().ToString();

Or do the resident coding gurus have a better suggestion for assigning a unique string based ID? Thanks guys.
Advertisement
Hashcodes aren't guaranteed to be unique. A better idea would probably be to generate a new Guid and grab a string from that.
Mike Popoloski | Journal | SlimDX
Fantastic, thank you!

this.iD = Guid.NewGuid().ToString();
Would it make more sense to have the id as a Guid object rather than a string?
What will these UIDs be used for?
Normally I just make a static int member called ID. I initialize it as 1 with a static constructor.

I then have a static method called getID that returns an int.

public static int getID(){    return ID++;}


Now you can get a unique ID any time you want.

theTroll
These GUID (as strings) are used for indentifying Quads in a resource pool of Quads.

E.G.

            //=============================================            //--------- [Add Quad to Resource Pools] ------            //=============================================            // If the Dictionary associated with the current effect doesn't contain the string key (quad_ID)             if (!this.vertices_Pool[this.effect_Type].ContainsKey(quad_GUID))            {                // Add the string key (quad_ID) to the Dictionary and                // Create a new array of Vertices (maximum of 4) for the current key                this.vertices_Pool[this.effect_Type].Add(quad_GUID, new VertexPositionNormalExtra[GameConstants.QUAD_NUMBER_OF_VERTICES]);                // Add the current Quad's vertices to the Dictionary under the quad_ID key                this.vertices_Pool[this.effect_Type][quad_GUID] = quad_Current.Vertices;            }            else            {                // Add the current Quad's vertices to the Dictionary under the quad_ID key                this.vertices_Pool[this.effect_Type][quad_GUID] = quad_Current.Vertices;            }
I think GUIDs are a bit overkill in this situation. Generating GUIDs is somewhat slowish if I recall correct, esp. when you're going to create thousands of them in a situation where an ID would suffice.

However, why exactly are you storing your quads with an ID? Wouldn't it be more efficient to just check if an object instance is already in the list? And on dictionaries, why not just calculate the hash code? The hash isn't used to store the object by, just as a way to quickly index it.

Toolmaker

Yes I believe you are right. GUIDs might be a bit of overkill in this situation.

I guess using the static getID method would be the fastest.

However, as 10000s Quads are created, updated and deleted from various points in the Dictionary in no particular order, I must know which ones to update, delete etc.

The ID will not change for the Dictionary but when using an index of an array of Quads the index would change when Quads are deleted, this is why I use a Dictionary with IDs.

I can also easily get the number of Vertices and Indices to draw as follows with a Dictionary:

        public void Draw(Camera camera)        {            foreach (string key in vertices_Pool.Keys)            {                // Check to see if there are any Quads to draw with the current key (effect)                if (vertices_Pool[key].Count != 0)                {                    // Reset index and current vertex and index count to 0 for current key (effect)                    index = 0;                    vertex_Count = 0;                    index_Count = 0;                    // Check each GUID (Global Unique ID) key to see if VertexPositionNormalExtra array contains any data                    foreach (string iD in vertices_Pool[key].Keys)                    {                        // If the VertexPositionNormalExtra array is not null                        if (vertices_Pool[key][iD] != null)                        {                            // Copy the vertex data to the drawable vertices for this effect                            vertices_Pool[key][iD].CopyTo(vertices_Drawable, vertex_Count);                            // As all Quad indices are identical, don't work out indices until drawable Quads are being calculated                            this.indices_Drawable[index++] = (short)(0 + vertex_Count);                            this.indices_Drawable[index++] = (short)(1 + vertex_Count);                            this.indices_Drawable[index++] = (short)(2 + vertex_Count);                            this.indices_Drawable[index++] = (short)(1 + vertex_Count);                            this.indices_Drawable[index++] = (short)(3 + vertex_Count);                            this.indices_Drawable[index++] = (short)(2 + vertex_Count);                            // Increase the vertex count by the number of vertices in a Quad                            vertex_Count += GameConstants.QUAD_NUMBER_OF_VERTICES;                                                        // Do the same for indices                            index_Count += GameConstants.QUAD_NUMBER_OF_INDICES;                        }                    }                    // Make sure there are Quad vertices to draw (will be 0 if all vertex arrays associated with iD are null)                    if (vertex_Count != 0)                    {                        // If reusing Quads (E.G. BulletManager) vertex_Count should not keep increasing if maximum avaialable Quad value (MAX_BULLETS) has been reached                        //Console.WriteLine(vertex_Count);                        DrawQuadrangles(key, camera);                    }                }            }        }


EDIT:

Something like this will do the trick?

    // Globally Unique IDentifier     public static class GUID    {        private static int ID;        public static int GetUID()        {            ID++;            return ID;        }    }
I wouldn't make a whole new class. I would add the static member and static method to the class the needs the UID. This keeps it where it is going to be used.

theTroll

This topic is closed to new replies.

Advertisement