1

I have a class with multiple parameterised constructors.

class MyClass{
    public MyClass(Context context) : this(context, VERTICAL)
    {
    }
    public MyClass(Context context, int Orientation) : base(context)
    {
        init(context, Orientation);
    }

    public MyClass(Context context, Android.Util.IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
              //more code
    }

    // I have to make an object of this MyClass into MyDataSetObserver class.

    public class MyDataSetObserver : DataSetObserver
    {
        MyClass mc;

        public MyDataSetObserver(MyClass _mc)
        {
            mc= _mc;
        }

        public override void OnChanged()
        {
                mc.onDataChanged ();
        }

            public override void OnInvalidated()
        {
                mc.onDataChanged();
        }
    }

//DatasetObserver usage

    public void setAdapter(Android.Widget.IAdapter myadapter, int initialPosition)
    {
        if (this.adapter != null) 
        {
            this.adapter.UnregisterDataSetObserver (adapterDataObserver);
        }

        //Assert.assertNotNull ("adapter should not be null", adapter);

        this.adapter = myadapter;
        adapterDataCount = adapter.Count;
        adapterDataObserver = new MyDataSetObserver (this);
        this.adapter.RegisterDataSetObserver (adapterDataObserver);
        if (adapterDataCount > 0) {
            SetSelection (initialPosition);
        }
    }
}

but this gives me the value of mc as null..

Also, I need to do a constructor chaining, Is this the right approach?

5
  • Show the Usage Code of MyDataSetObserver Commented Dec 23, 2013 at 6:23
  • Where are you seeing mc is null? In the MyDataSetObserver constructor? Commented Dec 23, 2013 at 6:38
  • @StevieB yes, mc is null in the MyDataSetObserver constructor Commented Dec 23, 2013 at 6:41
  • I would expect mc to be non null in the MyDataSetObserver constructor with the posted code after the assignment statement. The 'this' being passed in must be valid so _mc will be valid and hence mc must be valid. Commented Dec 23, 2013 at 7:06
  • I don't suppose you've mixed up mc and _mc in your code? The usual style is to prefix private members with an underscore and prefix locals with a letter whereas the opposite style is being used in the constructor. Commented Dec 23, 2013 at 7:08

1 Answer 1

1
adapterDataObserver = new MyDataSetObserver (this); 

What is this Here? is your class is the Instance of MyClass ? then It should have a Context Object.

or else just Create the Instance of MyClass by the answer of @Yohannes and then Construct the MyDataSetObserver object passing this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.