0
var maxHeight = draw._shapes.Aggregate((agg, next) =>next.height > agg.height ? next : agg);
if (draw._shapes.Count == 0)
    trackBar_Size.Maximum = 484;
else
{
    foreach (float heights in maxHeight)
    {
        if (heights < 412)
        {
            trackBar_Size.Maximum = 484;
        }
        else if (heights > 412)
        {
            trackBar_Size.Maximum = 415;
        }
    }
}

Error 3 foreach statement cannot operate on variables of type 'sCreator.Shape' because 'sCreator.Shape' does not contain a public definition for 'GetEnumerator'

I got this error on the var maxHeight statement. So how do I fix this error and use the LINQ result as a float value?

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • 2
    `Aggregate` doesn't return collection, it returns scalar value. See also: https://stackoverflow.com/questions/7105505/linq-aggregate-algorithm-explained. – Tetsuya Yamamoto Oct 11 '18 at 06:00

2 Answers2

5

That's because Aggregate method returned single value (from what I can see, the greatest value in _shapes).

Simply, try writing maxHeight.GetEnumerator() to see, that compiler would complain. In order to use foreach loop you need to have collection (which have iterator).

Or, try writing maxHeight.GetType() (or maxHeight.ToString()) to inspect, what it really is.

Or just inspect the variable in debug mode, setting breakpoints in appropriate places.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
1
var maxHeight = draw._shapes.Aggregate((agg, next) => next.height > agg.height ? next : agg);
            if (maxHeight.height > 412)
            {
                trackBar_Size.Maximum = 412;
            }
            else if (maxHeight.height < 412)
            {
                trackBar_Size.Maximum = 484;
            }

This code will get your desired Height as a float value.

HEWhoDoesn'tKnow
  • 347
  • 3
  • 14