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

A

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
 
T

Tom Shelton

Andrew said:
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,
 
A

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
 
P

Phill W.

Andrew said:
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.
 

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