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#.
In VB.NET, I have the following class:
Public Class bill_staff Inherits System.Windows.Forms.Form
....
End Class
If I want to use this class in C#, I do this:
using (var frm = new bill_staff())
frm.ShowDialog();
However, in the VB.NET code, the class can be used like this:
bill_staff.ShowDialog();
ShowDialog
is defined in the metadata like this:
Public Function ShowDialog() As System.Windows.Forms.DialogResult
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?
The project was initially converted from VB6 - is it some weird legacy feature?
To create an object of a named class by using an object initializer. Begin the declaration as if you planned to use a constructor. Type the keyword With , followed by an initialization list in braces. In the initialization list, include each property that you want to initialize and assign an initial value to it.
Calling Dispose on the class will cause it to notify the file system that it no longer needs the file, and it may thus be made available to other entities. In general, if an object which implements IDisposable is abandoned without calling Dispose , some things which should get done, won't be. There is a mechanism in .
Constructors and DestructorsA destructor is a special member Sub of a class that is executed whenever an object of its class goes out of scope. A destructor has the name Finalize and it can neither return a value nor can it take any parameters.
NET languages, data is viewed through the object. Objects are collections of data values and associated source code. Whereas in older BASIC dialects, each data element was more or less independent through its named variable, related data values in OOP languages can be grouped into objects.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With