Count

S

shapper

Hello,

I am trying to create a validator rule that determines if the number
of elements in an Array or List is inferior to a certain number,
Maximum
I am not sure if I can use the same rule for arrays and lists.

I was trying to use GetUpperBound and work from one that I already
use:

public static Boolean Range<T>(T value, T? minimum, T? maximum)
where T : struct, IComparable<T> {
// No range
if (!minimum.HasValue && !maximum.HasValue) return true;
// Minimum range
if (minimum.HasValue && value.CompareTo(minimum.Value) < 0)
return false;
// Maximum range
if (maximum.HasValue && value.CompareTo(maximum.Value) > 0)
return false;
// In range
return true;
} // Range

In this case there would be no minimum.

Is this possible?

Thanks,
Miguel
 
S

shapper

Hello,

I am trying to create a validator rule that determines if the number
of elements in an Array or List is inferior to a certain number,
Maximum
I am not sure if I can use the same rule for arrays and lists.

I was trying to use GetUpperBound and work from one that I already
use:

    public static Boolean Range<T>(T value, T? minimum, T? maximum)
where T : struct, IComparable<T> {
      // No range
      if (!minimum.HasValue && !maximum.HasValue) return true;
      // Minimum range
      if (minimum.HasValue && value.CompareTo(minimum.Value) < 0)
return false;
      // Maximum range
      if (maximum.HasValue && value.CompareTo(maximum.Value) > 0)
return false;
      // In range
      return true;
    } // Range

In this case there would be no minimum.

Is this possible?

Thanks,
Miguel

I was trying something like:

public static Boolean Count<T>(T value, T? maximum) where T : struct,
IEnumerable<T> {

But I get an error:
Type parameter 'T' inherits conflicting constraints 'IEnumerable<T>'
and 'object'

And can I apply the concept to both an array or to a list?
 
S

shapper

I was trying something like:

public static Boolean Count<T>(T value, T? maximum) where T : struct,
IEnumerable<T> {

But I get an error:
Type parameter 'T' inherits conflicting constraints 'IEnumerable<T>'
and 'object'

And can I apply the concept to both an array or to a list?

I was missing System.Collections but still have the problem. I have
the following:

public static Boolean Count<T>(T value, T? maximum) where T :
struct, IEnumerable<T> {

return (maximum.HasValue && value. ????? > 0) true : false;

}

In this case value does not have a Count property. How can I make this
work?

Thanks,
Miguel
 
G

Göran Andersson

shapper said:
Hello,

I am trying to create a validator rule that determines if the number
of elements in an Array or List is inferior to a certain number,
Maximum
I am not sure if I can use the same rule for arrays and lists.

I was trying to use GetUpperBound and work from one that I already
use:

public static Boolean Range<T>(T value, T? minimum, T? maximum)
where T : struct, IComparable<T> {
// No range
if (!minimum.HasValue && !maximum.HasValue) return true;
// Minimum range
if (minimum.HasValue && value.CompareTo(minimum.Value) < 0)
return false;
// Maximum range
if (maximum.HasValue && value.CompareTo(maximum.Value) > 0)
return false;
// In range
return true;
} // Range

In this case there would be no minimum.

Is this possible?

Thanks,
Miguel

Make the method take an IEnumerable<T>, then it will support many
different collections including arrays and lists.

You just have to loop through the items and compare them, something like
this:

public static bool AllBelow(IEnumerable<T> values, T maximum) where T :
IComparable<T> {
foreach (T value in values) {
if (value.CompareTo(maximum) >= 0) return false;
}
return true;
}
 
S

shapper

Make the method take an IEnumerable<T>, then it will support many
different collections including arrays and lists.

You just have to loop through the items and compare them, something like
this:

public static bool AllBelow(IEnumerable<T> values, T maximum) where T :
IComparable<T> {
    foreach (T value in values) {
       if (value.CompareTo(maximum) >= 0) return false;
    }
    return true;

}

Hi Peter and Goram, I am not trying to compare the values to maximum.

I am just trying to Count if the number of elements in List/Array is
less than maximum:

public static Boolean Count<T>(T value, T? maximum) where T :
struct, IEnumerable<T> {
return (maximum.HasValue && value. ????? > 0) true : false;
}

So if I pass Maximum = 10 and the Array/List has:
- 5 elements then it return true;
- 10 elements then it will return true;
- 20 elements then it will return false;

Peter, I have my code above. I am not just sure of how to get the
Count of value. Do you understand?

Thanks,
Miguel
 
P

Pavel Minaev

I am trying to create a validator rule that determines if the number
of elements in an Array or List is inferior to a certain number,
Maximum
I am not sure if I can use the same rule for arrays and lists.

Given that any array type T[] always implements interface IList<T>, I
don't see why would that be a problem. Just deal with lists and use
Count, arrays fill fit right in on their own.
I was trying something like:

public static Boolean Count<T>(T value, T? maximum) where T : struct,
IEnumerable<T> {

This is a very strange constraint you have here. You're essentially
asking type T to be an IEnumerable of itself! Is that really what you
want to do?
 
G

Göran Andersson

shapper said:
Hi Peter and Goram, I am not trying to compare the values to maximum.

I am just trying to Count if the number of elements in List/Array is
less than maximum:

public static Boolean Count<T>(T value, T? maximum) where T :
struct, IEnumerable<T> {
return (maximum.HasValue && value. ????? > 0) true : false;
}

So if I pass Maximum = 10 and the Array/List has:
- 5 elements then it return true;
- 10 elements then it will return true;
- 20 elements then it will return false;

Peter, I have my code above. I am not just sure of how to get the
Count of value. Do you understand?

Thanks,
Miguel

Why make it so complicated then?

For an array:

if (theArray.Length <= maximum) ...

For a list:

if (theList.Count <= maximum) ...

If you want it as a method, don't send a reference to the collection to
it, just send the length. The method has no use for the actual items,
and it doesn't care what type they are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top