1

Upon looking into the following code I am unable to figure out the this.AddRange(hoteltasks); on line 21.

I want to know to which collection the AddRange method adds the elements of hoteltasks.

public class HotelViewModel : ObservableRangeCollection<RoomViewModel>, INotifyPropertyChanged
{
    // It's a backup variable for storing TaskViewModel objects
    private ObservableRangeCollection<RoomViewModel> hoteltasks = new ObservableRangeCollection<RoomViewModel>();

    public HotelViewModel()
    {
    }

    public HotelViewModel(Hotel hotel, bool expanded = false)
    {
        this.Hotel = hotel;
        this._expanded = expanded;

        foreach (Room task in hotel.Tasks)
        {
            hoteltasks.Add(new RoomViewModel(task));
        }
        if (expanded)
            this.AddRange(hoteltasks);
    }
 
    private bool _expanded;
    public bool Expanded
    {
        get { return _expanded; }
        set
        {
            if (_expanded != value)
            {
                _expanded = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Expanded"));
                  
                if (_expanded)
                {
                    this.AddRange(hoteltasks);
                }
                else
                {
                    this.Clear();
                }
            }
        }
    }
}
Palle Due
  • 5,929
  • 4
  • 17
  • 32

3 Answers3

2

AddRange is called on this, so the contents of hoteltasks are added to this.

this refers to the current instance of HotelViewModel. Since line 21 is in the constructor, this refers to the newly created instance of HotelViewModel. There is also a second occurrence of this.AddRange(hoteltasks); further down the code in the setter of Expanded. There, the current instance is the instance on which you are accessing Expanded. For more info, see What is the meaning of "this" in C#

Although this is an instance of something called "HotelViewModel", which doesn't sound like a collection, it is in fact a collection. This is because it is declared to inherit from ObservableRangeCollection<RoomViewModel>, so it has all the behaviours of a ObservableRangeCollection<RoomViewModel>, such as being able to have RoomViewModels stored in it, and being able to have a range of RoomViewModel added to it.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • What if when I declare another variable of ObservableRangeCollection name as hoteltasksnew then in which variable this.AddRange will add the contents of hoteltasks as now there are two variable one is hoteltasks and hoteltasksnew . – Kettan Sinha Aug 19 '22 at 04:46
  • You didn't seem to have understood my answer. Before you "declare another variable", `this.AddRange(hoteltasks)` ***does not add*** the contents of `hoteltasks` to `hoteltasks`. That would actually throw an exception (but that's another story). `this.AddRange` adds the contents of `hoteltasks` to `this`, which is itself a `ObservableRangeCollection`. Try to convince yourself that `this` is also a `ObservableRangeCollection` by reading my last paragraph. – Sweeper Aug 19 '22 at 04:51
  • Got it...Thanks for you prompt response – Kettan Sinha Aug 19 '22 at 10:10
0

HotelViewModel is a ObservableRangeCollection, which is a List-Type

If you create somewhere a new instance

HotelViewModel myNewModel= new HotelViewModel(hotel,true);

then myNewModel is the List (this.AddRange(hoteltasks);)

Try

var t = myNewModel.ToList();
Chris Berlin
  • 744
  • 6
  • 15
0

ObservableRangeCollection inherits from ObservableCollection, which inherits Collection<T> which implement ICollection interface.

Collection<T> have protected Items property;

https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.items?view=net-6.0

So this.AddRange() adds items in Items propery

Alexandr S
  • 309
  • 4
  • 10