Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

Dictionaries in C#

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a Dictionary in C# how can I add additional information (very much like a list) to a specific key?

private Dictionary<byte, VertexPositionNormalExtra[]> vertices_Dictionary = new Dictionary<byte, VertexPositionNormalExtra[]>();

......

vertices_Dictionary.Add(0, primitive.Vertices);

So the first time around the primitive.Vertices get added a key 0 but how do I add additional Vertices to the same key with overwriting the Vertices that are already in there? Very much like using List.Add() Thanks

Share this post


Link to post
Share on other sites
Advertisement
You can cahnge the values of existing keys in a Dictionary just like you would read them back by using the indexer (i.e. vertices_Dictionary[key] = newValue).
Doing so will override the previous value. If you want to add vertices to a key you would have to use a List<> as the value type.

Share this post


Link to post
Share on other sites

Dictionary<byte, List<VertexPositionNormalExtra>> vertices;

if (vertices.ContainsKey(key))
vertices[key].Add(newVertices);
else
vertices.Add(key, new List<VertexPositionNormalExtra>() { newVertices });

Share this post


Link to post
Share on other sites
Thanks very much lads.

Are there any potential problems using this method, such as high levels of garbage?



if (vertices_Dictionary.ContainsKey(key))
{
vertices_Dictionary[key].Add(primitive.Vertices[0]);
vertices_Dictionary[key].Add(primitive.Vertices[1]);
vertices_Dictionary[key].Add(primitive.Vertices[2]);
vertices_Dictionary[key].Add(primitive.Vertices[3]);
}
else
{
vertices_Dictionary.Add(key, new List<VertexPositionNormalExtra>() { primitive.Vertices[0] });
vertices_Dictionary[key].Add(primitive.Vertices[1]);
vertices_Dictionary[key].Add(primitive.Vertices[2]);
vertices_Dictionary[key].Add(primitive.Vertices[3]);
}

Share this post


Link to post
Share on other sites
In the examples above, the value type is a list. Rather than assigning a value to a key as in your example, you should be adding to the list represented by that key. In the code you posted, you are replacing the value with a new value (or would be if the code worked!).

See below, based on Mike's snippet;

// This is a Dictionary of Lists, each referenced to a byte key.
Dictionary<byte, List<VertexPositionNormalExtra>> vertices;

// Check to see if the list represented by 'key' is present
if (vertices.ContainsKey(key))
{
// There is already a list associated with the key - append the new vertices
vertices[key].AddRange(newVertices);
}
else
{
// There isn't a vertex list assocated with the key - create one, containing the new vertices
vertices.Add(key, new List<VertexPositionNormalExtra>(newVertices));
}


Where newVertices is the list of vertices to add, and key representing the list you want to append to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!