Home All Groups Group Topic Archive Search About

How to dynamically instantiate a base calss (w/ args to constructor?)

Author
29 Aug 2006 11:32 PM
Andrew Backer
Hello,

I am having a problem creating a class dynamically.  The class I have
is a base class of another, and the parent class has the constructor
(which takes one argument).  The base class (Class1, below) does not
have any constructors.  I am using code like this to create it, and
it's blowing up :

Dim res As Object = Activator.CreateInstance( _
   GetType( MyNameSpace.Class1 ), _
   "String Argument" _
)

Is there something I am doing wrong?  It worked for simple cases with
no constructor parameters.  Do I need to revert to that and just make a
setter on the class to set the properties?

Thanks,
Andrew

Author
30 Aug 2006 12:44 AM
Tom Shelton
Andrew Backer wrote:
Show quoteHide quote
> Hello,
>
> I am having a problem creating a class dynamically.  The class I have
> is a base class of another, and the parent class has the constructor
> (which takes one argument).  The base class (Class1, below) does not
> have any constructors.  I am using code like this to create it, and
> it's blowing up :
>
> Dim res As Object = Activator.CreateInstance( _
>    GetType( MyNameSpace.Class1 ), _
>    "String Argument" _
> )
>
> Is there something I am doing wrong?  It worked for simple cases with
> no constructor parameters.  Do I need to revert to that and just make a
> setter on the class to set the properties?
>
> Thanks,
> Andrew

Andrew - I'm having a little trouble decifering your class hierarchy...
Can you please be a little more specfic.  You seem to be using base
class and parent class in the wrong way.

The base (parent) class is the class that the derived (child) class
inherits from.  Which are you creating?  And which has the constructor?
Can you express in a small code sample (showing the inheritance
relationship) that will clarify you problem?

Thanks,

--
Tom Shelton
Author
30 Aug 2006 3:31 PM
Andrew Backer
I hoped I had made it clear (and I'm not sure the inheritance has
anything to do with it), but this might help:  Two classes, base and
derived.  Base class has constructor that takes an argument.  Derived
class has none, just methods.  Class1 is actually the derived class,  I
screw'd up in the post.

BaseClass :   2 public constructors.  first takes 1 param, second takes
none
Class1       :  derived from BaseClass, no constructors of it's own

I need to dynamically create an instance of the dervied class, but am
going *boom* when I try.    The actual error is this :

System.MissingMethodException: Constructor on type
'Long.Namespace.Class1' not found.
   at System.RuntimeType.CreateInstanceImpl ...
   at System.Activator.CreateInstanc ...

Hope that clears it up.  Thanks for the response, and sorry for having
to write in such a rush last time.

- Andrew

Tom Shelton wrote:
Show quoteHide quote
> Andrew Backer wrote:
> > Hello,
> >
> > I am having a problem creating a class dynamically.  The class I have
> > is a base class of another, and the parent class has the constructor
> > (which takes one argument).  The base class (Class1, below) does not
> > have any constructors.  I am using code like this to create it, and
> > it's blowing up :
> >
> > Dim res As Object = Activator.CreateInstance( _
> >    GetType( MyNameSpace.Class1 ), _
> >    "String Argument" _
> > )
> >
> > Is there something I am doing wrong?  It worked for simple cases with
> > no constructor parameters.  Do I need to revert to that and just make a
> > setter on the class to set the properties?
> >
> > Thanks,
> > Andrew
>
> Andrew - I'm having a little trouble decifering your class hierarchy...
>  Can you please be a little more specfic.  You seem to be using base
> class and parent class in the wrong way.
>
> The base (parent) class is the class that the derived (child) class
> inherits from.  Which are you creating?  And which has the constructor?
>  Can you express in a small code sample (showing the inheritance
> relationship) that will clarify you problem?
>
> Thanks,
>
> --
> Tom Shelton
Author
30 Aug 2006 3:44 PM
Andrew Backer
Ok, Here is the last bit of info that I didn't put.  I can get this to
work if I don't try to invoke the constructor with arguments (as in the
samle code before).

If I just create the derived class, it doesn't seem to care that the
class itself does't have a constructor (only the base class does).
After that, I can set properties and such.  If I try to pass a
parameter to make it invoke the other class, it blows up.  I can get
around this by creating a simple constructor in each derived class that
matches the signature of the base class, and just calls that, but thats
extra work ;)   Please forgive the vb, but I am required to do it :

public class BaseClass
    public sub New()
    public sub New( x as initStruct )
public class Class1 : Inherits BaseClass
    ... no constructors ...

I am calling this like this :
  Activator.CreateInstance( gettype(class1) )
  Activator.CreateInstance( gettype(class1), parameter )

The second call blows up, but the first is ok.  Maybe there is
something funny I am doing?

Thanks again,
Andrew


Andrew Backer wrote:
Show quoteHide quote
> I hoped I had made it clear (and I'm not sure the inheritance has
> anything to do with it), but this might help:  Two classes, base and
> derived.  Base class has constructor that takes an argument.  Derived
> class has none, just methods.  Class1 is actually the derived class,  I
> screw'd up in the post.
>
> BaseClass :   2 public constructors.  first takes 1 param, second takes
> none
> Class1       :  derived from BaseClass, no constructors of it's own
Author
31 Aug 2006 2:58 PM
Phill W.
Andrew Backer wrote:

> If I just create the derived class, it doesn't seem to care that the
> class itself does't have a constructor (only the base class does).

Actually, the Derived class /does/ have a Constructor, but you didn't
write it - Visual Basic did.  If you don't code /any/ Constructors in a
Class, VB will "write" one for you, with no arguments.  As soon as you
code /any/ Constructor in the class, the "free" one "goes away" again.
Can be confusing if you're using the "invisible" niladic one, then add
another one of your own and the first stops working!

> After that, I can set properties and such.  If I try to pass a
> parameter to make it invoke the other class, it blows up. 

That's because your Derived class doesn't have a Constructor that takes
an argument, so you get a MissingMethodException.

The important point here is that *Constructors are not Inherited*.
You have to code them (all) in /every/ derived class.

> I can get around this by creating a simple constructor in each derived
> class that matches the signature of the base class, and just calls that,
> but thats extra work ;)

And that's exactly what you have to do.

After all, your Base class Constructor may need to be "fed" with stuff
that only the Derived classes can get hold of but that you don't want
the Outside World to even know about.

> Please forgive the vb, but I am required to do it :
Nothing to forgive - I'm seeing this a Visual Basic group ... ;-)

HTH,
    Phill  W.