Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.




113 posts

Master Geek


#58729 17-Mar-2010 23:37
Send private message

Hi guys,

 

Say I have a class Item, which has 2 attributes, ID : String and Description : String.  I want to be able to make an array or list of Items and be able to index it on the ID which is a string.

 

I've had a look at Arrays, Lists, Collections, ArrayLists but cant find anything that will let me do what I need it to do.

 

Im pretty new to C#, and this wasnt covered in the course I took.

 

Essentially I need what Jade calls a MemberKeyDictionary but in C#.

 

Is this possible?

 

Cheers

 

Pwaddles

Create new topic
Cloud Guru
4062 posts

Uber Geek

Trusted
Snowflake
Subscriber

  #308562 17-Mar-2010 23:51
Send private message

you need to include the IEnumerator in your class. Something like this which was cut out of some inelegant code i wrote once:

public class MyObjectList
{
public MyObject[] MyObjectList = new MyObject[0];

public IEnumerator GetEnumerator()
{
return (MyObjectList as IEnumerable).GetEnumerator();
}

public void AddMyObject(MyObject NewItem){
Array.Resize(ref MyObjectList, MyObjectList.Length + 1);
MyObjectList[MyObjectList.Length -1] = NewItem;
}
}

PS. i'm not a C# guru..... those who are please step in and offer advice or criticism!

PPS.  Google up IEnumerator and C# and you'll probably find some useful articles




Cloud Guru
4062 posts

Uber Geek

Trusted
Snowflake
Subscriber

 
 
 
 


4894 posts

Uber Geek

Trusted

  #308602 18-Mar-2010 08:36
Send private message

You could look at:
System.Collections.Generic.SortedList<string,yourObjecttype> myArray = new System.Collections.Generic.SortedList<string,yourObjecType>();

EDIT: There should be greater than and less than signs around the string and yourobjecttype - but the editor seems to kill them

And create a struct for you objects with the two attributes.

This will create a sorted list collection with strongly typed objects.

Then to enumerate the collection of values you can go:
foreach (YourObjectType myObject in myArray.Values)
{
     Do stuff here
}




Previously known as psycik

OpenHAB: Gigabyte AMD A8 BrixOpenHAB with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave, Xiaomi Humidity and Temperature sensors and Bluetooth LE Sensors
Media:Chromecast v2, ATV4, Roku3, HDHomeRun Dual
Windows 10
Host (Plex Server/Crashplan): 2x2TB, 2x3TB, 1x4TB using DriveBender, Samsung 850 evo 512 GB SSD, Hyper-V Server with 1xW10, 1xW2k8, 2xUbuntu 16.04 LTS, Crashplan, NextPVR channel for Plex,NextPVR Metadata Agent and Scanner for Plex


Create new topic





Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

OPPO announces new flash charge technologies
Posted 16-Jul-2020 08:17


Soul Machines joins forces with the World Health Organization
Posted 13-Jul-2020 18:00


Chorus completes the build and commissioning of two new core Ethernet switches
Posted 8-Jul-2020 09:48


National Institute for Health Innovation develops treatment app for gambling
Posted 6-Jul-2020 16:25


Nokia 2.3 to be available in New Zealand
Posted 6-Jul-2020 12:30


Menulog change colours as parent company merges with Dutch food delivery service
Posted 2-Jul-2020 07:53


Techweek2020 goes digital to make it easier for Kiwis to connect and learn
Posted 2-Jul-2020 07:48


Catalyst Cloud launches new Solutions Hub to support their kiwi Partners and Customers
Posted 2-Jul-2020 07:44


Microsoft to help New Zealand job seekers acquire new digital skills needed for the COVID-19 economy
Posted 2-Jul-2020 07:41


Hewlett Packard Enterprise introduces new HPE GreenLake cloud services
Posted 24-Jun-2020 08:07


New cloud data protection services from Hewlett Packard Enterprise
Posted 24-Jun-2020 07:58


Hewlett Packard Enterprise unveils HPE Ezmeral, new software portfolio and brand
Posted 24-Jun-2020 07:10


Apple reveals new developer technologies to foster the next generation of apps
Posted 23-Jun-2020 15:30


Poly introduces solutions for Microsoft Teams Rooms
Posted 23-Jun-2020 15:14


Lenovo launches new ThinkPad P Series mobile workstations
Posted 23-Jun-2020 09:17



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.