为什么会出现在VB.Net,但不是在C#中各种形式的默认实例? [英] Why is there a default instance of every form in VB.Net but not in C#?

查看:97
本文介绍了为什么会出现在VB.Net,但不是在C#中各种形式的默认实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇,想知道有(名称)属性,它重新presents Form类的名称。此属性的名称空间内用于唯一地标识类,该表是一个实例,并且,在Visual Basic的情况下,用于访问的形式的默认实例

现在在哪里这个默认实例从何而来,为什么不能C#有一个等效的方法了这一点。

此外,例如,以显示在C#中的形式,我们做这样的事:

  //唯一方法
Form1的FRM =新Form1的();
frm.Show();

但在VB.Net,我们有两种方式做到这一点:

第一常用方法
Form1.Show()第二种方法
昏暗FRM作为新Form1的()
frm.Show()


  1. 我的问题源于此第一种方法。这是什么 Form1中,它是 Form1中或实例的 Form1中类本身?现在,作为我的表单名称上面提到的是在VB.Net默认实例。但是,我们也知道, Form1中定义的类设计器凭什么名称是相同的两个实例和班级名称?
    如果 Form1中是一类则没有(静态\\共享)方法命名展()。
    那么,这是否来自这个方法?


  2. 他们在产生IL有何区别呢?


  3. 最后为什么不能C#有一个这样的相同呢?



解决方案

这是加回在VB.NET的版本与VS2005附带的语言。应广大用户要求,VB6的程序员有一个很难用看到一个类型,该类型的对象的引用之间的差异。 Form1上VS在您的片段FRM。有历史的,VB没有得到班,直到VB4而形式去所有的方式回到VB1。这是相当否则削弱程序员的头脑,理解是不同的是的非常的重要。在写有效的面向对象code得到一个镜头。的很大一部分原因是C#没有这个。

您可以得到这个早在C#中为好,尽管它不会是相当那么干净,因为C#不允许添加的属性和方法,以全局命名空间像VB.NET一样。您可以在点胶添加到您的形式为code,是这样的:

 公共部分类窗体2:表格{
    [ThreadStatic]私有静态Form2的实例;    公共窗体2(){
        的InitializeComponent();
        例如=这一点;
    }    公共静态窗体2实例{
        获得{
            如果(例如== NULL){
                例如=新Form2的();
                instance.FormClosed + = {委托实例= NULL; };
            }
            返回实例;
        }
    }
}

您现在可以在code使用Form2.Instance,就像你可以在VB.NET中使用窗体2。在属性getter的if语句code应该转移到自己的私有方法,使之高效,我把它这种方式清晰。

顺便说一句,在片段中的[ThreadStatic]属性是什么使许多程序员VB.NET放弃彻底绝望线程。一个问题,当抽象的泄漏。您的真正的关闭不会做的更好这个都没有。

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?

解决方案

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.

这篇关于为什么会出现在VB.Net,但不是在C#中各种形式的默认实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