|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Compare ValuesI 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 String does not count as a struct.
On Dec 12, 4:47 pm, "Peter Morris" <mrpmorri...@SPAMgmail.com> wrote: Ok I just removed it ...> String does not count as a struct. > > -- > Pete > ====http://mrpmorris.blogspot.comhttp://www.capableobjects.com Thanks, Miguel 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. -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@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 On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote: Show quoteHide quote > Miguel, I am a little bit confused about this. I have the following:> > 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 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 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 On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"news:3fc4cb76-11e1-4c9c-b5ba-db9c2a1f9651@k1g2000prb.googlegroups.com... <m...@spam.guard.caspershouse.com> wrote: Show quoteHide quote > Miguel, I am a little bit confused about this. I have the following:> > 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 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 On Dec 12, 5:48 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote: Show quoteHide quote > shapper, Ok I understand that since I am checking only if two objects are equal> > 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 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 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 On Dec 12, 5:48 pm, "Nicholas Paldino [.NET/C# MVP]"news:10d71865-63f6-42a1-b81f-1447444e63fb@r15g2000prd.googlegroups.com... <m...@spam.guard.caspershouse.com> wrote: Show quoteHide quote > shapper, Ok I understand that since I am checking only if two objects are equal> > 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 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 On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote: Show quoteHide quote > Miguel, You mean this?> > 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 public static bool Equal<T>(T value, T compare) where T : IEqualityComparer<T> { return EqualityComparer<T>.Default.Equals(value, compare); } // Equal 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 On Dec 12, 5:06 pm, "Nicholas Paldino [.NET/C# MVP]"news:9fd50e08-5b65-4b2a-a0bf-257258ca115a@o40g2000prn.googlegroups.com... <m...@spam.guard.caspershouse.com> wrote: Show quoteHide quote > Miguel, You mean this?> > 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 public static bool Equal<T>(T value, T compare) where T : IEqualityComparer<T> { return EqualityComparer<T>.Default.Equals(value, compare); } // Equal
Other interesting topics
noise reduction in image
LINQ add more where statements Convert doc, xls, pdf, rtf to plain Text (string) - I will pay for .NET Dllimport System AccessViolationExecption Check extension how to detect object reference between objects? Scrolling text for messages. javascript How to manage processes on a shared resource |
|||||||||||||||||||||||