Error: "No parameterless constructor defined for this object."

S

Sachi

Hi,
I am trying to invoke as like below.

Form frmChild = (Form) Activator.CreateInstance
(typeInstance);
frmChild.MdiParent =
_parentForm;
frmChild.StartPosition =
FormStartPosition.CenterScreen;
frmChild.Show();

When I invoke a form it works fine after frmchild is
shown it is giving error saying "No parameterless
constructor defined for this object.". But still works.

Catch is when i again invoke some child frm from
frmChild it throws again "No parameterless constructor
defined for this object." and fails to load any form.

But when i remove mdiparent property it works fine.
That is not the soultion i want. I want all the forms
inside MDI application.

There is no clue or solution soo far. Hope someone can
share some light on this problem.

Best Regards,
Sachi
 
J

Jon Skeet [C# MVP]

Sachi said:
I am trying to invoke as like below.

Form frmChild = (Form) Activator.CreateInstance
(typeInstance);
frmChild.MdiParent =
_parentForm;
frmChild.StartPosition =
FormStartPosition.CenterScreen;
frmChild.Show();

When I invoke a form it works fine after frmchild is
shown it is giving error saying "No parameterless
constructor defined for this object.". But still works.

What do you mean by "but still works"? It won't have created the form.

Basically, Activator.CreateInstance tries to invoke a parameterless
constructor. If there isn't one, it can't create an instance.
Catch is when i again invoke some child frm from
frmChild it throws again "No parameterless constructor
defined for this object." and fails to load any form.

With the above, frmChild will never actually be assigned to, so you'll
need to clarify exactly what you're doing.
 
S

sachi

What do you mean by "but still works"? It won't have
created the form.
Well, When I remove this line

frmChild.MdiParent = _parentForm;

it works as a modelform and which is not part of MDI
application. But I want all the forms to be part of MDI.

I checked out all the forms which i am invoking. There
is constructor and there is no parameters for those
constructors.

Hope to solve it soon. :) .

Thanks and Regards,
Sachi
 
J

Jon Skeet [C# MVP]

sachi said:
Well, When I remove this line

frmChild.MdiParent = _parentForm;

it works as a modelform and which is not part of MDI
application. But I want all the forms to be part of MDI.

It won't even *get* to that line though, if Activator.CreateInstance
has thrown an exception.
I checked out all the forms which i am invoking. There
is constructor and there is no parameters for those
constructors.

Hope to solve it soon. :) .

At this stage we really need a complete example, because I can't see
how the various things you're saying tie in with each other.

See http://www.pobox.com/~skeet/csharp/complete.html
 
J

Jon Skeet [C# MVP]

Sachi said:
Can I discuss this offline so then i can post details on
code?

Of course. If your example could be as simple as possible (while still
demonstrating the problem) that would help a lot.
 
H

Herfried K. Wagner [MVP]

* "Sachi said:
Hi,
I am trying to invoke as like below.

Form frmChild = (Form) Activator.CreateInstance
(typeInstance);
frmChild.MdiParent =
_parentForm;
frmChild.StartPosition =
FormStartPosition.CenterScreen;
frmChild.Show();

When I invoke a form it works fine after frmchild is
shown it is giving error saying "No parameterless
constructor defined for this object.". But still works.

Which line throws the exception?
 
S

Sachi

frmChild = (Form) Activator.CreateInstance(typeInstance);
frmChild.MdiParent = _parentForm;
frmChild.StartPosition = FormStartPosition.CenterScreen;
frmChild.Show();


In the above code when i am trying to assign mdiparent
only it is throwing error.

It is highly clubbed with lot of forms. I will send you a
sample to your mail id more similar example.

...
Sachi
 
S

Sachi

protected virtual void OnMenuItemClick(object sender,
EventArgs e)
{

DynamicMenuItem menuItem = (DynamicMenuItem)sender;
if(menuItem.Expression == "") return;

// The first object we need is a reflection of the
// current object, encapsulated in a "Type" object.
Type thisType = this.GetType();

// Now we look for the method in question, using the
// Type.GetMethod(string) method, which will return a
// MethodInfo representation object, or null if no matching
// method was found

MethodInfo theMethod = thisType.GetMethod
(menuItem.Expression);
theMethod.Invoke(this, userParameters);

}

Above code I am using to invoke method dynamically when
clicked on the menuitem.

Also I am overloading in the base form onLoad method too.

Any clue?

Best Regards,
Sachi
 
H

Herfried K. Wagner [MVP]

* "Sachi said:
frmChild.MdiParent = _parentForm;

Above line throws the error.

I don't understand that. There is no ctor called in this line. Did you
place 'Try...Catch' around this line?
 
J

Jon Skeet [C# MVP]

Sachi said:
frmChild.MdiParent = _parentForm;

Above line throws the error.

Are you absolutely sure? That seems a very odd line to get the error
on, but the Activator.CreateInstance line would be a very *normal*
place to get the error.

Are you able to step over the Activator.CreateInstance line in the
debugger, for example?
 
S

Sachi

Ya, I am very much sure about this line.
It throws error when trying to set mdiparent for frmChild
form.

CreateInstance works perfectly. I had been debugging on
this for more then 5hours :( .
Because it is been throwing error this line i am now with
no clue to guess.

Only thing i thought was something related to base class
constructor. But when i run in debugg mode - all the
constructor is called without any any exception thrown.

...
Sachi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top