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


3940 posts

Uber Geek
+1 received by user: 493

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 »

Microsoft Dynamics 365 Business Central launches
Posted 10-Jul-2018 10:40


Spark completes first milestone in voice platform upgrade
Posted 10-Jul-2018 09:36


Microsoft ices heated developers
Posted 6-Jul-2018 20:16


PB Technologies charged for its extended warranties and warned for bait advertising
Posted 3-Jul-2018 15:45


Almost 20,000 people claim credits from Spark
Posted 29-Jun-2018 10:40


Cove sells NZ's first insurance policy via chatbot
Posted 25-Jun-2018 10:04


N4L helping TAKA Trust bridge the digital divide for Lower Hutt students
Posted 18-Jun-2018 13:08


Winners Announced for 2018 CIO Awards
Posted 18-Jun-2018 13:03


Logitech Rally sets new standard for USB-connected video conference cameras
Posted 18-Jun-2018 09:27


Russell Stanners steps down as Vodafone NZ CEO
Posted 12-Jun-2018 09:13


Intergen recognised as 2018 Microsoft Country Partner of the Year for New Zealand
Posted 12-Jun-2018 08:00


Finalists Announced For Microsoft NZ Partner Awards
Posted 6-Jun-2018 15:12


Vocus Group and Vodafone announce joint venture to accelerate fibre innovation
Posted 5-Jun-2018 10:52


Kogan.com to launch Kogan Mobile in New Zealand
Posted 4-Jun-2018 14:34


Enable doubles fibre broadband speeds for its most popular wholesale service in Christchurch
Posted 2-Jun-2018 20:07



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.