Today, I found a problem in my post on the 24th. Some Visual Basic users think that the built-in functions of VB are directly defined in Microsoft. in the Visual Basic namespace, so it cannot be accessed directly in C. In fact, functions in Microsoft. VisualBasic. dll are defined in modules. in C #, you only need to directly write the module name to access them. However, in VB, only Microsoft. visual Basic accesses these functions, which makes general VB programmers do not know these functions in the module or the static import operations behind them. They prefer to write Microsoft. visualBasic. IIf, and do not write Interaction. IIf (in fact, the latter is the most direct method ).
Since this problem is often vague, I will write a post to introduce the knowledge of the VB module and static import. First, let's look at the module. VB defines a Module using the following syntax: Public Module MyModule
End Module
The VB module is essentially a class (with special Attribute to show the difference), but it cannot be regarded As a Type in VB (that is, it cannot be written after As, Of, CType and other statements ), of course, it cannot be instantiated or inherited. In addition, all members are automatically Shared members, so that they do not need to be written to Shared. The constructor of the module is also a Shared constructor. These features make the module A good container for stateless functions and data, and the new static class introduced in C #2.0 is actually a class of things. However, the module has one more static import step than the static class.
The "static import" feature supported by VB (this has recently become a new feature of Java 5.0, but C # does not support it ), it means to import accessible static members of a class into the global namespace, so that they can be called without writing type names. For example, you can write Imports System. Math at the beginning of the Code.
Imports System. Console
After writing this code, you can call Math. Sin to directly write the code as Sin, and call Console. WriteLine to write the code as WriteLine. This is particularly useful for classes that define constants.
Back to the previous topic, because the module can only define static members, VB automatically implements a layer of static import for all modules by default, import all accessible members in the module to the namespace where the module is located. This is exactly why all built-in functions in VB do not require the type name as the prefix. Some people say that the VB module defines global functions and variables, but in fact there is only one Automatic Static import, making the syntax similar to global functions or variables.
For example, the Microsoft. VisualBasic. Strings. Left function can be accessed by Microsoft. VisualBasic. Left because String is a module and Automatic Static import. If Microsoft. VisualBasic is also imported, you can directly use the Left name for access. In Windows Form, the Left function cannot be accessed directly using the function name because the Form has its own Left attribute. In this case, you only need to add the module name and use Strings. left can be accessed without using Microsoft. visualBasic. left is accessed by such a long name. To view the specific type of a function, you can view the Microsoft. VisualBasic namespace in the VS. NET Object Browser.
After knowing this, we can also use the automatic static import feature of the module to help us simplify some syntax. For example, we have a class ComputerClass. When using this class for users, we want users to directly use the members of ComputerClass. Because there is only one computer, you do not need to instantiate it on your own. The ComputerClass implementation may be stateful and cannot be made into a static class. You can write a module and use a single-instance mode named Computer in the module to implement the ComputerClass class. In this way, you do not need ModuleName. Computer, but only use the Computer name to access a unique Computer object. It not only maintains the static class syntax, but also has the advantages of the singleton mode. This is just a simple example of static import, which has been widely used in My namespace of VB. My members are actually single-instance objects.
For C # And C ++/CLI, the module does not have static import, which is a common static class and must use the module name. the static members in the module are accessed by the member name, which is actually the syntax used by the twenty-four painters, rather than using VB to encapsulate a layer. Visual Basic programmers should also be clear that built-in functions are defined in modules and are classified and organized by modules. In case of Name Conflict, the "module name. function name" method should be given priority to resolve the conflict, rather than mistakenly believing that these functions are directly defined in Microsoft. VisualBasic.