none
How do I convert a string into a form? RRS feed

  • Question

  • Suppose I have a string such as "Form8" and there is also a form by that name in my program.

    I want the user to be able to go to that form based on the string contents.

    I found some code on stackoverflow as follows:

    Dim frm As Form Dim dr As DialogResult Dim asm As Assembly dim formname as string = "Form8"

    asm = Assembly.GetExecutingAssembly() frm = CType(asm.CreateInstance("WindowsFormsApplication1." & formname), Form) dr = frm.ShowDialog()


    The above crashes, with a bang.

    What is the correct way to do this?


    • Edited by Gidmaestro Tuesday, May 22, 2018 10:57 AM omission
    Tuesday, May 22, 2018 10:56 AM

Answers

  • What you are using is Reflection, another word for late binding. In the executable is searched if something with the name "Form8" exist and the first one is taken. It is an insecure way and often the reason of crashing unpredictable after a program small change at runtime. 

    But it can be used likewise programming with Option Strict Off can be used which is in fact the same. 

    This is the difference by the way between strongly typed programs and scripting programs which all are relying on late binding (and therefore probably all browsers have default set not to break on scripting errors). 


    Success
    Cor


    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:02 PM
    • Edited by Cor Ligthert Tuesday, May 22, 2018 7:47 PM by and between, typing quicker than thinking
    Tuesday, May 22, 2018 11:34 AM
  • Tommy,

    Reflection is the process of analyzing/accessing code members by looking through the meta data that describes the object.

    So when you take an instance of a Type, and use that Type instance to call a method on an existing Object instance, it is reflection that uses the information from the Type to call the method.  As Cor mentioned, when you use LateBinding to call a method on an Object instance, the code is implicitly using Reflection to find the member you are trying to call.

    For example, if we have:

    Dim X As Object = "this is a test"

    We know that the type of X will be a String.  If we allow late binding in our code, we could write:

    If X.Contains("a") Then...  and reflection would be used implicitly to search for a Contains() method on the String instance.  This is essentially the same as doing:

    GetType(String).GetMethod("Contains").Invoke(x, {"a"})

    So reflection is a way of examining all of the members of a Type and then accessing those members from an instance of a general Object.

    Make sense?

    On the forum issue, yes, depending on the state of the platform at the moment, I've seen all of those behaviors before.  Sometimes I have to copy what I just wrote, refresh the thread, and they try posting again.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"



    Tuesday, May 22, 2018 1:51 PM
    Moderator
  • Here's an example that looks up the assembly name:

    Dim formName As String = "Form2"
    Dim asm = Reflection.Assembly.GetExecutingAssembly
    Dim obj = asm.CreateInstance(asm.GetName.Name & "." & formName)
    CType(obj, Form).ShowDialog()
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:02 PM
    Tuesday, May 22, 2018 11:17 AM
    Moderator
  • What is it you want to do exactly?

    Try this assuming you are showing a form in the project test1.

    Public Class Form5 Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim objType As Type = Type.[GetType]("test1." & "Form2") Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control) objForm.Show()

    End Sub End Class


    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:02 PM
    Tuesday, May 22, 2018 11:30 AM
  • This post (with the answer) I am not sure if it is the same as your post with the answer or not.  :)

    Essentially, yes. :)  Just two different ways to go about instantiating an object via reflection.

    Also, I'll do the same if during the time it took me to write an answer, someone else beat me to it.  I'll post mine, but then hit the propose link on the post that came in sooner and/or edit my post to reflect that there were initially no answers supplied while I was working the problem out.

    No harm, no foul on this thread. :)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed,

    For some reason I dont really know what reflection is... in fact the code I posted I found on the web and use to make a run time menu of forms in my test project and I dont really understand it...

    and yeah...

    In the past (this seems improved recently?) if you had the editor open for a (long?) period and now for some reason the original thread has been updated (not sure how) then one attempts to save the post/edit ... the editor throws an err and wont save/locks up/ and/or possibly you loose your whole edit... so I try to remember to refresh just before I post and then get to it and post before the penalty period begins...

    Also we need to make special effort to actually read and understand the other posts before posting.

    :)


    • Edited by tommytwotrain Tuesday, May 22, 2018 1:44 PM
    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:03 PM
    Tuesday, May 22, 2018 1:38 PM

