Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
usually WE try to return the object of the List<object> along with the integer index like example below:
C#
public List<object> data = new List<object>();
public object Test(int index)
{
   return data[index];
}

But I would like to do is, I wanting to replace the indexed integer to indexed string like below:
C#
public List<object> data = new List<object>();
public object Test(string indexName)
{
   return data[indexName];
}
void main()
{
....
((ClassA)Test["AttributeNo1"]).Hello1();
((ClassA)Test["AttributeNo1"]).Hello2("blabla");
((ClassA)Test["AttributeNo1"]) = new ClassA();
...
((ClassA)data["AttributeNo1"]).Hello1();
((ClassA)data["AttributeNo1"]).Hello2("blabla");
((ClassA)data["AttributeNo1"]) = new ClassA();
...
}

Is there anyway possible to make so? Or something similiar with it, just like MySQL DataReader way, that appears just like:
C#
DataReader reader ....
return reader["AttributeNo1"];
Posted
Updated 23-Jan-15 23:13pm
v3
Comments
Karthik_Mahalingam 24-Jan-15 5:01am    
indexname value will be in number format or some alphanumeric value?

Use Dictionary[^]:
C#
public Dictionary<string,object> data = new Dictionary<string,object>
public object Test(string indexName)
{
   return data[indexName];
}
 
Share this answer
 
Comments
Zoltán Zörgő 24-Jan-15 5:15am    
Yes. That's the way. +5
spaika 24-Jan-15 5:22am    
thanks a lot, this mean to me.. been explored this for years lol
C#
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    Dictionary<string, int> dictionary =
        new Dictionary<string, int>();

    dictionary.Add("apple", 1);
    dictionary.Add("windows", 5);

    // See whether Dictionary contains this string.
    if (dictionary.ContainsKey("apple"))
    {
        int value = dictionary["apple"];
        Console.WriteLine(value);
    }

    // See whether it contains this string.
    if (!dictionary.ContainsKey("acorn"))
    {
        Console.WriteLine(false);
    }
    }
}

Output

1
False


Source: http://www.dotnetperls.com/dictionary[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900