x


An array or list of hashtables C#

Heya!

Is there any way of creating a list or an array of hashtables?

What I have is a script that sends a query to a server and receives data back. Right now I'm storing the data in Lists after extracting them from the returned values. There would be a list for:

Names Icons Description

And I would say something like:

 int index;
 for (index = 0; index < myListOfNames.Count; index++){
 print("My name is " + myListOfNames[index] + " and " + myListOfDescriptions[index]);
 }

Since I stored them in numerical order, index 0 of names would match index 0 of description. Same with all other index. index 7 of names would correspond to index 7 of icons etc

However I want to be able to create them in a way that allows me to order them alphabetically or display only the ones with a particular name etc

I'm not sure if hashtables would be practical or not. I'd appreciate any insight on the matter.

Thanks heaps!!

more ▼

asked May 30 '12 at 08:46 AM

Alismuffin gravatar image

Alismuffin
111 17 24 25

You wish to be able to store the names using indexes and still locate the description using the name?

May 30 '12 at 08:54 AM GuyTidhar

Ideally I'd like to be able to store the data like so:

IndexOfList

Name : Test Name

Icon : Texture2D

Description : This is a test description

Hmm it's a bit hard to explain but I'll try. A container of containers holding variables of varying types.

I'm mainly looking to clean things up and make it easier to order things in a user defined way Alphabetically, or however else I choose

May 30 '12 at 09:03 AM Alismuffin

Ah I think what I should be looking at is class instantiation?

May 30 '12 at 09:05 AM Alismuffin
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
  [System.Serializable]
    public class TestDef
    {
       public string name;
       public Texture2D icon;
       public string description;
    }

public class MyMain
{
   public TestDef[] tests;

   private Hashtable testsList;

   void Start()
   {
      testsList = new Hashtable();
      foreach(TestDef test in TestDef)
      {
         testsList.Add(test.name, test);
      }
   }

   void SortList()
   {
      // Do you sort algorithm on 'tests'

      // Now you can iterate through 'tests' for sorted list
   }

   // Example for fetching by name using the Hahstable
   TestDef GetData(string byName)
   {
      if ( testsList.ContainsKey(byName) )
         return testsList[byName];

      return null;
   }
}
more ▼

answered May 30 '12 at 09:14 AM

GuyTidhar gravatar image

GuyTidhar
2.2k 4 8 13

:O 20 sec difference lol

May 30 '12 at 09:16 AM flamy

You guys are awesome! Thanks so much I'd like to push the correct answer on both of yours, but since I cant I'll do it on the one that came first.

Thanks!!!

May 30 '12 at 09:22 AM Alismuffin

well this answer is more elaborate :)

May 30 '12 at 09:24 AM flamy

Hey is the section:

foreach(TestDef test in TestDef) A typo?

May 30 '12 at 10:45 AM Alismuffin

Yeah he meant foreach(TestDef test in tests)

May 30 '12 at 11:06 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

create a class like this,

[System.Serializable]
Class  Foo
{
  String name;
  Texture2D icon;
  String description;
}

and create a list
include this bfore that.

using System.Collections.Generic;

and in the code,

List<Foo> _fooList = new List<Foo>();

for(int i=0;i<_fooList.count;i++)
{
  print("My name is " + _fooList[i].name + " and " + _fooList[i].description);
}

I am serializing the class so that it would be visible in the editor too, so that it would be easy to add value frm scene or to debug.

more ▼

answered May 30 '12 at 09:15 AM

flamy gravatar image

flamy
3.6k 5 12 38

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4374
x1396
x382
x5
x2

asked: May 30 '12 at 08:46 AM

Seen: 2352 times

Last Updated: May 30 '12 at 11:42 AM