none
DefInstance()

    Question

  • I have upgraded from VB6 to VB.NET.

    The application contains a form which in turn contains a Timer which activates other processes. The sub main() contains frm.DefInstance.Show() in it. 

    But after migration, the above form is not being activated and the timer is not being executed. How can I make it work?

    Friday, May 26, 2006 8:08 PM

Answers

  • In VB6 if you create a Form, eg frmCal, you can access the Form and it's Controls, Properties, etc using that name - frmCal.   This is something called default instancing which meant it would create a default instance of that class/form if you referred to it by the form name.

    In VB.NET (Prior to 2005) you couldnt.    In VB.NET, frmCal is the name of the class <only> and you must instantiate that class and use the object thus created.   So you would use the following lines to create an instance and then call the show method on this instance.

    Dim x as new frmCal
    x.show


    When converting a VB6 project to VB.NET, the converter creates a Shared member
    of the class called DefInstance to which it assigns the reference to the Form instance that it creates.

    Something like:
    Class frmCal
    Public Shared DefInstance As frmCal

    Public Sub New
    DefInstance = Me

    If you have frmCal.TextBox1 in your code, for instance, it won't behave the way that you would expect from knowing VB6.
    Most often it will bark at you (reference to a non shared member). The working equivalent is frmCal.DefInstance.TextBox1.

    This method only works for Form classes which will only have one instance. DefInstance <is> that one.

    So you have an option which would be to create an instance of the object similar to

    Dim x as new frmCal
    x.show

    Which is preferable.

    or if you are using VB Express / 2005 you can use the

    My.Forms.frmCal.Show()

    which will create a default instance of the frmcal.

    As to what is going on with your code and why the timer form is not being activated and the timer not running.    I'd put a breakpoint in the form load and form activate events and see if they are actually getting fired.

    The Conversion is not a great solution and it is better to rewrite you code to not rely on the default instancing as it is really a hack and the conversion is a work around of this hack.  

    Once you can provide some more information as to what events are occuring and how you code is structured - this may help in identifying the issue.

     

     

    Saturday, May 27, 2006 12:22 AM

All replies

  • Welcome to the MSDN fora. I hope you find an answer to your question.

    In order to assist you with that, I wonder if you've read this?

    Helpful Suggestions

    Friday, May 26, 2006 8:42 PM
  • In VB6 if you create a Form, eg frmCal, you can access the Form and it's Controls, Properties, etc using that name - frmCal.   This is something called default instancing which meant it would create a default instance of that class/form if you referred to it by the form name.

    In VB.NET (Prior to 2005) you couldnt.    In VB.NET, frmCal is the name of the class <only> and you must instantiate that class and use the object thus created.   So you would use the following lines to create an instance and then call the show method on this instance.

    Dim x as new frmCal
    x.show


    When converting a VB6 project to VB.NET, the converter creates a Shared member
    of the class called DefInstance to which it assigns the reference to the Form instance that it creates.

    Something like:
    Class frmCal
    Public Shared DefInstance As frmCal

    Public Sub New
    DefInstance = Me

    If you have frmCal.TextBox1 in your code, for instance, it won't behave the way that you would expect from knowing VB6.
    Most often it will bark at you (reference to a non shared member). The working equivalent is frmCal.DefInstance.TextBox1.

    This method only works for Form classes which will only have one instance. DefInstance <is> that one.

    So you have an option which would be to create an instance of the object similar to

    Dim x as new frmCal
    x.show

    Which is preferable.

    or if you are using VB Express / 2005 you can use the

    My.Forms.frmCal.Show()

    which will create a default instance of the frmcal.

    As to what is going on with your code and why the timer form is not being activated and the timer not running.    I'd put a breakpoint in the form load and form activate events and see if they are actually getting fired.

    The Conversion is not a great solution and it is better to rewrite you code to not rely on the default instancing as it is really a hack and the conversion is a work around of this hack.  

    Once you can provide some more information as to what events are occuring and how you code is structured - this may help in identifying the issue.

     

     

    Saturday, May 27, 2006 12:22 AM
  • I put breakpoints to find out the flow to timer. The timer should get executed after the form is loaded. But looks like application closes after the form is getting loaded and hence the timer is not getting triggered. (Application will terminate when Sub Main() finishes.)

    How to solve this?

     

    Tuesday, May 30, 2006 4:59 PM
  • Display the first form with showdialog is one way.

    Sub Main

    Dim x as new form1
    x.Show

    End Sub

    or use the following 

    Sub Main

     MainForm = New frmMain
    Application.Run MainForm

    End Sub

     

    Tuesday, May 30, 2006 5:17 PM