ThePentiumGuy Posted January 6, 2005 Posted January 6, 2005 Hey, Is there any way I can get back the key of a collection? By this I mean: Dim x as new collection x.add(aClass, "key") x.add(aclass2, "key2") for i as integer = 1 to x.count MessageBox.Show(x.Items(i).key) end for I'm looking for some way to retrieve the key value for this thing. Thanks, -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
pas30339 Posted January 7, 2005 Posted January 7, 2005 It depends on the type of collection. The most straight forward is a Hashtable. You can also use a SortedList. Quote
sgt_pinky Posted January 7, 2005 Posted January 7, 2005 What are aClass and aClass2? Using a hash table like pas30339 said, you can do this kind of thing: Dim x As New Collections.Hashtable x.Add("key1", "value1") x.Add("key2", "value2") Dim sVal As String = x.Item("key1") MessageBox.Show(sVal) And sVal will contain "value1". Quote
dwm Posted January 7, 2005 Posted January 7, 2005 .GetKey should return the key of a particular item Quote
ThePentiumGuy Posted January 7, 2005 Author Posted January 7, 2005 aClass1 adn aclass2 are instances of a class. x.Add("key1", "value1") x.Add("key2", "value2") Judging from what you wrote it seems as though in a hashtable you can only add strings (value1 and value2) instead of classes. Is there any way to retreive the key based on the number (index) -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
dwm Posted January 7, 2005 Posted January 7, 2005 .GetByIndex will retrieve an object based on the index...from there you should just be able to check the .GetKey property... x.GetByIndex(i).GetKey I haven't tested this since I don't have VS .NET in front of me, but if I remember correctly, that should do the trick... hope that helps... Quote
pas30339 Posted January 7, 2005 Posted January 7, 2005 Quote aClass1 adn aclass2 are instances of a class. x.Add("key1", "value1") x.Add("key2", "value2") Judging from what you wrote it seems as though in a hashtable you can only add strings (value1 and value2) instead of classes. Is there any way to retreive the key based on the number (index) -The Pentium Guy Actually a Hashtable stores objects for both the keys and the values. You can't access the key via an index. You access the keys and the values via a DictionaryEntry object: Hashtable ht = new Hashtable(); MyTest mt1 = new MyTest(); mt1.localValue = "Test 1"; MyTest mt2 = new MyTest(); mt2.localValue = "Test 2"; MyTest mt3 = new MyTest(); mt3.localValue = "Test 3"; ht.Add("One", mt1); ht.Add("Two", mt2); ht.Add("Three", mt3); foreach( DictionaryEntry entry in ht ) { Console.WriteLine( entry.Key + " " + ((MyTest)entry.Value).localValue ); } // prints out // One Test 1 Three Test 3 Two Test 2 Quote
ThePentiumGuy Posted January 7, 2005 Author Posted January 7, 2005 Ah thanks. Bt that brings up another question: What's the difference between a Collection and a Collection.HashTable. -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
pas30339 Posted January 7, 2005 Posted January 7, 2005 (edited) Quote Ah thanks. Bt that brings up another question: What's the difference between a Collection and a Collection.HashTable. -The Pentium Guy A Collection is a means of storing groups of objects or Types. Depending upon how you want to store and retrieve your objects determines the type of collection you use: A HashTable is a type of collection. Reference Edited January 7, 2005 by pas30339 Quote
ThePentiumGuy Posted January 7, 2005 Author Posted January 7, 2005 So it seems as though they're the same. But HashTable has a little extra 'features'. -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
pas30339 Posted January 7, 2005 Posted January 7, 2005 Quote So it seems as though they're the same. But HashTable has a little extra 'features'. -The Pentium Guy Well, this won't work: Collection c = new Collection() ; since the .Net Framework does not have a type 'Collection', but it does have a type Hashtable which belongs to the System.Collections namespace. A collection is a type of container, but you need to know the type of container you want to use. Simply saying 'give me a container' won't work but 'give me a hashtable' works. Quote
ThePentiumGuy Posted January 8, 2005 Author Posted January 8, 2005 Are you serious? I swear to god it worked for me in VB.NET. I guess this is one of those things which seperates VB.NET from C#? Judging from what you said, Dim c as new Collection() is improper code, and should not be written (even though VB.NET allows this). -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
pas30339 Posted January 8, 2005 Posted January 8, 2005 Quote Are you serious? I swear to god it worked for me in VB.NET. I guess this is one of those things which seperates VB.NET from C#? Judging from what you said, Dim c as new Collection() is improper code, and should not be written (even though VB.NET allows this). -The Pentium Guy It appears you are correct. The Collection object you're familair with is in the Microsoft.VisualBasic namespace. 1 I was unaware of that. I've never worked with VB. And it has an Items property which can be indexed too. 2 Quote
ThePentiumGuy Posted January 8, 2005 Author Posted January 8, 2005 Yeah. I think I'll stick with a hashtable, thanks. It seems like a "higher-end" version of a Collection. -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
sgt_pinky Posted January 9, 2005 Posted January 9, 2005 It's not a 'higher end' of collection, it's just a type of collection. There may be another type of collection that is more suitable, if you are indexing classes. Collections.ArrayList, for example, is excellent if you're indexing any type of object, and the number of objects changes often. If you say, "I want a car", someone will ask you, "What type of car do you want?". Same with collections. Quote
ThePentiumGuy Posted January 9, 2005 Author Posted January 9, 2005 Hmm. This brings up another point: What are the advantages/disadvantages of each type? (I'm writing a tutorial on Collections/ArrayLists/Hashtables/Stacks/Queues. I know stacks and queues are different from the rest so please ignore that). From my experience: ArrayLists are faster. You can specify a "default" bound. (ex dim x as new arraylist(75)). The default is set to make it more optimized. If you go over the bound it will be slower, as per IcePlug. Collections are basic. They're simply used to add/remove objects. Hashtables are more advanced in that you can store anything you want as the Key value (it can be any Object). Therefore, you can have class-class 'pairings', in one hashtable. -TPG Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.