Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a default instance of every form in VB.Net but not in C#?

I'm just curious to know that there is the (Name) property, which represents the name of the Form class. This property is used within the namespace to uniquely identify the class that the Form is an instance of and, in the case of Visual Basic, is used to access the default instance of the form.

Now where this Default Instance come from, why can't C# have a equivalent method to this.

Also for example to show a form in C# we do something like this:

// Only method
Form1 frm = new Form1();
frm.Show();

But in VB.Net we have both ways to do it:

' First common method
Form1.Show()

' Second method
Dim frm As New Form1()
frm.Show()
  1. My question comes from this first method. What is this Form1, is it an instance of Form1 or the Form1 class itself? Now as I mentioned above the Form name is the Default instance in VB.Net. But we also know that Form1 is a class defined in Designer so how can the names be same for both the Instance and class name? If Form1 is a class then there is no (Static\Shared) method named Show(). So where does this method come from?

  2. What difference they have in the generated IL?

  3. And finally why can't C# have an equivalent of this?

like image 778
Shekhar_Pro Avatar asked Jan 15 '11 07:01

Shekhar_Pro


People also ask

What is the default form name in VB net?

When a new object is added to an application in Visual Studio it is assigned a default name which usually consists of the object type and a number. For example the first form object in an application is named Form1, the second Form2, and so on.


2 Answers

This was added back to the language in the version of VB.NET that came with VS2005. By popular demand, VB6 programmers had a hard time with seeing the difference between a type and a reference to an object of that type. Form1 vs frm in your snippet. There's history for that, VB didn't get classes until VB4 while forms go all the way back to VB1. This is otherwise quite crippling to the programmer's mind, understanding that difference is very important to get a shot at writing effective object oriented code. A big part of the reason that C# doesn't have this.

You can get this back in C# as well, albeit that it won't be quite so clean because C# doesn't allow adding properties and methods to the global namespace like VB.NET does. You can add a bit of glue to your form code, like this:

public partial class Form2 : Form {
    [ThreadStatic] private static Form2 instance;

    public Form2() {
        InitializeComponent();
        instance = this;
    }

    public static Form2 Instance {
        get {
            if (instance == null) {
                instance = new Form2();
                instance.FormClosed += delegate { instance = null; };
            }
            return instance;
        }
    }
}

You can now use Form2.Instance in your code, just like you could use Form2 in VB.NET. The code in the if statement of the property getter should be moved into its own private method to make it efficient, I left it this way for clarity.

Incidentally, the [ThreadStatic] attribute in that snippet is what has made many VB.NET programmers give up threading in utter despair. A problem when the abstraction is leaky. You are really better off not doing this at all.

like image 124
Hans Passant Avatar answered Oct 25 '22 13:10

Hans Passant


VB is adding a load of code into your project behind your back, basically.

The easiest way to see what's going on is to build a minimal project and look at it with Reflector. I've just created a new WinForms app with VB and added this class:

Public Class OtherClass    
    Public Sub Foo()
        Form1.Show()
    End Sub
End Class

The compiled code for Foo looks like this when decompiled as C#:

public void Foo()
{
    MyProject.Forms.Form1.Show();
}

MyProject.Forms is a property in the generated MyProject class, of type MyForms. When you start diving into this you see quite large amounts of generated code in there.

C# could do all of this, of course - but it doesn't typically have a history of doing quite as much behind your back. It builds extra methods and types for things like anonymous types, iterator blocks, lambda expressions etc - but not in quite the same way that VB does here. All the code that C# builds corresponds to source code that you've written - just cleverly transformed.

There are arguments for both approaches, of course. Personally I prefer the C# approach, but that's probably no surprise. I don't see why there should be a way of accessing an instance of a form as if it was a singleton but only for forms... I like the language to work the same way whether I'm using GUI classes or anything else, basically.

like image 28
Jon Skeet Avatar answered Oct 25 '22 13:10

Jon Skeet