473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reflection, creating object with inherited cunstructor, how?


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
Sep 14 '06 #1
2 1547
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.
Sep 14 '06 #2
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

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ee**********@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.

Sep 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
1
by: Luiz Rafael Fernandes | last post by:
Hi... i try to serialize a object inherited of label control, as follow: //definition public class WinLabel : System.Windows.Forms.Label { public WinLabel() {} }
1
by: mark | last post by:
hi, I get the problem here, I have exception thrown when I try to cast the reflection created object. first there is BaseObject dll that implemation a interface, factory dll use the reflection...
1
by: Irfan | last post by:
Hi all I am new to DotNet and C# Is there any way to get a type of object from a string. Lets suppose i have a string variable containing a type "MyNameSpace.MyComponent" where MyComponent.dll...
1
by: Michel Diemunsch | last post by:
Hello I have created a new Windows Application. Visual has createt the form Form1 I compile and run application. I quit the application and want to create a new form that inherits from Form1...
1
by: ben m | last post by:
Hi all - we've recently switched up to 2005, and I'm having trouble getting the hang of some things, among them, creating a control for the project. Currently, we use a combination of controls on a...
6
by: CreateObject | last post by:
Assume that I have the classes below; class mercedes: IAuto { .... } class ford: IAuto{ .... }
3
by: Kürþat | last post by:
Hi, I want to set a plug-in architecture and need object creation at run-time. I can create objects at run-time using Activator.CreateObject method, so far so good. But some objects have...
10
by: psbasha | last post by:
HI, Is it possible to access the Methods of a class without creating object? Thanks PSB
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.