views:

538

answers:

5
+1  Q: 

Forms in VB.Net

I am from a c# background and am converting a vb.net windows forms app to c#. I have a windows form called associateForm. In code the developer references associate form like so:-

Private Sub NotifyIcon1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles EMS.MouseDoubleClick
    If e.Button = Windows.Forms.MouseButtons.Left Then

        If associateForm.WindowState = FormWindowState.Normal And associateForm.Visible = True Then
            associateForm.WindowState = FormWindowState.Minimized
        Else
            associateForm.WindowState = FormWindowState.Normal
        End If

        associateForm.Show()
        associateForm.Focus()
        'bring to front of other windows
        associateForm.Activate()
    End If
End Sub

But the thing is that associateForm is not instantiated within the class where the method is being executed. Nor is the class static. Nor does there seem to be an instance of the class anywhere in code. Can anyone shine any light on why this seems to work but when i try this in C# it is not having any of it.

+5  A: 

VB.Net for .Net 2.0 and later has something called default form instances. When you define a form, you get an automatic instance of the form with the same name as the type.

Joel Coehoorn
A really really stupid "feature".
Micah
It comes from the history of language. In the 16-bit and 32-bit version you added a form to the project and referred to it by it's name. There was no instantiation or NEW keyword involved. Do some research first.
RS Conley
Thanks for the response on this I was really struggling to know what this was!
anonym0use
A: 

Joel is correct. This feature exists because VB.NET emulates the VB6 method of being able to refer to a default instance of a form through it's name.

When you convert this to C# you need to provide global access to a a single instance named associateForm. There is a chance that something is preserved in between calls to this form. After you successfully gotten it working this way. Then you can refactor the software and test to see if a local instantiation will work the same.

RS Conley
+2  A: 

Several things are wrong with the previous comments. Joel's is basically the correct answer, with one small caveat:

When you define a form, you get an automatic instance of the form with the same name as the type.

You actually only get the instance when you call it. Before that, no default instance is created. Thus, instance creation is deferred until the time of usage.

Using the name of the form as an instance is actually a shortcut to My.Forms.Formname, which is a compiler-generated list of properties for all forms. These properties are responsible for the object creation. It is unfortunate that Microsoft has decided to make these properties implicitly available at global scope (hence the OP's question).

This feature exists because VB.NET emulates the VB6 method of being able to refer to a default instance of a form through it's name.

Well. While this feature is inspired by previous versions of VB, it's not a direct descended. VB 7 didn't have it. Rather, it was re-introduced with VB 8 (= VB 2005).

A really really stupid "feature"

Actually, it's a dead useful feature that plays very well with VB's RAD development philosophy. As mentioned above, it's unfortunate that such a loose scoping is employed. But other than that, I'd be very interested in hearing what's so “stupid” about this feature.

Konrad Rudolph
A: 

Ok so I see the default instance creation mechanism. Is there a comparable feature in C#? I have rewritten a system tray app which uses this feature to minimise and maximise the associate form window when a user clicks on the icon in the tray. The form that creates this associateform called "trayform" is what calls this method. How could I reproduce this in C#?

anonym0use
A: 

I can't answer your question directly, but...

I'm guessing you're converting from VB.Net to C# manually - have you tried using an automatic convertor instead?

Richard Ev
This is something not covered by most automatic converters. They normally just do basic syntax conversion, and this involves refactoring some code.
Joel Coehoorn