473,739 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflecti on
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(MyClass 2).GetConstruct or(New Type()
{})
Dim myObj As Object = conInfo.Invoke( New Object() {})

Dim conInfo1 As ConstructorInfo = GetType(MyClass 2).GetConstruct or(New
Type() {GetType(Boolea n)})
Dim myObj1 As Object = conInfo.Invoke( New Object() {False})
End Sub

End Module
Sep 14 '06 #1
2 1569
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.j a.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
3997
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 ID=MyID
1
3059
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
3040
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 load the dll, create the object and return. BaseObject reference the factory dll. factory.dll public object CreateObj(string objectType, ref string suppMsg) { string LoadObj = string.Format("WitsmlObjects.{0}",objectType);
1
227
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 is already been referenced in the client project. Now i want to create any object from that string. I think this require getting a type from that string and then creating object from that type. If this or any alternative is possible, pls let me...
1
1335
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 I go to the menu "Inherited Form" I choose the base class : Form1 When the designer wants to display the new inherited Form I have an error
1
1904
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 TabControl - mainly a grid on one tab, and a checkboxlist on the other, that coordinate back and forth, so (un)checking a box will hide or show a column on the grid. In addition, I would like the flexibility of adding a DataSource at design time...
6
1356
by: CreateObject | last post by:
Assume that I have the classes below; class mercedes: IAuto { .... } class ford: IAuto{ .... }
3
5990
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 constructors other than default one and I should pass required parameters to use them. Is it possible to instantiate objects at run-time using non-default construcotors?
10
3472
by: psbasha | last post by:
HI, Is it possible to access the Methods of a class without creating object? Thanks PSB
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.