Click here to Skip to main content
15,905,420 members
Home / Discussions / C#
   

C#

 
QuestionHow to Create Dynamic Buttons With Students Roll No. on It From Database Pin
jackspero1820-Jul-13 1:38
jackspero1820-Jul-13 1:38 
AnswerRe: How to Create Dynamic Buttons With Students Roll No. on It From Database Pin
Garth J Lancaster20-Jul-13 2:05
professionalGarth J Lancaster20-Jul-13 2:05 
QuestionC# unmanage DLL export Pin
hijeenu19-Jul-13 10:32
hijeenu19-Jul-13 10:32 
QuestionUpdate Shorthand? Pin
eddieangel19-Jul-13 8:37
eddieangel19-Jul-13 8:37 
AnswerRe: Update Shorthand? Pin
Dave Kreskowiak19-Jul-13 9:50
mveDave Kreskowiak19-Jul-13 9:50 
AnswerRe: Update Shorthand? Pin
Abhinav S19-Jul-13 18:48
Abhinav S19-Jul-13 18:48 
AnswerRe: Update Shorthand? Pin
Jean A Brandelero22-Jul-13 3:59
Jean A Brandelero22-Jul-13 3:59 
QuestionCalling VB 6 OCX from C# EXE Pin
matinon2219-Jul-13 2:18
matinon2219-Jul-13 2:18 
AnswerRe: Calling VB 6 OCX from C# EXE Pin
Dave Kreskowiak19-Jul-13 2:21
mveDave Kreskowiak19-Jul-13 2:21 
GeneralRe: Calling VB 6 OCX from C# EXE Pin
matinon2223-Jul-13 22:18
matinon2223-Jul-13 22:18 
GeneralRe: Calling VB 6 OCX from C# EXE Pin
Dave Kreskowiak24-Jul-13 3:52
mveDave Kreskowiak24-Jul-13 3:52 
QuestionGeneric collections Pin
PozzaVecia19-Jul-13 0:09
PozzaVecia19-Jul-13 0:09 
AnswerRe: Generic collections Pin
Nicholas Marty19-Jul-13 0:41
professionalNicholas Marty19-Jul-13 0:41 
GeneralRe: Generic collections Pin
PozzaVecia19-Jul-13 1:12
PozzaVecia19-Jul-13 1:12 
GeneralRe: Generic collections Pin
Nicholas Marty19-Jul-13 1:17
professionalNicholas Marty19-Jul-13 1:17 
GeneralRe: Generic collections Pin
PozzaVecia19-Jul-13 1:26
PozzaVecia19-Jul-13 1:26 
GeneralRe: Generic collections Pin
Nicholas Marty19-Jul-13 2:07
professionalNicholas Marty19-Jul-13 2:07 
AnswerRe: Generic collections Pin
Keith Barrow19-Jul-13 1:04
professionalKeith Barrow19-Jul-13 1:04 
GeneralRe: Generic collections Pin
Nicholas Marty19-Jul-13 2:09
professionalNicholas Marty19-Jul-13 2:09 
GeneralRe: Generic collections Pin
Keith Barrow19-Jul-13 2:33
professionalKeith Barrow19-Jul-13 2:33 
AnswerRe: Generic collections Pin
Richard Deeming19-Jul-13 3:22
mveRichard Deeming19-Jul-13 3:22 
AnswerRe: Generic collections Pin
BillWoodruff21-Jul-13 0:57
professionalBillWoodruff21-Jul-13 0:57 
I really like (and upvoted) your question; it was fun to see what I could implement quickly, and I'm going to post the code here before I read the other responses on the thread, since I feel I can learn something from knowing whether or not the code I post actually meets your requirements, and, possibly, learning from other people's responses.

I wonder if I have not just duplicated the functionality of a Dictionary<K, V> ? Also, I suspect Linq queries could be used in some way, efficiently here, rather than, as I did, creating two Lists in the class (memory cost ?).

Disclaimer: I've tested only with KeyValuePair<string, double> since that's what you used. Testing with instances of a custom class as K, or V: that's on my list Smile | :)

Here's the revised class:
C#
public class IndexedKVPList<K, V> : List<KeyValuePair<K, V>>
{
    private readonly List<K> keyList = new List<K>();
    private readonly List<V> valueList = new List<V>();

    // modified to use 'new
    new public void Add(KeyValuePair<K, V> newKVP)
    {
        keyList.Add(newKVP.Key);
        valueList.Add(newKVP.Value);
        base.Add(newKVP);
    }

    // new function to Remove KeyValue pair
    new public void Remove(KeyValuePair<K, V> killKVP)
    {
        if(! this.Contains(killKVP)) throw new Exception("error in Remove in IndexedKVPPair");

        keyList.Remove(killKVP.Key);
        valueList.Remove(killKVP.Value);
        base.Remove(killKVP);
    }

    public KeyValuePair<K, V> GetKVPByValue(V theValue)
    {
        return this[valueList.IndexOf(theValue)];
    }

    public K GetKeyByValue(V theValue)
    {
        return this[valueList.IndexOf(theValue)].Key;
    }

    // new function to get the index by KeyValue pair
    public int GetIndexByKVP(KeyValuePair<K, V> theKVPPair)
    {
        return this.IndexOf(theKVPPair);
    }

    // new function to get the index by Value
    // what happens if there are multiple identical values ?
    public int GetIndexByValue(V theValue)
    {
        return (valueList.IndexOf(theValue));
    }

    public KeyValuePair<K, V> GetKVPByKey(K theKey)
    {
        return this[keyList.IndexOf(theKey)];
    }

    public V GetValueByKey(K theKey)
    {
        return this[keyList.IndexOf(theKey)].Value;
    }

    // new function to get the index by Key
    public int GetIndexByKey(K theKey)
    {
        return (keyList.IndexOf(theKey));
    }
}
Here's the revised test:
XML
private void TestIKVP()
{
    var iKVList = new IndexedKVPList<string, double>();

    for (int i = 0; i < 10; i++)
    {
        iKVList.Add(new KeyValuePair<string, double>("Key: " + i, i));
    }

    var kvp1 = iKVList.GetKVPByKey("Key: 8");
    var kvp2 = iKVList.GetKVPByValue(6);

    Console.WriteLine(kvp1 + " " + kvp2);

    // test Remove function
    iKVList.Remove(iKVList[4]);

    foreach (var theKVP in iKVList)
    {
        Console.WriteLine(theKVP.Key + " Value:" + theKVP.Value.GetType());
    }

    Console.WriteLine(iKVList.GetIndexByKVP(kvp2));
    Console.WriteLine(iKVList.GetIndexByKey("Key: 9"));
    Console.WriteLine(iKVList.GetIndexByValue(3));
}


~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal


modified 22-Jul-13 7:06am.

GeneralRe: Generic collections Pin
PozzaVecia21-Jul-13 23:05
PozzaVecia21-Jul-13 23:05 
GeneralRe: Generic collections Pin
BillWoodruff22-Jul-13 0:44
professionalBillWoodruff22-Jul-13 0:44 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 1:14
PozzaVecia22-Jul-13 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.