All replies

  • Is the name of your program "WindowFormsApplication1"?

    I'm guessing not, as that would be the only apparent error in the code.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 11:14 AM
    Moderator
  • Here's an example that looks up the assembly name:

    Dim formName As String = "Form2"
    Dim asm = Reflection.Assembly.GetExecutingAssembly
    Dim obj = asm.CreateInstance(asm.GetName.Name & "." & formName)
    CType(obj, Form).ShowDialog()
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:02 PM
    Tuesday, May 22, 2018 11:17 AM
    Moderator
  • What is it you want to do exactly?

    Try this assuming you are showing a form in the project test1.

    Public Class Form5 Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim objType As Type = Type.[GetType]("test1." & "Form2") Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control) objForm.Show()

    End Sub End Class


    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:02 PM
    Tuesday, May 22, 2018 11:30 AM
  • What you are using is Reflection, another word for late binding. In the executable is searched if something with the name "Form8" exist and the first one is taken. It is an insecure way and often the reason of crashing unpredictable after a program small change at runtime. 

    But it can be used likewise programming with Option Strict Off can be used which is in fact the same. 

    This is the difference by the way between strongly typed programs and scripting programs which all are relying on late binding (and therefore probably all browsers have default set not to break on scripting errors). 


    Success
    Cor


    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:02 PM
    • Edited by Cor Ligthert Tuesday, May 22, 2018 7:47 PM by and between, typing quicker than thinking
    Tuesday, May 22, 2018 11:34 AM
  • Are the forms lagging again?  Were my posts not visible when you started yours Tommy?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 11:41 AM
    Moderator
  • Sorry, "forums"... I'm getting an "unexpected error" when trying to edit any post today.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 11:42 AM
    Moderator
  • Sorry, "forums"... I'm getting an "unexpected error" when trying to edit any post today.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed,

    No there was no answer when I started my edit. It took a while.

    I tried to edit my post above and could not, get an err when try to save the edit.

    Tuesday, May 22, 2018 11:49 AM
  • Sorry, "forums"... I'm getting an "unexpected error" when trying to edit any post today.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed,

    No there was no answer when I started my edit. It took a while.

    I tried to edit my post above and could not, get an err when try to save the edit.


    Oh wait. Maybe your first answer was there when I started mine.
    Tuesday, May 22, 2018 11:51 AM

  • Oh wait. Maybe your first answer was there when I started mine.

    I'd certainly give you the benefit of the doubt that it wasn't... I wasn't sure if the current wonkiness was on my end or the platform, but since you are getting the same edit error I bet its the platform.  I've noticed a few instances recently of folks basically repeating what I just posted and it is from people who would not normally do that (like yourself).  So I'm chalking it up to the forums having issues again.

    Thanks for clarifying.  :)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 12:00 PM
    Moderator

  • Oh wait. Maybe your first answer was there when I started mine.

    I'd certainly give you the benefit of the doubt that it wasn't... I wasn't sure if the current wonkiness was on my end or the platform, but since you are getting the same edit error I bet its the platform.  I've noticed a few instances recently of folks basically repeating what I just posted and it is from people who would not normally do that (like yourself).  So I'm chalking it up to the forums having issues again.

    Thanks for clarifying.  :)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    I see. Welp I am not sure now it could be both your posts were there.

    Yes, I try to refresh the question just before I post to check other answers but sometimes forget. And then sometimes I still post what I have taken the time to write even if someone else has already answered just because I wrote an answer so I post it.

    This post (with the answer) I am not sure if it is the same as your post with the answer or not.  :)

    Tuesday, May 22, 2018 12:06 PM
  • This post (with the answer) I am not sure if it is the same as your post with the answer or not.  :)

    Essentially, yes. :)  Just two different ways to go about instantiating an object via reflection.

    Also, I'll do the same if during the time it took me to write an answer, someone else beat me to it.  I'll post mine, but then hit the propose link on the post that came in sooner and/or edit my post to reflect that there were initially no answers supplied while I was working the problem out.

    No harm, no foul on this thread. :)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 12:59 PM
    Moderator
  • This post (with the answer) I am not sure if it is the same as your post with the answer or not.  :)

    Essentially, yes. :)  Just two different ways to go about instantiating an object via reflection.

    Also, I'll do the same if during the time it took me to write an answer, someone else beat me to it.  I'll post mine, but then hit the propose link on the post that came in sooner and/or edit my post to reflect that there were initially no answers supplied while I was working the problem out.

    No harm, no foul on this thread. :)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed,

    For some reason I dont really know what reflection is... in fact the code I posted I found on the web and use to make a run time menu of forms in my test project and I dont really understand it...

    and yeah...

    In the past (this seems improved recently?) if you had the editor open for a (long?) period and now for some reason the original thread has been updated (not sure how) then one attempts to save the post/edit ... the editor throws an err and wont save/locks up/ and/or possibly you loose your whole edit... so I try to remember to refresh just before I post and then get to it and post before the penalty period begins...

    Also we need to make special effort to actually read and understand the other posts before posting.

    :)


    • Edited by tommytwotrain Tuesday, May 22, 2018 1:44 PM
    • Marked as answer by Gidmaestro Tuesday, May 22, 2018 7:03 PM
    Tuesday, May 22, 2018 1:38 PM
  • Tommy,

    Reflection is the process of analyzing/accessing code members by looking through the meta data that describes the object.

    So when you take an instance of a Type, and use that Type instance to call a method on an existing Object instance, it is reflection that uses the information from the Type to call the method.  As Cor mentioned, when you use LateBinding to call a method on an Object instance, the code is implicitly using Reflection to find the member you are trying to call.

    For example, if we have:

    Dim X As Object = "this is a test"

    We know that the type of X will be a String.  If we allow late binding in our code, we could write:

    If X.Contains("a") Then...  and reflection would be used implicitly to search for a Contains() method on the String instance.  This is essentially the same as doing:

    GetType(String).GetMethod("Contains").Invoke(x, {"a"})

    So reflection is a way of examining all of the members of a Type and then accessing those members from an instance of a general Object.

    Make sense?

    On the forum issue, yes, depending on the state of the platform at the moment, I've seen all of those behaviors before.  Sometimes I have to copy what I just wrote, refresh the thread, and they try posting again.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"



    Tuesday, May 22, 2018 1:51 PM
    Moderator
  • Tommy,

    Reflection is the

    Make sense?

    On the forum issue, yes, depending on the state of the platform at the moment, I've seen all of those behaviors before.  Sometimes I have to copy what I just wrote, refresh the thread, and they try posting again.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Yes thanks.

    Sounds like looking through the wrong end of the binoculars?


    And, it does seem to me the forum editor and etc has been more stable recently maybe and these editor things were affected?


    PS Oh, I get it now... Reflection...!

    Tuesday, May 22, 2018 2:02 PM
  • That's a fair way of putting it. :)

    And I'm not sure on the stability... I've been seeing weird issues (like the editing thing this morning, which seems to have gone away now) so there's no telling what state the platform is in at any given instant.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 2:06 PM
    Moderator
  • That's a fair way of putting it. :)

    And I'm not sure on the stability... I've been seeing weird issues (like the editing thing this morning, which seems to have gone away now) so there's no telling what state the platform is in at any given instant.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    I get it now "REFLECTION" is like looking at it in the backwards order. 

    Well I am not saying the editor is perfect. Just that it seems some bugs are gone but I cant say exactly.

    And it seems I have not had an edit lost due to background updating while editing for a while and that is what I mean that seems improved? And some other things too. etc.

    No big deal.

    Tuesday, May 22, 2018 2:15 PM