Beginner: problem with writing my first form manager class

  • Thread starter Chris Crowhurst (ems)
  • Start date
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
 
T

tamberg

Are you shure Type.GetType("FirstForm") returns a value != null? (or do
you call it "Nothing" in VB?)
 

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