Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've written some code here
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tests
{
    public class KeyValueStatusGroup<TKey, TValue, TStatus>
    {
        public TKey Key;
        public ValueStatusPair<TValue, TStatus> ValueStatusPair = new ValueStatusPair<TValue, TStatus>(); 
    }

    public class ValueStatusPair<TValue, TStatus>
    {
        public TValue Value;
        public TStatus Status;
    }

    public class TriArray<TKey, TValue, TStatus>
    {
        private List<KeyValueStatusGroup<TKey, TValue, TStatus>> list = new List<KeyValueStatusGroup<TKey, TValue, TStatus>>();

        public ValueStatusPair<TValue, TStatus> this[TKey key]
        {
            get
            {
                foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
                {
                    if (keyValueStatusGroup.Key == key)
                    {
                        return keyValueStatusGroup.ValueStatusPair;
                    }
                }
            }
            set
            {
                foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
                {
                    if (keyValueStatusGroup.Key == key)
                    {
                        keyValueStatusGroup.ValueStatusPair = value;
                    }
                }
            }
        }
    }
}

I think it should work fine but I'm getting an error that says:
Operator '==' cannot be applied to operands of type 'TKey' and 'TKey'

What is the problem and how can I fix it???

Thanks Jake
Posted

This happens because KeyValuePair<tkey,> does not define a custom == operator and is not included in the predefined list of value types that can use it.
Refer:http://msdn.microsoft.com/en-us/library/53k8ybth%28v=VS.100%29.aspx[^]

Quote:
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise.


Your best bet for an equality check in this case, because this is not a struct you have control over, is to call default(KeyValuePair<tkey,tvalue>).Equals(pair) instead.
 
Share this answer
 
Comments
BillWoodruff 13-Jan-14 3:50am    
Voted 5. I can't understand why anyone would one-vote this answer !
Gitanjali Singh 13-Jan-14 3:55am    
Thanks BillWoodruff!
TKey, TValue and TStatus - as you use them are NOT real types!!!
This is a kind of notation to declare placeholders for generic types (http://msdn.microsoft.com/en-us/library/512aeb7t.aspx[^]).
When you use your new type KeyValueStatusGroup (new) you have to declare the de-facto types instead of TKey, TValue and TStatus...
 
Share this answer
 
If you change the ValueStatusPair Property to:
C#
public ValueStatusPair<TValue, TStatus> this[TKey key]
{
    get
    {
        foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
        {
            // use reference equality, not value equality
            if (keyValueStatusGroup.Equals(key))
            {
                return keyValueStatusGroup.ValueStatusPair;
            }
        }
         
        // all paths must have a return value !
        return null;
    }
    set
    {
        foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
        {
            // use reference equality, not value equality
            if (keyValueStatusGroup.Equals(key))
            {
                keyValueStatusGroup.ValueStatusPair = value;
            }
        }
    }
}
It will compile, but I haven't had time to test your code, or grok its design.

My impression is you are implementing a generic Tuple with three elements of certain generic types.
 
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