Jump to content
  • Advertisement
Sign in to follow this  

Hashtables and Materials (C#)

This topic is 3715 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

Ok, So i'm creating a class that loads DirectX materials from an XML document into a hash file and then spits the materials back out. this is how i hav it set up... first i create my hashtable
Hashtable iMaterials = new Hashtable();

than i take teh material that i've pulled from the XML file im calling it Tmaterial and matName is a string (i've already checked to make sure it creates the material properly)
iMaterials.Add(matName, Tmaterial);

this part SEEMS to work properly... however when i try to get the material out again...
Material temp = iMaterials["Red"];

i know that there is a key called "Red". Visual Studio gives me this... Cannot implicitly convert type 'object' so i tryed this...
Material temp = (Material)iMaterials["Red"];

and i get... A first chance exception of type 'System.NullReferenceException' am i just useing HashTable wrong?? Any help would be appricated!

Share this post


Link to post
Share on other sites
Advertisement
A much better solution would be to use the newer and safer Dictionary<> class, which replaces the Hashtable starting from .NET 2.0. In this case, you would use:

Dictionary<string, Material> materials = new Dictionary<string, Material>();

Second, I wonder where exactly you are getting that null reference exception. Is it being thrown on the line you provided, or somewhere else? What does the debugger say about the matName string?

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
A much better solution would be to use the newer and safer Dictionary<> class, which replaces the Hashtable starting from .NET 2.0. In this case, you would use:

Dictionary<string, Material> materials = new Dictionary<string, Material>();

Second, I wonder where exactly you are getting that null reference exception. Is it being thrown on the line you provided, or somewhere else? What does the debugger say about the matName string?


OK, i'll give the dictionary a shot. so far the debuger dosnt have anything to say about my matName string. however the Null Reference exception is thrown right on this line "Material temp = (Material)iMaterials["Red"];" i'm not exactly sure why.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!