-1

In a class constructor I have a instantiate a list containing properties:

public MDInstrument() : base()
{
    // bidss = new TickData[] {Bid0};
    Bids = new List<TickData> { Bid0, Bid1, Bid2, Bid3, Bid4, Bid5, Bid6, Bid7, Bid8, Bid9, Bid10, Bid0, Bid11, Bid13, Bid14, Bid15, Bid6, Bid17, Bid18, Bid19};
    Offers = new List<TickData> { Ask0, Ask1, Ask2, Ask3, Ask4, Ask5, Ask6, Ask7, Ask8, Ask9, Ask10, Ask0, Ask11, Ask13, Ask14, Ask15, Ask6, Ask17, Ask18, Ask19};
}

A method in the class updates the object in the list but why is the object always null ?

enter image description here

I must be missing something

enter image description here

GKonheiser
  • 61
  • 1
  • 10
  • 2
    Please post a [mcve]. – nvoigt Nov 23 '17 at 12:47
  • 1
    You are setting a property to the value of the same property, and it is null to start with. What else would you expect? In general, I would advice you to _seriously_ rethink your strategy of having 20 separate properties, as well as a list containing all of them. Just the list would do, unless you are the last dev on earth getting paid per LoC. – oerkelens Nov 23 '17 at 12:50
  • 2
    Obviously you never set `Bid1` at any point. Anyway: why do you even *have* 20 (!!) properties with an index? Why not just a list or an array? – MakePeaceGreatAgain Nov 23 '17 at 12:50
  • You claim "A method in the class updates the object in the list but the object is always null ?" but you show a screenshot hat shows that Bid[0] is **not** null. Please explain what your problem is, because "the object is always null" is clearly not true. – oerkelens Nov 23 '17 at 12:52
  • I was using 20 individual properties as I want to use INotifyPropertyChanged to change values in a gui. ObservableCollection doesnt fire when a value is changed. – GKonheiser Nov 23 '17 at 12:59
  • Perhaps `BindingList` then? https://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop – mjwills Nov 23 '17 at 13:04
  • You can use [`ObservableCollection`](https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx) in wpf. – Sinatr Nov 23 '17 at 13:15

1 Answers1

0

Your problem is that to begin with Bid{x} and Ask{x} have not been instantiated, i.e. they're null, and then you store a reference to those values, and of course the reference is null. When you then later on update Bid0 (for example), then that reference is updated, but nothing can know that this is intended to be stored within your set.

Suggest that you change your list to be an array of a fixed known size (here, 20) which will be all nulls to begin with. Then change your getter/setter accessors for the individual Bid items to actually the array internally. Then you also don't need all of those separate Bid{x}/Ask{x} variables.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • I didn't say it was bad for your example. However it's always a good idea when you are going to have more than one thing with the same 'role' to put them in a collection. Then if you ever want to change the number to 10, or 25, or 1000, then you have one number to change and it all just works. – LordWilmore Nov 23 '17 at 14:49