CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    47

    Question Form Instantiation

    I am 5 months into converting my project from VB6 to vb.net (don't ask) and have just re-organised to do it properly. To be specific, I am treating forms as a class rather than as in VB6 where you just take them for granted.

    I declare them, just like you would a string or an integer. So, what would you expect to happen in the following piece of code (be honest). Then see the screenshot below

    Code:
    Module Module1
    
    Friend MyForm1 As New Form1 
    
    Sub Main()
      MsgBox(MyForm1.Name)
    End Sub
    
    End Module
    So, why does that happen. Well, there is an excellent article here on auto instantiation:

    http://visualbasic.about.com/od/usin.../a/NewForm.htm

    but it does not really answer my question ...

    As for why Microsoft added "auto instantiation" my guess is that it is a bit of kludge to cater for the great unwashed ie VB6 programmers and they are rather embarrassed about it. The result, as far as I can see, is a mess. If you use auto instantiation you get some ambiguities and if you do it explicitly you also get some ambiguities ... but maybe I am totally wrong! I await elucidation from my betters ...

    .
    Attached Images Attached Images  
    Last edited by wavering; June 24th, 2014 at 04:02 AM.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Form Instantiation

    Well as I see it you are making a copy of Form1. I would not expect the name to change, it is assigned in the original Form1 properties.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Aug 2010
    Posts
    47

    Re: Form Instantiation

    Quote Originally Posted by DataMiser View Post
    Well as I see it you are making a copy of Form1. I would not expect the name to change, it is assigned in the original Form1 properties.
    LOL! I have wracked my brains trying work out what was happening but your explanation was staring me in the face and I could not see it.

    Apologies for wasting people's time ... I will go and find a hole to hide in ...

    But the article on "auto instantiation" (no connection to me) is well worth reading as this is important but obscure
    .
    Last edited by wavering; June 27th, 2014 at 02:59 AM.

  4. #4
    Join Date
    Aug 2010
    Posts
    47

    Re: Form Instantiation

    On the subject of Auto-Instantiation (or, behavior as a function of the My.Forms object as Microsoft like to call it) I have just spent a couple of days trying to solve a problem. To be specific I had:

    Code:
    Friend frmOffice As New frmOffice
    
    Public Sub Main()
    ...
      frmOffice.Show()         ' This reads values from the Registry and sets buttons in frmOffice
    ...
    End Sub
    Code:
    Private Sub frmReception_Load(sender As System.Object,  e As System.EventArgs) Handles MyBase.Load
    ...
      blnMode = frmOffice.Rad01.Checked
    ...
    End Sub
    What I found was that although frmOffice was on display and the setting was clearly made as "True", blnMode was showing "False". After a lot of messing about it became apparent that there were TWO versions of frmOffice in existence. One auto instantiated by Microsoft (thanks for nothing) and one by me!

    So, I got rid of "Friend frmOffice As New frmOffice" and suddenly it all worked as it should ...
    MOPEKS - a freeware program that generates programs that use each other to solve problems. Is this the correct route to a genuinely intelligent machine?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured