问题描述
我坚持认为同时具有VB.NET和C#组件的应用程序。我认为这两种语言差异只在语法,但我发现一个奇怪的特性在VB.NET不是present在C#。
I am maintaining an application that has both VB.NET and c# components. I thought these two languages differed only in syntax, but I have found a strange feature in VB.NET that is not present in C#.
在VB.NET,我有以下类:
In VB.NET, I have the following class:
Public Class bill_staff Inherits System.Windows.Forms.Form
....
End Class
如果我想使用这个类在C#中,我这样做:
If I want to use this class in C#, I do this:
using (var frm = new bill_staff())
frm.ShowDialog();
不过,在VB.NET code,类可以这样使用:
However, in the VB.NET code, the class can be used like this:
bill_staff.ShowDialog();
的ShowDialog
在这样的元数据定义的:
ShowDialog
is defined in the metadata like this:
Public Function ShowDialog() As System.Windows.Forms.DialogResult
因此,在VB.NET,就可以调用这个类的一个实例方法。据我所知,这似乎隐含创建类的新实例,然后调用对象的方法。在C#中,这是不可能的 - 静态方法必须被调用的类,方法和实例方法必须调用的对象
So in VB.NET, it is possible to call an instance method on the class. As far as I can tell, this seems to implicitly create a new instance of the class, and then calls the method on that object. In C#, this is not possible - static methods must be called on the class, and instance methods must be called on objects.
我找不到这个互联网上的任何信息。所谓的功能,并且它是好的做法呢?
I can't find any information about this on the Internet. What is the feature called, and is it good practice?
该项目最初转换从VB6 - 这是一些奇怪的原有功能,
The project was initially converted from VB6 - is it some weird legacy feature?
推荐答案
是的,这是传统的行为。类没有出现在VB中,直到第4版,在此之前, Form1.Show
是的路的显示形式。为了保持previous code兼容(VB3也很受欢迎),旧的方法维护。
Yes that is legacy behavior. Classes did not show up in VB until v4, before that Form1.Show
was The Way to show forms. In order to keep previous code compatible (VB3 was also very popular), the old method was maintained.
它仍然是在.NET支持作为法律手段来展现形式。最初,这被添加到可以很容易地迁移VB6 code到VB.NET。不过,也有可以很容易得到的东西在VB运行 - MS是指它作为的在你的指尖功能的和类似的短语。
It is still supported in .NET as a legal means to show forms. Initially, this was added to make it easy to migrate VB6 code to VB.NET. But is also there to make it easy to get something running in VB - MS refers to it as functionality at your fingertips and similar phrases.
基本上,它提供了工匠不理解对象和面向对象编程的简单方法。试想一下,我们就在这里,如果 Form1.Show
抛出了一个错误的问题。
Basically, it provides the tinkerer an easy way to program without understanding Objects and OOP. Imagine the questions we would have here if Form1.Show
threw an error.
明确实例化是更好的方法,因为它是面向对象的,使得它不太可能你的code将引用 - 或者创建 - 一个新的窗体2
当你真正想要的使用现有的实例。
Explicit instancing is the better method because it is object oriented and makes it less likely your code will reference - or create - a new Form2
when you actually wanted to use an existing instance.
这篇关于对象在vb.net中隐式实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!