Search code examples
c#instantiation

c# blazor > Can't understand why double instantiation is needed


I use this simple c# code (ok, the result is a little bit silly but it's just to try to solve my problem) to fill a grid (a Syncfusion Blazor datagrid which reads the _monitoringDatas variable to fill out ) :

namespace ligprod.Client.Pages
{
    public partial class Monitoring_ligne
    {
        ...
        private Monitoring Monit;
        private List<Monitoring> _monitoringDatas;
    
        protected override async Task OnInitializedAsync()
       {
           ...
           _monitoringDatas = new List<Monitoring>();
       }
    
       Task ReceiveLidarValue(string arg)
       {          
           _monitoringDatas = new List<Monitoring>();
           Monitoring Monit = new Monitoring()
           {
              IdMonotoring = 1,
              RealProduction = false,
              Machine = "remplisseuse",
              DatePassage = DateTime.Now,
           };

           _monitoringDatas.Add(Monit);       
      }
 }

Why do I have to instantiate _monitoringDatas into the ReceiveLidarValue(string arg) task to make the grid filled whereas it was already instantiated in the OnInitializedAsync() Task ? Yet, the OnInitializedAsync() Task is reached correctly. Indeed, it also contains code to get values from a Hub which works well.

  • ReceiveLidarValue() is called each time a particular message is thrown to a hub from a service which receive mqtt messages coming from an external electronic card which have a sensor for movements detection :

1/ Server side > As soon as the application is started the service runs and waits for messages from the electronic card. 2/ Client side > The web page (shown in my demand above) is started in a browser (manually). The OnInitializedAsync() method is called and so,-monitoringDatas is instantiated. 3/ Server side > Each time a movement is detected by the sensor, a message (with mqtt protocol) is sent by the electronic card and is finally read by the service which then send a particular message into a hub. 4/ Client side > The message into the hub is detected by the web page and the ReceiveLidarValue(string arg) Task is called. 5/ This Task build a new Monitoring object called Monit (which normally contains an Id, a bool value, the name of the machine on which the movement is detected and the date of detection). This "Monit" is added to the datagrid.

So, each time a new movement is detected, the *ReceiveLidarValue(string arg)*Task is launched and a new Monitoring object is created and added to the datagrid.

All this process works well, but If I comment or erase the _monitoringDatas = new List<Monitoring>(); instantiation into the ReceiveLidarValue(string arg) Task, the grid is not filled (without any error). And Of course, if I leave this second instantiation in the code, the datagrid will be filled but it will always contain only one line replacing the previous one instead of having the new line written after the previous one.


Solution

  • You haven't explained where ReceiveLidarValue gets called, so it's possible it's being called before OnInitializedAsync() is called. You also didn't explain why you think you have to instantiate _monitoringDatas twice. If you are receiving a null ref exception, then my comment above is probably correct. If not, please fill in more details.