3
Answers

Error with "this"

brian ashcroft

brian ashcroft

17y
3.1k
1
This is probably an already answered questions, but you can imagine the trouble using google to find "c# + this".  So I have a class that is trying to trigger an event. 
I have the delegate definition
public delegate void OnServingSelectedEvent(object sender, decimal Serving);
class FractionDecimalTextBox:SourceGrid.Cells.Editors.TextBox
    {
        public event OnServingSelectedEvent OnServingSelected;

        public override bool ApplyEdit()
        {
            OnServingSelected(this, Generals.ToDecimal(Control.Text));
            return base.ApplyEdit();
           
        }
  }

I keep getting the error

Object reference not set to an instance of an object.
With

Cannot obtain value of local or argument '<this>' as it is not available at this instruction pointer, possibly because it has been optimized away.    AutocompleteGrid.FoodGrid.FractionDecimalTextBox

shown in the watch page.
As far as I understand it, this is supposed to point to the current object, but I keep getting this error, although it is completely unregular as I have another class where it works great.  Does anyone know what I am doing wrong?



Answers (3)