Link to home
Start Free TrialLog in
Avatar of Arikael
ArikaelFlag for Switzerland

asked on

Can't instance because it is declared as abstract

Hi experts

I never actually try to instance MdiChild (I'd unterstand the error if I did) but I get the error that I can't create an instance of typ MdiChild because it's declared as abstract.

Can you tell my why I get that error?
abstract public class MdiChild : Form, ISubject
    {
        public MdiChild() : base()
        {
 
        }
}
 
 
public partial class Form2 : MdiChild
    {
        public Form2()
        {
            InitializeComponent();
        }
 
     
    }
 
//in mdiparent
Form childForm2 = new Form2();
childForm2.MdiParent = this;

Open in new window

Avatar of dampsey
dampsey
Flag of Türkiye image

this is not related with MDI forms.
You have declared the class as "abstract"
abstract classes are for inheritance.
You cannot create an instance from abstract classes.

declare a new class, inherit this from your abstract cass and try to instantiate from that.

for example:

public class myMdiChlic : MdiChlid
{
}

//in mdiparent
myMdiChlic childForm2 = new myMdiChlic ();
childForm2.MdiParent = this;
Avatar of Arikael

ASKER

That's what I'am doing

Form2 inherits from MdiChild and gets instantiated (see code above)
I'm really sorry. I don't know why but I didn't see that your form2 inherits mdichild. :(
Avatar of nsanga
nsanga

Have you implemented all the methods of abstract class or ISubject interface. if you haven't implemented, then class Form2 also becomes an abstract class.
Avatar of Arikael

ASKER

MdiChild does implement all methods from ISubject.
And the methods of MdiChild are not declared as abstract, so in my understanding I don't have to implement them in Form2.

I've created two small classes manually, which work perfectly but as soon as add a Windows form and try the same the warning displays.

Here my test-classes
//file with the interface
interface ISubject
    {
        void AddObserver();
        void DeleteObserver();
        void updateObserver();
    }
 
//file 1 
public abstract class MyParent : Form, ISubject
 
    {
        public MyParent()
            : base()
        {
 
        }
 
        public void AddObserver()
        {
 
        }
 
        public void DeleteObserver()
        {
 
        }
 
        public void updateObserver()
        {
 
        }
    }
 
    public partial class MyChild : MyParent
    {
        public MyChild()
        {
 
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Arikael
Arikael
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial