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


4053 posts

Uber Geek
+1 received by user: 535

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: 
Gigabyte AMD A8 Brix --> Samsung LA46A650D via HDMI, NextPVR,
OpenHAB: ODroid C2 eMMC DriveOpenHAB with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave and Bluetooth LE Sensors
Media:Chromecast v2, ATV4, Roku3, 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:



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.