0

I'm using the .NET System.Windows.Forms.DataVisualization.Charting types and want to assign a MarkerStyle to each Series I create preferably skipping MarkerStyle.None, but getting the next one each time and cycling around to the beginning again.

private MarkerStyle markers = new MarkerStyle();

public Series CreateSeries(string name)
{
    var s = chart1.Series.Add(name);
    s.ChartType = SeriesChartType.Point;
    s.MarkerSize = 7;
    s.MarkerStyle = Cycle(markers ,MarkerStyle.None); // get next marker style
    s.XValueType = ChartValueType.DateTime;
    return s;
}

I tried variations of the following code I found, all of which don't compile:

public static IEnumerable<T>Cycle<T>(IEnumerable<T> iterable,<T> skip)
{
    while (true)
     {
        foreach (T t in iterable)
        {
            if(t == skip)
            continue;
            yield return t;
        }
    }
}

Gives error

CS0411. The type arguments for method 'MyClass.Cycle(System.Collections.Generic.IEnumerable)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Although I have a specific use it would be nice for a generic answer.

Edit: Having looked at the code above more closely and I see it was never going to work outside a foreach loop. Ho hum. Time to write a hack:

private int _markerIter = 0;
private readonly int _markerCount = Enum.GetValues(typeof(MarkerStyle)).Length; 
public MarkerStyle Cycle()
{
    if (_markerIter + 1 == _markerCount)
    {
        _markerIter = 1; //skips MarkerStyle.None which is == 0
    }
    else
    {
        _markerIter++;
    }

    return (MarkerStyle) _markerIter;
}
Simon Heffer
  • 121
  • 1
  • 1
  • 10
  • 1
    There's nothing wrong with your Cycle method - the problem is when you're calling it, your first argument is an instance of MarkerStyle, not an enumerable of MarkerStyle – Martin Ernst May 02 '14 at 15:18
  • Thanks Martin I tried MarkerStyle but that gave a compiler error too. So an enum is not enumerable? – Simon Heffer May 02 '14 at 15:33

1 Answers1

0

Try

   Enum.GetValues(typeof(T))

http://msdn.microsoft.com/en-us/library/system.enum.getvalues%28v=vs.110%29.aspx

apc
  • 5,306
  • 1
  • 17
  • 26