Sign in to follow this  
Neon_C

Hashtables and Materials (C#)

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this