473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Generic Collection Indexer

149 100+
Hi Guys,

I have assigned a variable to a generic list class and would like to access the propertied dynamically.

Do I need to create an indexer for this? And how do I do that with a strongly typed collection.

Here's the code;

Expand|Select|Wrap|Line Numbers
  1. PageInfo pageIDS = PageInfo.GetPageFromCache(pageId);
  2.                 for (int i = 0; i < 10; i++)
  3.                 {
  4.                     int compId = (int)pageIDS["ComponentID"+i]; //this bit doesn't work
  5.                     List<PageCompData> recordset = PageCompData.GetCompTypeByPageID(pageId, compId);
  6.                     comp = GetComponentListFromComponentDetailsList(recordset);
  7.                 }
  8.                 BaseCache.CacheData(key, comp);
Please help!
Oct 10 '07 #1
2 1627
rsdev
149 100+
I have created a strongly typed indexer. But I don't think this is good OOP. What would you do?

Expand|Select|Wrap|Line Numbers
  1. //Indexer
  2.         public int this[int index]
  3.         {
  4.             get
  5.             {
  6.                 if (index == 1)
  7.                     return this.ComponentID1;
  8.                 if (index == 2)
  9.                     return this.ComponentID2;
  10.                 if (index == 3)
  11.                     return this.ComponentID3;
  12.                 if (index == 4)
  13.                     return this.ComponentID4;
  14.                 if (index == 5)
  15.                     return this.ComponentID5;
  16.                 if (index == 6)
  17.                     return this.ComponentID6;
  18.                 if (index == 7)
  19.                     return this.ComponentID7;
  20.                 if (index == 8)
  21.                     return this.ComponentID8;
  22.                 if (index == 9)
  23.                     return this.ComponentID9;
  24.                 if (index == 10)
  25.                     return this.ComponentID10;
  26.                 else
  27.                     return 0;
  28.             }
  29.             set
  30.             {
  31.                 this[index] = value;
  32.             }
  33.         }
Oct 10 '07 #2
rsdev
149 100+
If anyone needs to know this seems to do the job.

Expand|Select|Wrap|Line Numbers
  1. //Indexer
  2.         public int this[int index]
  3.         {
  4.             get
  5.             {
  6.                 return this[index];
  7.             }
  8.             set
  9.             {
  10.                 this[index] = value;
  11.             }
  12.         }
[/quote]
Oct 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). ...
4
by: Matt | last post by:
Hi, I've been thinking about how to do this, but can't think of a solution. I have a class that is derived from System.Web.UI.WebControls.DataGrid which works a treat, but I'd like to extend...
8
by: Steven Cummings | last post by:
Hello, I've scoured this usenet group and didn't find anything specific to my problem, so hopefully this won't be a repeated question. I'm all but certain it's not. I would like to *declare*...
1
by: SP | last post by:
I have created an abstract class inheriting from KeyedCollection<long, TItemto use as a base class for my collections. In some derived classes I am providing a new indexer property for the key...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
5
by: Nikolay Belyh | last post by:
I have created a "collection" in C# like this: namespace ClassLibrary1 { public class X {} public class Class1 { public ArrayList objs {
3
by: Taurin | last post by:
I have a method that I would like to be able to pass a generic list (such as List<string>, List<int>, etc). I'm trying to figure out what would be the best type of parameter to use to pass in the...
6
by: christian2.schmidt | last post by:
Hi, why does the first Console::Write give error "indexer needs array or pointer" and the second not? generic <typename TList, typename ItemTypewhere TList : IList<ItemType> ref class MyClass...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.