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

 
 
 
 


4919 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




News »

Pre-orders for Huawei MateBook 13 open now
Posted 14-Aug-2020 14:26


Freeview On Demand app launches on Sony Android TVs
Posted 6-Aug-2020 13:35


UFB hits more than one million connections
Posted 6-Aug-2020 09:42


D-Link A/NZ extends COVR Wi-Fi EasyMesh System series with new three-pack
Posted 4-Aug-2020 15:01


New Zealand software Rfider tracks coffee from Colombia all the way to New Zealand businesses
Posted 3-Aug-2020 10:35


Logitech G launches Pro X Wireless gaming headset
Posted 3-Aug-2020 10:21


Sony Alpha 7S III provides supreme imaging performance
Posted 3-Aug-2020 10:11


Sony introduces first CFexpress Type A memory card
Posted 3-Aug-2020 10:05


Marsello acquires Goody consolidating online and in-store marketing position
Posted 30-Jul-2020 16:26


Fonterra first major customer for Microsoft's New Zealand datacentre
Posted 30-Jul-2020 08:07


Everything we learnt at the IBM Cloud Forum 2020
Posted 29-Jul-2020 14:45


Dropbox launches native HelloSign workflow and data residency in Australia
Posted 29-Jul-2020 12:48


Spark launches 5G in Palmerston North
Posted 29-Jul-2020 09:50


Lenovo brings speed and smarter features to new 5G mobile gaming phone
Posted 28-Jul-2020 22:00


Withings raises $60 million to enable bridge between patients and healthcare
Posted 28-Jul-2020 21:51



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.