user3034944
user3034944

Reputation: 1751

Creating object of a class with parameterised constructors c#

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?

Upvotes: 1

Views: 140

Answers (1)

Gokul E
Gokul E

Reputation: 1386

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

Upvotes: 1

Related Questions