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

前端 未结 2 1048
小蘑菇
小蘑菇 2020-11-22 02:55

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

相关标签:
2条回答
  • 2020-11-22 03:39

    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.

    0 讨论(0)
  • 2020-11-22 03:42

    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.

    0 讨论(0)
提交回复
热议问题