-1

Can someone explain how I would add the GENERATED notifications inside the C# class constructor, if an initial list is started in the code that is creating the new class. Is Notifications = notificationsPASSEDList only run after the constructor?

//.. get list of notifications to pass
var myClass = new MyClass { Notifications = notificationsPASSEDList }

public class MyClass {

    public List<NotificationsClass> Notifications;

    public MyClass () {
        // .. get list of generated notifications
        Notifications.AddRange(notificationsGENERATEDList)
    }

}
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
SweetTomato
  • 509
  • 1
  • 6
  • 16

2 Answers2

1

It's possible to do what you ask, but it isn't a great idea, because you'd have to modify the Notifications property so that assigning to it performs an add rather than an assignment, which is unconventional and confusing. But here you go:

public class MyClass {

    public List<NotificationsClass> _notifications;

    public MyClass()
    {
        _notifications = notificationsGENERATEDList;
    }

    public Notifications
    {
        get { return _notifications; }
        set { _notifications.AddRange(value); }
    }
}

A better way would be to pass the PASSEDlist in the constructor rather than using initializer syntax:

public class MyClass {

    public List<NotificationsClass> Notifications;

    public MyClass(List<NotificationClass> passedList)
    {
        Notifications = notificationsGENERATEDList;
        Notifications.Add(passedList);
    }
}

Then call it with:

var myClass = new MyClass( notificationsPASSEDList);
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

Well, when the properties are being set you can check if there is anything exist in the list (that is surely added in the constructor) and preserver them:

    private generated = null;
    private List<NotificationsClass> notifications;
    public List<NotificationsClass> Notifications 
    {
        get 
        {
             return notifications;
        }
        set
        {
            if(generated != null)
            {
                notifications = generated ;
                notifications.AddRange(value);
                generated = null
             }
             else
                notifications = value;
        }

    }

public MyClass () {
        generated = notificationsGENERATEDList;
    }

Note that private guaranteed = null; line may seem extra, but it is there to make sure not always assigning a list will actually append it to the list, and will only be added when there is a guaranteed list. otherwise will just assign it.

If you want to always keep the guaranteed list, then you can change this part of the code:

if(guaranteed != null)
{
    notifications = generated ;
    notifications.AddRange(value);
    generated = null;
}
else
    notifications = value;

to this:

if(generated != null)
{
     notifications = generated;
     notifications.AddRange(value);
}
else
    notifications = value;
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171