Home All Groups Group Topic Archive Search About
Author
12 Dec 2008 4:42 PM
shapper
Hello,

I am creating a validation class and I defined a few rules. One is the
following:

    public static bool Equal<T>(T value, T compare) where T : struct,
IComparable<T> {
      return value.CompareTo(compare) == 0;
    }

I use this rule to compare two values: strings, ints, doubles, ...

However, when I use:
Rule.Equal("a", "a")

I get the error:
The type 'string' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method
'MyApp.Validation.Rule.Equal<T>(T, T)'

What am I doing wrong?

Can't I create a general rule like this?

Thanks,
Miguel

Author
12 Dec 2008 4:47 PM
Peter Morris
String does not count as a struct.



Are all your drivers up to date? click for free checkup

Author
12 Dec 2008 5:07 PM
shapper
On Dec 12, 4:47 pm, "Peter Morris" <mrpmorri...@SPAMgmail.com> wrote:
> String does not count as a struct.
>
> --
> Pete
> ====http://mrpmorris.blogspot.comhttp://www.capableobjects.com

Ok I just removed it ...

Thanks,
Miguel
Author
12 Dec 2008 5:06 PM
Nicholas Paldino [.NET/C# MVP]
Miguel,

    As Peter says, you can't, because string is a reference type and you
have a constraint on struct.  Furthermore, you can't have constraints on
generics which are based on the union (using an OR operation between the
constraints), rather, only an intersection (AND operation on the
constratints).

    That being said, you should just use the EqualityComparer<T> class,
calling the static Default property, and then calling Equals on the type
returned, like so:

bool equal = EqualityComparer<string>.Default.Equals(value, compare);

    Hope this helps.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"shapper" <mdmo***@gmail.com> wrote in message
news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
> Hello,
>
> I am creating a validation class and I defined a few rules. One is the
> following:
>
>    public static bool Equal<T>(T value, T compare) where T : struct,
> IComparable<T> {
>      return value.CompareTo(compare) == 0;
>    }
>
> I use this rule to compare two values: strings, ints, doubles, ...
>
> However, when I use:
> Rule.Equal("a", "a")
>
> I get the error:
> The type 'string' must be a non-nullable value type in order to use it
> as parameter 'T' in the generic type or method
> 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> What am I doing wrong?
>
> Can't I create a general rule like this?
>
> Thanks,
> Miguel
Author
12 Dec 2008 5:31 PM
shapper
On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> Miguel,
>
>     As Peter says, you can't, because string is a reference type and you
> have a constraint on struct.  Furthermore, you can't have constraints on
> generics which are based on the union (using an OR operation between the
> constraints), rather, only an intersection (AND operation on the
> constratints).
>
>     That being said, you should just use the EqualityComparer<T> class,
> calling the static Default property, and then calling Equals on the type
> returned, like so:
>
> bool equal = EqualityComparer<string>.Default.Equals(value, compare);
>
>     Hope this helps.
>
> --
>           - Nicholas Paldino [.NET/C# MVP]
>           - m...@spam.guard.caspershouse.com
>
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
>
> > Hello,
>
> > I am creating a validation class and I defined a few rules. One is the
> > following:
>
> >    public static bool Equal<T>(T value, T compare) where T : struct,
> > IComparable<T> {
> >      return value.CompareTo(compare) == 0;
> >    }
>
> > I use this rule to compare two values: strings, ints, doubles, ...
>
> > However, when I use:
> > Rule.Equal("a", "a")
>
> > I get the error:
> > The type 'string' must be a non-nullable value type in order to use it
> > as parameter 'T' in the generic type or method
> > 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> > What am I doing wrong?
>
> > Can't I create a general rule like this?
>
> > Thanks,
> > Miguel

I am a little bit confused about this. I have the following:

    public static bool Equal<T>(T value, T compare) where T :
IComparable<T> {
      return value.CompareTo(compare) == 0;
    }

It compiles. Do you mean I need to create a equal method just for
string?

Sorry, but I am not understanding your suggestion.

Thanks,
Miguel
Author
12 Dec 2008 5:48 PM
Nicholas Paldino [.NET/C# MVP]
shapper,

    No, you don't.  Just removing the struct constraint was enough, but mind
you, this will compile against any type that implements IComparable<T>.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

"shapper" <mdmo***@gmail.com> wrote in message
news:3fc4cb76-11e1-4c9c-b5ba-db9c2a1f9651@k1g2000prb.googlegroups.com...
On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> Miguel,
>
> As Peter says, you can't, because string is a reference type and you
> have a constraint on struct. Furthermore, you can't have constraints on
> generics which are based on the union (using an OR operation between the
> constraints), rather, only an intersection (AND operation on the
> constratints).
>
> That being said, you should just use the EqualityComparer<T> class,
> calling the static Default property, and then calling Equals on the type
> returned, like so:
>
> bool equal = EqualityComparer<string>.Default.Equals(value, compare);
>
> Hope this helps.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - m...@spam.guard.caspershouse.com
>
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
>
> > Hello,
>
> > I am creating a validation class and I defined a few rules. One is the
> > following:
>
> > public static bool Equal<T>(T value, T compare) where T : struct,
> > IComparable<T> {
> > return value.CompareTo(compare) == 0;
> > }
>
> > I use this rule to compare two values: strings, ints, doubles, ...
>
> > However, when I use:
> > Rule.Equal("a", "a")
>
> > I get the error:
> > The type 'string' must be a non-nullable value type in order to use it
> > as parameter 'T' in the generic type or method
> > 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> > What am I doing wrong?
>
> > Can't I create a general rule like this?
>
> > Thanks,
> > Miguel

I am a little bit confused about this. I have the following:

    public static bool Equal<T>(T value, T compare) where T :
IComparable<T> {
      return value.CompareTo(compare) == 0;
    }

It compiles. Do you mean I need to create a equal method just for
string?

Sorry, but I am not understanding your suggestion.

Thanks,
Miguel
Author
12 Dec 2008 6:06 PM
shapper
On Dec 12, 5:48 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> shapper,
>
>     No, you don't.  Just removing the struct constraint was enough, but mind
> you, this will compile against any type that implements IComparable<T>.
>
> --
>           - Nicholas Paldino [.NET/C# MVP]
>           - m...@spam.guard.caspershouse.com
>
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:3fc4cb76-11e1-4c9c-b5ba-db9c2a1f9651@k1g2000prb.googlegroups.com...
> On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
>
>
>
> <m...@spam.guard.caspershouse.com> wrote:
> > Miguel,
>
> > As Peter says, you can't, because string is a reference type and you
> > have a constraint on struct. Furthermore, you can't have constraints on
> > generics which are based on the union (using an OR operation between the
> > constraints), rather, only an intersection (AND operation on the
> > constratints).
>
> > That being said, you should just use the EqualityComparer<T> class,
> > calling the static Default property, and then calling Equals on the type
> > returned, like so:
>
> > bool equal = EqualityComparer<string>.Default.Equals(value, compare);
>
> > Hope this helps.
>
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - m...@spam.guard.caspershouse.com
>
> > "shapper" <mdmo***@gmail.com> wrote in message
>
> >news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
>
> > > Hello,
>
> > > I am creating a validation class and I defined a few rules. One is the
> > > following:
>
> > > public static bool Equal<T>(T value, T compare) where T : struct,
> > > IComparable<T> {
> > > return value.CompareTo(compare) == 0;
> > > }
>
> > > I use this rule to compare two values: strings, ints, doubles, ...
>
> > > However, when I use:
> > > Rule.Equal("a", "a")
>
> > > I get the error:
> > > The type 'string' must be a non-nullable value type in order to use it
> > > as parameter 'T' in the generic type or method
> > > 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> > > What am I doing wrong?
>
> > > Can't I create a general rule like this?
>
> > > Thanks,
> > > Miguel
>
> I am a little bit confused about this. I have the following:
>
>     public static bool Equal<T>(T value, T compare) where T :
> IComparable<T> {
>       return value.CompareTo(compare) == 0;
>     }
>
> It compiles. Do you mean I need to create a equal method just for
> string?
>
> Sorry, but I am not understanding your suggestion.
>
> Thanks,
> Miguel

Ok I understand that since I am checking only if two objects are equal
I should use EqualityComparer:

    public static bool Equal<T>(T value, T compare) where T :
IEqualityComparer<T> {
      return EqualityComparer<T>.Default.Equals(value, compare);
    } // Equal

But with this I get the same problem, I had before, when I use:

Rule.Equal("a", "a")

The type 'string' cannot be used as type parameter 'T' in the generic
type or method 'MyApp.Validation.Rule.Equal<T>(T, T)'. There is no
implicit reference conversion from 'string' to
'System.Collections.Generic.IEqualityComparer<string>'.

But if with it works with IComparable (without struct) why not with
IEqualityComparer?

Could you, please, tell me what is wrong on my code?

Thanks,
Miguel
Author
12 Dec 2008 6:11 PM
Nicholas Paldino [.NET/C# MVP]
shapper,

    The compiler is telling you. System.String doesn't implement
IEqualityComparer<string>.  It does implement IComparable<T>.

    You don't need ANY constraints on your Equals method.  Just remove them
and it will work for any type, and it will use the EqualityComparer for that
type.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

"shapper" <mdmo***@gmail.com> wrote in message
news:10d71865-63f6-42a1-b81f-1447444e63fb@r15g2000prd.googlegroups.com...
On Dec 12, 5:48 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> shapper,
>
> No, you don't. Just removing the struct constraint was enough, but mind
> you, this will compile against any type that implements IComparable<T>.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - m...@spam.guard.caspershouse.com
>
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:3fc4cb76-11e1-4c9c-b5ba-db9c2a1f9651@k1g2000prb.googlegroups.com...
> On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
>
>
>
> <m...@spam.guard.caspershouse.com> wrote:
> > Miguel,
>
> > As Peter says, you can't, because string is a reference type and you
> > have a constraint on struct. Furthermore, you can't have constraints on
> > generics which are based on the union (using an OR operation between the
> > constraints), rather, only an intersection (AND operation on the
> > constratints).
>
> > That being said, you should just use the EqualityComparer<T> class,
> > calling the static Default property, and then calling Equals on the type
> > returned, like so:
>
> > bool equal = EqualityComparer<string>.Default.Equals(value, compare);
>
> > Hope this helps.
>
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - m...@spam.guard.caspershouse.com
>
> > "shapper" <mdmo***@gmail.com> wrote in message
>
> >news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
>
> > > Hello,
>
> > > I am creating a validation class and I defined a few rules. One is the
> > > following:
>
> > > public static bool Equal<T>(T value, T compare) where T : struct,
> > > IComparable<T> {
> > > return value.CompareTo(compare) == 0;
> > > }
>
> > > I use this rule to compare two values: strings, ints, doubles, ...
>
> > > However, when I use:
> > > Rule.Equal("a", "a")
>
> > > I get the error:
> > > The type 'string' must be a non-nullable value type in order to use it
> > > as parameter 'T' in the generic type or method
> > > 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> > > What am I doing wrong?
>
> > > Can't I create a general rule like this?
>
> > > Thanks,
> > > Miguel
>
> I am a little bit confused about this. I have the following:
>
> public static bool Equal<T>(T value, T compare) where T :
> IComparable<T> {
> return value.CompareTo(compare) == 0;
> }
>
> It compiles. Do you mean I need to create a equal method just for
> string?
>
> Sorry, but I am not understanding your suggestion.
>
> Thanks,
> Miguel

Ok I understand that since I am checking only if two objects are equal
I should use EqualityComparer:

    public static bool Equal<T>(T value, T compare) where T :
IEqualityComparer<T> {
      return EqualityComparer<T>.Default.Equals(value, compare);
    } // Equal

But with this I get the same problem, I had before, when I use:

Rule.Equal("a", "a")

The type 'string' cannot be used as type parameter 'T' in the generic
type or method 'MyApp.Validation.Rule.Equal<T>(T, T)'. There is no
implicit reference conversion from 'string' to
'System.Collections.Generic.IEqualityComparer<string>'.

But if with it works with IComparable (without struct) why not with
IEqualityComparer?

Could you, please, tell me what is wrong on my code?

Thanks,
Miguel
Author
12 Dec 2008 5:34 PM
shapper
On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> Miguel,
>
>     As Peter says, you can't, because string is a reference type and you
> have a constraint on struct.  Furthermore, you can't have constraints on
> generics which are based on the union (using an OR operation between the
> constraints), rather, only an intersection (AND operation on the
> constratints).
>
>     That being said, you should just use the EqualityComparer<T> class,
> calling the static Default property, and then calling Equals on the type
> returned, like so:
>
> bool equal = EqualityComparer<string>.Default.Equals(value, compare);
>
>     Hope this helps.
>
> --
>           - Nicholas Paldino [.NET/C# MVP]
>           - m...@spam.guard.caspershouse.com
>
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
>
> > Hello,
>
> > I am creating a validation class and I defined a few rules. One is the
> > following:
>
> >    public static bool Equal<T>(T value, T compare) where T : struct,
> > IComparable<T> {
> >      return value.CompareTo(compare) == 0;
> >    }
>
> > I use this rule to compare two values: strings, ints, doubles, ...
>
> > However, when I use:
> > Rule.Equal("a", "a")
>
> > I get the error:
> > The type 'string' must be a non-nullable value type in order to use it
> > as parameter 'T' in the generic type or method
> > 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> > What am I doing wrong?
>
> > Can't I create a general rule like this?
>
> > Thanks,
> > Miguel

You mean this?

    public static bool Equal<T>(T value, T compare) where T :
IEqualityComparer<T> {
      return EqualityComparer<T>.Default.Equals(value, compare);
    } // Equal
Author
12 Dec 2008 5:47 PM
Nicholas Paldino [.NET/C# MVP]
shapper,

    Yes, but you don't need the constraint on T.  EqualityComparer will work
with any type.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

"shapper" <mdmo***@gmail.com> wrote in message
news:9fd50e08-5b65-4b2a-a0bf-257258ca115a@o40g2000prn.googlegroups.com...
On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> Miguel,
>
> As Peter says, you can't, because string is a reference type and you
> have a constraint on struct. Furthermore, you can't have constraints on
> generics which are based on the union (using an OR operation between the
> constraints), rather, only an intersection (AND operation on the
> constratints).
>
> That being said, you should just use the EqualityComparer<T> class,
> calling the static Default property, and then calling Equals on the type
> returned, like so:
>
> bool equal = EqualityComparer<string>.Default.Equals(value, compare);
>
> Hope this helps.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - m...@spam.guard.caspershouse.com
>
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:bbc1bd31-785c-4b0d-8449-173b527d1c2a@p2g2000prn.googlegroups.com...
>
> > Hello,
>
> > I am creating a validation class and I defined a few rules. One is the
> > following:
>
> > public static bool Equal<T>(T value, T compare) where T : struct,
> > IComparable<T> {
> > return value.CompareTo(compare) == 0;
> > }
>
> > I use this rule to compare two values: strings, ints, doubles, ...
>
> > However, when I use:
> > Rule.Equal("a", "a")
>
> > I get the error:
> > The type 'string' must be a non-nullable value type in order to use it
> > as parameter 'T' in the generic type or method
> > 'MyApp.Validation.Rule.Equal<T>(T, T)'
>
> > What am I doing wrong?
>
> > Can't I create a general rule like this?
>
> > Thanks,
> > Miguel

You mean this?

    public static bool Equal<T>(T value, T compare) where T :
IEqualityComparer<T> {
      return EqualityComparer<T>.Default.Equals(value, compare);
    } // Equal

Bookmark and Share