-1

I Need to Clear my CSLA ReadOnly List which is of course not possible because CSLA is protecting my ReadOnly Lst. I also cant simply set the List null because that is removing the FilterMethods of my List in the XAML. is there a way to make an own Clear Method in my ReadOnlyListBase ?

Short:

  • ShowGroupPanel in the UI of WPF is false when I set the List on null.

  • I cant clear/remove the List because it has to be ReadOnly.

So is there maybe a way to return a Empty List in the ReadOnlyListBase Object?

dennis schütz
  • 383
  • 4
  • 21
  • 1
    Can you set your variable to refer to a *new* `ReadOnlyList`? – Jon Skeet Mar 18 '14 at 08:19
  • u r right, I only thought in the "csla" way – dennis schütz Mar 18 '14 at 08:26
  • 2
    Baby seals get slapped in the face everytime someone adds a Clear method in ReadOnly collections. Plz think of the seals! – atomaras Mar 23 '14 at 12:35
  • Instead you should internally hold an `List` while you give out an `IReadOnlyList`. By that approach the owner of the list can add, remove, clear the list while each consumer can only read it. – Oliver Jun 24 '22 at 20:01

2 Answers2

1

Replace it with a new ObservableRangeCollection like this:

SelectedDates = new ObservableRangeCollection<DateTime>();
morteng
  • 1,133
  • 2
  • 9
  • 22
Kiriakos41
  • 11
  • 1
  • 2
0

I agree with atomaras, clearing a readonly collection is a bad idea.

But if you really want to do this, you can implement a public method on your collection class that sets the protected IsReadOnly property to false, clears the collection, then sets IsReadOnly to true again.

Not a good idea, but certainly something that will work.

Rockford Lhotka
  • 842
  • 6
  • 9