Passing a constructor parameter with Activator.CreateInstance

G

Guest

I'm having a problem passing a constructor parameter with
Activator.CreateInstance.

Details:
I have a form that I'm trying to create from a shared function that uses the
Activator.CreateInstance.

The constructor code for the form:

Public Sub New(ByVal SearchID As String)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
m_searchID = SearchID

End Sub

Here is the code that is failing from the shared function:

Dim F As Form
Dim args(0) As String
args(0) = SearchID
F = CType(Activator.CreateInstance(newFormType, args), Form)
F.MdiParent = CType(Activator.CreateInstance(mdiParentType), Form).ActiveForm
F.Show()

It is failing on the Activator.CreateInstance line, I assume I'm not passing
the constructor parameter correctly in the args variable. How do update my VB
to correctly pass this constructor parameter to instantiate the form? Thanks.
 
J

Jeffrey Tan[MSFT]

Hi jmh,

Thanks for your post.

First, there are 2 lines of Activator.CreateInstance calling in your code
snippet, can you tell me which line fails the calling? Also, can you show
us the detailed error message of this exception?

Normally, we can dynamically create a form instance like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim f As Form = CType(Activator.CreateInstance(GetType(Form)), Form)
f.Show()
End Sub

This code snippet works well on my side.
=============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Jeffrey:

It is failing on:
F = CType(Activator.CreateInstance(newFormType, args), Form)

The reason I can not use your code snippet is the form I'm instantiating has
a parameter in the constructor, which requires that it passed when
instantiated.

To create the form without using Activator.CreateInstance, the code looks
like this:
Dim searchForms As New searchForms(SearchID)
searchForms.MdiParent = Me.MdiParent
searchForms.Show()

Note, I pass SearchID as a parameter in the constructor.

Here is the error message with Activator.CreateInstance:

An unhandled exception of type 'System.MissingMethodException' occurred in
mscorlib.dll

Additional information: Constructor on type TenantNetDesktop.mainMenu not
found.

Josh.
 
J

Jeffrey Tan[MSFT]

Hi Josh,

Thanks for your feedback.

For other inherited form, we can pass the arguments like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim args(0) As Object
args(0) = New Object
args(0) = 2
Dim f As Form2 = CType(Activator.CreateInstance(GetType(Form2),
args), Form2)
f.Show()
End Sub

Public Class Form2
Inherits System.Windows.Forms.Form

Sub New(ByVal SearchID As Int32)
MessageBox.Show(SearchID.ToString())
End Sub
End Class

It works well on my side. Thanks
==================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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