Hashtables and Materials (C#)

Started by
2 comments, last by Neon_C 16 years, 1 month ago
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!
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?
Mike Popoloski | Journal | SlimDX
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.
Ok cool... so the dictionary class worked

This topic is closed to new replies.

Advertisement