469,299 Members | 2,073 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

implicit form loading...

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
Nov 20 '05 #1
2 838
* "Lee Moody" <an*******@discussions.microsoft.com> scripsit:
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?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
"Lee Moody" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chris Thunell | last post: by
1 post views Thread by Steve Jerkyson | last post: by
3 posts views Thread by Eric | last post: by
reply views Thread by Joe | last post: by
36 posts views Thread by Chad Z. Hower aka Kudzu | last post: by
2 posts views Thread by Geraldine Hobley | last post: by
18 posts views Thread by Tony | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.