implicit form loading...

L

Lee Moody

I'm using the following code to load a form based on the
name of the form:

Dim myType As Type = Type.GetType("SysName.CallingForm")
Dim myForm As Object = System.Activator.CreateInstance _
(myType)
myForm.mdiparent = MainMDIForm
myType.GetMethod("Show").Invoke(myForm, Nothing)

After it executes I get the following error:

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

Additional information: Exception has been thrown by
the target of an invocation.

I've used this particular type of code in a few places
within my application and it works fine. I can't see
anything wrong to determine why this isn't working here.
I was hoping maybe someone on the form has seen a problem
similar to this and can point me in the right direction.

Thanks

-Lee
 
H

Herfried K. Wagner [MVP]

* "Lee Moody said:
I'm using the following code to load a form based on the
name of the form:

Dim myType As Type = Type.GetType("SysName.CallingForm")
Dim myForm As Object = System.Activator.CreateInstance _
(myType)
myForm.mdiparent = MainMDIForm
myType.GetMethod("Show").Invoke(myForm, Nothing)

Why not assign the form object to a variable of type 'Form' and use the
'Show' method directlly without reflection?
 
J

Jeremy Cowles

Lee Moody said:
I'm using the following code to load a form based on the
name of the form:

Dim myType As Type = Type.GetType("SysName.CallingForm")
Dim myForm As Object = System.Activator.CreateInstance _
(myType)
myForm.mdiparent = MainMDIForm
myType.GetMethod("Show").Invoke(myForm, Nothing)

This exception is says that there is an error in the form that is loading,
not in the load-er. So it could be literally anything.

This methodology has a two enormous problems:

1. You never get exact error messages, no matter what the error is, the
description will always be the same. This makes debugging a client error
almost impossible.

2. You have effectively eliminated the strong type system, now you have just
rolled your code back into the days of VB Script.

I think I see what you are trying to accomplish - you should look into using
Delegates, or better yet, a generic Interface. Delegates will eliminate the
2 problems above, but using an Interface would be much cleaner.

Of couse, this is all just my opinion, I appoligize if I come off as bashing
your code.

~
Jeremy
 

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