Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Alismuffin · May 30, 2012 at 08:46 AM · c#arraylist

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GuyTidhar · May 30, 2012 at 08:54 AM 0
Share

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

avatar image Alismuffin · May 30, 2012 at 09:03 AM 0
Share

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

avatar image Alismuffin · May 30, 2012 at 09:05 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GuyTidhar · May 30, 2012 at 09:14 AM

   [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;
    }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flamy · May 30, 2012 at 09:16 AM 1
Share

:O 20 sec difference lol

avatar image Alismuffin · May 30, 2012 at 09:22 AM 1
Share

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

avatar image flamy · May 30, 2012 at 09:24 AM 1
Share

well this answer is more elaborate :)

avatar image Alismuffin · May 30, 2012 at 10:45 AM 0
Share

Hey is the section:

foreach(TestDef test in TestDef) A typo?

avatar image whydoidoit · May 30, 2012 at 11:06 AM 1
Share

Yeah he meant foreach(TestDef test in tests)

Show more comments
avatar image
3

Answer by flamy · May 30, 2012 at 09:15 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Aug 08, 2013 at 10:30 AM 0
Share

really usefull, thank you all

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

Give a name for each class in List/array 2 Answers

Splitting text from lines into variables 0 Answers

Inventory System wont work 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges