1

I'm not sure I understand the cause of this error or what to do about it.

I have a generic class.

public class RadioCommandCollection<T> : List<RadioCommand<T>> where T : struct, IComparable<T>
{
    /// <summary>
    /// Sets the value associated with the selected menu item to button.
    /// </summary>
    public void SetValue(T value)
    {
        foreach (RadioCommand<T> command in this)
        {
            command.SetIsSelected(command.Value == value);
        }
    }
}

And the line in the foreach block gives me an error:

CS0019 Operator '==' cannot be applied to operands of type 'T' and 'T'

Note that RadioCommand<T>.Value is of type T. It seems like I'm only defining T once here. I don't get the conflict.

I added the IComparable<T> constraint but that didn't help. Note that I'd like to restrict T to an enum, but that's not a valid constraint.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • You should use `Comparer.Default` to compare the values. `Enum` is valid generic constraint starting from C# 7.3 – Pavel Anikhouski Apr 27 '20 at 16:53
  • 1
    Does this answer your question? [Can't operator == be applied to generic types in C#?](https://stackoverflow.com/questions/390900/cant-operator-be-applied-to-generic-types-in-c) There are numbers of duplicates of this question on this site – Pavel Anikhouski Apr 27 '20 at 16:55
  • `T` is expressed only for `RadioCommandCollection`, not `RadioCommand`, meaning in the context of `RadioCommand` (e.g., for its `Value` property), `T` could be anything that obeys whatever constraints are on `T` *for `RadioCommand`* To remedy, cast `command.Value` as `T`... I think. – Heretic Monkey Apr 27 '20 at 16:59
  • @HereticMonkey: It's the same type. The types all seem explicit to me, and all of the same definition of `T`. – Jonathan Wood Apr 27 '20 at 17:03
  • @PavelAnikhouski: Do'h! You're right. I was using `enum`. `Enum` *is* a valid constraint. – Jonathan Wood Apr 27 '20 at 17:04
  • @PavelAnikhouski: I would have thought that using `Enum` as the constraint would signify how the two could be compared. But apparently not. – Jonathan Wood Apr 27 '20 at 17:05
  • @PavelAnikhouski: The selected answer in the other question says to be more explicit. I don't see how I can do that in my case. And the answer with the most upvotes states that *it will only work when T is constrained to be a reference type*. I had that, and now I'm using `Enum`. So I'm not sure how that helps. – Jonathan Wood Apr 27 '20 at 17:11
  • @JonathanWood there is another duplicate [How to compare values of generic types?](https://stackoverflow.com/questions/6480577/how-to-compare-values-of-generic-types), which can be found after a few a seconds of googling, as well as the similar questions, appearing here every one/two weeks – Pavel Anikhouski Apr 27 '20 at 17:16
  • 1
    @PavelAnikhouski: No need to insult me. I spent some time looking before I posted. None of the ones I found were compatible with my particular case. – Jonathan Wood Apr 27 '20 at 17:17

1 Answers1

0

Simpliest way

public class RadioCommandCollection<T> : List<RadioCommand<T>> where T : struct
{
    /// <summary>
    /// Sets the value associated with the selected menu item to button.
    /// </summary>
    public void SetValue(T value)
    {
        foreach (RadioCommand<T> command in this)
        {
            command.SetIsSelected(command.Value.Equal(value));
        }
    }
}

Other way

public class RadioCommandCollection<T> : List<RadioCommand<T>> where T : class
{
    /// <summary>
    /// Sets the value associated with the selected menu item to button.
    /// </summary>
    public void SetValue(T value)
    {
        foreach (RadioCommand<T> command in this)
        {
            command.SetIsSelected(command.Value == value);
        }
    }
}
Mertuarez
  • 901
  • 7
  • 24