Home All Groups Group Topic Archive Search About

Reflection, creating object with inherited cunstructor, how?

Author
14 Sep 2006 1:33 PM
Søren M. Olesen
Hi

How do I create an instance of an object with an inherited cunstructor??

In the below example, I'm able to create an instance of MyClass2 using the
forst two lines of code, however if I try using the next two lines of code
it fails.



How do I create an instance of an object with an inherited cunstructor that
takes an argument??


TIA

Søren

Imports System.Reflection
Module Module1
Public Class MyBaseClass
Public Sub New()

End Sub
Public Sub New(ByVal arg As Boolean)

End Sub
End Class

Public Class MyClass2
Inherits MyBaseClass

End Class

Sub Main()

Dim conInfo As ConstructorInfo = GetType(MyClass2).GetConstructor(New Type()
{})
Dim myObj As Object = conInfo.Invoke(New Object() {})

Dim conInfo1 As ConstructorInfo = GetType(MyClass2).GetConstructor(New
Type() {GetType(Boolean)})
Dim myObj1 As Object = conInfo.Invoke(New Object() {False})
End Sub

End Module

Author
14 Sep 2006 2:03 PM
Phill W.
Søren M. Olesen wrote:

> How do I create an instance of an object with an inherited cunstructor that
> takes an argument??

Constructors are *not* inherited.

What you are seeing is the /implicit/ creation (by the VB compiler) of a
niladic Constructor in the derived class because /you/ haven't coded
/any/ Constructors of your own in that class.
The code that's running is more like:

Module Module1
    Public Class MyBaseClass
       Public Sub New()
       Public Sub New(ByVal arg As Boolean)

    Public Class MyClass2
       Inherits MyBaseClass
       Public Sub New() <--- this one is written for you.

The /only/ way to call the constructors in the base class is to do so
from a duplicate constructor in the derived class, as in

    Public Class MyClass2
       Inherits MyBaseClass
       Public Sub New()
          MyBase.New()
       End Sub
       Public Sub New(ByVal arg As Boolean)
          MyBase.New(arg)
       End Sub
    End Class

HTH,
    Phill  W.
Author
14 Sep 2006 2:15 PM
Søren M. Olesen
Hmmm... I see, but how come that the new constructor on my baseclass gets
callen in the first example then??

Guess it must be more like:

    Public Class MyClass2
       Inherits MyBaseClass
       Public Sub New()
            MyBase.New()
        End Sub





Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:eebngj$gdf$1@south.jnrs.ja.net...
> Søren M. Olesen wrote:
>
>> How do I create an instance of an object with an inherited cunstructor
>> that takes an argument??
>
> Constructors are *not* inherited.
>
> What you are seeing is the /implicit/ creation (by the VB compiler) of a
> niladic Constructor in the derived class because /you/ haven't coded /any/
> Constructors of your own in that class.
> The code that's running is more like:
>
> Module Module1
>    Public Class MyBaseClass
>       Public Sub New()
>       Public Sub New(ByVal arg As Boolean)
>
>    Public Class MyClass2
>       Inherits MyBaseClass
>       Public Sub New() <--- this one is written for you.
>
> The /only/ way to call the constructors in the base class is to do so from
> a duplicate constructor in the derived class, as in
>
>    Public Class MyClass2
>       Inherits MyBaseClass
>       Public Sub New()
>          MyBase.New()
>       End Sub
>       Public Sub New(ByVal arg As Boolean)
>          MyBase.New(arg)
>       End Sub
>    End Class
>
> HTH,
>    Phill  W.