Home > front end >  AddRange adding to which of my collections?
AddRange adding to which of my collections?

Time:08-16

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();
                }
            }
        }
    }
}

CodePudding user response:

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.

CodePudding user response:

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();

CodePudding user response:

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

Collection<T> have protected Items property;

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

So this.AddRange() adds items in Items propery

  • Related