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


Topic # 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
Infrastructure Geek
4056 posts

Uber Geek
+1 received by user: 195

Trusted
Microsoft NZ
Subscriber

  Reply # 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




Technical Evangelist
Microsoft NZ
about.me/nzregs
Twitter: @nzregs


Infrastructure Geek
4056 posts

Uber Geek
+1 received by user: 195

Trusted
Microsoft NZ
Subscriber

  Reply # 308565 17-Mar-2010 23:58
Send private message




Technical Evangelist
Microsoft NZ
about.me/nzregs
Twitter: @nzregs


3982 posts

Uber Geek
+1 received by user: 513

Trusted

  Reply # 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

NextPVR/OpenHAB: 
Gigabyte AMD A8 Brix --> Samsung LA46A650D via HDMI, NextPVR,OpenHAB with Aeotech ZWave Controller
Media:Chromecast v2, ATV4, Roku3, Raspberry PI temperature Sensors and Bluetooth LE Sensors,HDHomeRun Dual
Windows 2012 
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 »

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 »

Intel introduces new NUC kits and NUC mini PCs
Posted 16-Aug-2018 11:03


The Warehouse leaps into the AI future with Google
Posted 15-Aug-2018 17:56


Targus set sights on enterprise and consumer growth in New Zealand
Posted 13-Aug-2018 13:47


Huawei to distribute nova 3i in New Zealand
Posted 9-Aug-2018 16:23


Home robot Vector to be available in New Zealand stores
Posted 9-Aug-2018 14:47


Panasonic announces new 2018 OLED TV line up
Posted 7-Aug-2018 16:38


Kordia completes first live 4K TV broadcast
Posted 1-Aug-2018 13:00


Schools get safer and smarter internet with Managed Network Upgrade
Posted 30-Jul-2018 20:01


DNC wants a safer .nz in the coming year
Posted 26-Jul-2018 16:08


Auldhouse becomes an AWS Authorised Training Delivery Partner in New Zealand
Posted 26-Jul-2018 15:55


Rakuten Kobo launches Kobo Clara HD entry level reader
Posted 26-Jul-2018 15:44


Kiwi team reaches semi-finals at the Microsoft Imagine Cup
Posted 26-Jul-2018 15:38


KidsCan App to Help Kiwi Children in Need
Posted 26-Jul-2018 15:32


FUJIFILM announces new high-performance lenses
Posted 24-Jul-2018 14:57


New FUJIFILM XF10 introduces square mode for Instagram sharing
Posted 24-Jul-2018 14:44



Geekzone Live »

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



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.