Search code examples
c#componentart

List of ObservableCollection items in C#


Don't know if my approach is correct, but I just want to understand this:

I have a ColorStopCollection of colors, this is a class from Componentart and this is the definition

    public sealed class ColorStopCollection : ObservableCollection<ColorStop>
    {
        public ColorStopCollection();
        public ColorStopMappingMode MappingMode { get; set; }
        public Palette Palette { get; set; }
        public ColorStopTransition Transition { get; set; }
        public Color GetColor(double value);
        public Color GetColor(double value, ColorStopTransition transition);
        public Color[] GetColorRange(int count);
        public Color[] GetColorRange(int count, ColorStopTransition transition);
        public Color[] GetColorRange(double minValue, double maxValue, int count, ColorStopTransition transition);
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e);
        public GradientStopCollection ToGradientStopCollection();
        public GradientStopCollection ToGradientStopCollection(double minValue, double maxValue);
        public ColorStop[] ToSortedArray();
    }

So I made a Collection of colors, like this (the Green variable is a ColorStopCollection of a few shades of green

public static ColorStopCollection Green= new ColorStopCollection() 
    { new ColorStop{Color=(Color)new ColorConverter().ConvertFrom("#FF3F5D1C"), Offset=0},
      new ColorStop{Color=(Color)new ColorConverter().ConvertFrom("#FF4B7021"), Offset=20},
      new ColorStop{Color=(Color)new ColorConverter().ConvertFrom("#FF588227"), Offset=40},
      new ColorStop{Color=(Color)new ColorConverter().ConvertFrom("#FF658D37"), Offset=60},
      new ColorStop{Color=(Color)new ColorConverter().ConvertFrom("#FF85A562"), Offset=80}
    };

I have ten of these variables, I need to list them so I made this statement:

public static List<ColorStopCollection> AllColors = new List<ColorStopCollection>(){
        Green,
        Blue,
        Purple,
        Lilac,
        Orange,
        ...
    };

My problem is when I try to get an ColorStopCollection item of my List AllColors

Name                        Value   Type
GlobalColors.AllColors[0]   null    ComponentArt.Win.DataVisualization.Gauges.ColorStopCollection

I was hoping to get a ColorStopCollection element (The collection of green in this case) but I get null, am I missing something?


Solution

  • It looks like you initialized AllColors before Green, so Green was null when you passed it.

    Move each field initializer after the fields that it references.