# Hashtables and Materials (C#)

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

## 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 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 on other sites
Quote:
 Original post by Mike.PopoloskiA 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 materials = new Dictionary();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 on other sites
Ok cool... so the dictionary class worked

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011473
• ### Who's Online (See full list)

There are no registered users currently online

×