C
Chris Crowhurst (ems)
Hi,
I am trying to write my first .NET CF app and I am struggling a bit. I
have created a form manager class to deal with forms. The OpenForm()
method checks each form in the arrForms collection. If the form does
not exist it should create a new instance of the form and return it.
When I run the code I get the following error:
System.ArgumentNullException was unhandled
Message="ArgumentNullException"
I think the problem is the way I am trying to create a new instance of
the form:
frmRetForm = Activator.CreateInstance(typeForm)
If I use the New keyword (so it looks like frmRetForm = New
Activator.CreateInstance(typeForm)) the IDE complains with Type
'Activator.CreateInstance' is not defined.
Can someone point out what I am doing wrong?
Thanks!!
Chris
Now for the code...
'---------------------------------------
Public Class FormManager
Friend arrForms As New ArrayList
' Opens or shows an instance of a form
Friend Function OpenForm(ByVal typeForm As System.Type) As Form
Dim frmRetForm As Form
Dim bExists As Boolean = False
' Check to see if a form of the requested type already exists
For Each frmWork As Form In arrForms
If frmWork.GetType() Is typeForm Then
frmRetForm = frmWork
bExists = True
Exit For
End If
Next
' If it does not exist create an instance of the form type and
it and add it to the collection
If Not bExists Then
' This bit here is not working, it throws an error at run
time
frmRetForm = Activator.CreateInstance(typeForm)
End If
Return frmRetForm
End Function
Friend Sub CloseForm(ByVal frmRemove As Form)
arrForms.Remove(frmRemove)
End Sub
End Class
Public Class Main
Public Shared Sub Main()
Dim MyFormManager As FormManager
MyFormManager = New FormManager
MyFormManager.OpenForm(Type.GetType("FirstForm")).ShowDialog()
End Sub
End Class
I am trying to write my first .NET CF app and I am struggling a bit. I
have created a form manager class to deal with forms. The OpenForm()
method checks each form in the arrForms collection. If the form does
not exist it should create a new instance of the form and return it.
When I run the code I get the following error:
System.ArgumentNullException was unhandled
Message="ArgumentNullException"
I think the problem is the way I am trying to create a new instance of
the form:
frmRetForm = Activator.CreateInstance(typeForm)
If I use the New keyword (so it looks like frmRetForm = New
Activator.CreateInstance(typeForm)) the IDE complains with Type
'Activator.CreateInstance' is not defined.
Can someone point out what I am doing wrong?
Thanks!!
Chris
Now for the code...
'---------------------------------------
Public Class FormManager
Friend arrForms As New ArrayList
' Opens or shows an instance of a form
Friend Function OpenForm(ByVal typeForm As System.Type) As Form
Dim frmRetForm As Form
Dim bExists As Boolean = False
' Check to see if a form of the requested type already exists
For Each frmWork As Form In arrForms
If frmWork.GetType() Is typeForm Then
frmRetForm = frmWork
bExists = True
Exit For
End If
Next
' If it does not exist create an instance of the form type and
it and add it to the collection
If Not bExists Then
' This bit here is not working, it throws an error at run
time
frmRetForm = Activator.CreateInstance(typeForm)
End If
Return frmRetForm
End Function
Friend Sub CloseForm(ByVal frmRemove As Form)
arrForms.Remove(frmRemove)
End Sub
End Class
Public Class Main
Public Shared Sub Main()
Dim MyFormManager As FormManager
MyFormManager = New FormManager
MyFormManager.OpenForm(Type.GetType("FirstForm")).ShowDialog()
End Sub
End Class