0

I'm trying to create an event handler and I am getting the error : 'Object' does not contain a definition for ListCountLastH " and same for listCountLH.

Changing private List to public List makes no difference and I am pretty sure I have to initialise the lists in protected override void OnStateChange() but anyway public /*override*/ void OnStateChange() makes no difference, so any help would be greatly appreciated.

public class Incrementer : DivergenceList5m throws ListCountLastH and listCountLH inaccessible due to protection level

DivergenceList5m.ListCountLastH.Add(DivergenceList5m.listCountLH); throws additionally "An object reference is required for the non-static field, method or property'Namwspace.Class.ListCountLastH " and same for listCountLH and I just don't know what else to try...

Thank you

public class Divergence5min : Strategy
{
   public int listCountLH;
   private List<int> ListCountLastH;
   private List<double> LastHSwDMI;

   protected override void OnStateChange()
   {
      ListCountH            = new List<int>(); 
      LastHSwDMI            = new List<double>();
   }
   ...
   protected override void OnBarUpdate()
   {  
     if (!LHsDmiAdd && b > 1 && HSwDMI[b-1] - HSwDMI[b] > 0.001 && HSwDMI[b-1] - HSwDMI[b-2] > 0.001)
     {
       LastHSwDMI.Add(HSwDMI[b-1]);
       listCountLH = LastHSwDMI.Count;
       ListCountLastH.Add(listCountLH);
      ...
     }
   }
   public class Incrementer
   {
      public EventHandler ItemAdded;

      public void AddItem()
      {

        base.ListCountLastH.Add(base.listCountLH);
        if (ItemAdded != null)
            ItemAdded(this, null);
      }
   }
}

Modifying like this with the private List still throws the 'inaccessible due to its protection level' error. The subscriber class does not throw any errors, though

public class Incrementer : DivergenceList5m
{
    public event EventHandler ItemAdded;            
    public void AddItem()
    {
        var divList = new DivergenceList5m();
        divList.ListCountLastH.Add(divList.listCountLH);
        if (ItemAdded != null)
            ItemAdded(this, null);
    }
}

class AddingItems
{
    public int ItemsCount {get; private set;}

    public void Items (Incrementer incrementer)
    {
        ItemsCount = 0;
        incrementer.ItemAdded += AddItems;

    }
    void AddItems (object source, EventArgs e)
    {
        ItemsCount++;               
    }
}

Last code version

public class Divergence5min : Strategy
{
   public int listCountLH;
   public List<int> ListCountLastH;
   public List<double> LastHSwDMI;
...
public class Incrementer : DivergenceList5m
        {
            public event EventHandler ItemAdded;            

            public void AddItem()
            {
                var divList = new DivergenceList5m();
                divList.ListCountLastH.Add(divList.listCountLH);
                if (ItemAdded != null)
                    ItemAdded(this, null);
            }
        }

        class AddingItems
        {
            public int ItemsCount {get; private set;}

            public void Items (Incrementer inc)
            {
                ItemsCount = 0;
                inc.ItemAdded += AddItems;              
            }
            void AddItems (object source, EventArgs e)
            {
                ItemsCount++;               
            }
        }

1 Answers1

3
  1. Looks like you are calling base.listCountLH in a class which inherits from object (i.e. you have not inherited it from anything - object is the default type). You need to inherit from Divergence5min like this:

    public class Incrementer: Divergence5min 
    
  2. In order to now call ListCountLastH from here, that property should be at least protected in order to access it from an inherited class:

    protected List<int> ListCountLastH;
    
  3. To access non-static members on a class you need to instantiate it first with:

    var divList = new DivergenceList5m();
    divList.ListCountLastH.Add(divList.listCountLH);
    
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Bassie, thank you for the answer, but as per my edit, I still have the protection level error (even with `protected List)` – Sorin Pascu Jan 19 '20 at 00:23
  • @SorinPascu in your edit you mentioned that the list is `private`, was that in error? Please try setting it to `public` and see what happens – Bassie Jan 19 '20 at 00:35
  • `private` was your fist advice, then I changed it to `protected` and now to `public`, all with no success, I'm afraid – Sorin Pascu Jan 19 '20 at 00:39
  • @SorinPascu Please update your question with the full code you are now running – Bassie Jan 19 '20 at 00:42