Visual Basic 2005 New Function Reviews (18) - Advanced Usage of MY Namespace (Repost)

xiaoxiao2021-03-06  200

I have already introduced all the default objects in my namespace, I believe that everyone has begun to experience the convenience of My. But for some advanced users, these features have still been lacking. The vetets sometimes have written a class or function similar to the MY function, so if you put them in the MY namespace. MY is like a toolbox that can be accessed at any time, except for tools that have existed inside, of course, let us put your own things in. Let's take a look at how to expand your My namespace.

Add a custom class or module

If we want to put into the function in my MY, it is a convenient way to put the class or module in the MY namespace. It is very simple. Just define the class or module in the MY namespace.

Namespace My Public Module Tools Public Sub Dosomething () 'Some Code Here End Sub End ModuleEnd Namespace

It is very convenient to use My.Tools to access custom modules with my.tools.

Add a custom class instance

If we observe the MY namespace members provided by Visual Basic, they will find that they are not the class itself, but the object. Because all methods are static after all, it is not the best way. We sometimes need to put some custom classes in the MY namespace, so it is not necessary to save memory, and easy to control resource release. First we have to define a custom class, you can put it anywhere without having to put it in my namespace so that you can avoid the class name directly displayed after the MY key. Then, under my namespace, define a module with HIDEModulenameAttribute, the name can be cascaded; finally set the properties of the custom class instance in the module. Suppose our custom class called ToolsProxy, and the custom property is called Tools, then you can write this:

'Namespace My _Friend Module MyCustomModule Private syncRoot As New Object Private _tools As ToolsProxy' ''

'' 'Contains my custom tools.' '' Public ReadOnly Property Tools () As ToolsProxy Get 'Double check lock to ensure thread safty. If _tools Is Nothing Then SyncLock syncRoot If _tools Is Nothing Then _tools = New ToolsProxy End If End SyncLock End If Return _tools End Get End PropertyEnd Module

With HIDEMODULENAME this Attribute, it does not appear behind the MY key, and its properties will be displayed. After completion, you will find that my.tools is in this time with Visual Basic's built-in objects. This method is to expand your best method, recommended.

Extend my.application or my.computer Sometimes, we must not only add a custom class to my, but also hope to add more to my.application or my.computer itself. This can be done. Visual Basic provides a Partial keyword, which can expand the classes or structure in the current project, whether or not to add a Partial keyword regardless of the original definition. So we can use this feature to expand the definition of My.Application or My.computer. My.Application corresponds to myApplication class, and my.computer corresponds to myComputer class. For example, we have to add a new function to my.computer, to control the pop-up, indentation and automatic running of the optical drive, you can rewrite the MyComputer class directly under the MY namespace, as follows:

'Namespace My

Partial Class MyComputer Public Readonly Property CDROMDRIVER () AS CDROMDRIVER GET 'CODES Here End Get Propertyend Class

This successfully adds a new property to my.computer, and you will find the exact same behavior of my.computer.cdromdriver and other my.compute. Similarly, My.Application can also expand through Partial and add custom properties.

Now we can see that my namespace is completely open and programmable, we can freely play the imagination, create a complete MY namespace that belongs to your own.

Maybe saying that the programmer of C # is very eye-catching, can you use my name space in C #? The answer is energy, and it can't. The C # designer does not provide a method of accessing an object via My, then use My directly. But Visual Basic's designers have already takenn these questions and open most of the MY namespaces, let the C # and other language programmers can use them.

Use my.application in C #

To use My.Application, you must inherit System.Windows.Forms.WindowsFormsApplicationBase (in Microsoft.visualBasic.dll). After inheriting, we must do a few things. First, write the constructor to initialize the MyApplication class. These settings in Visual Basic are generated, while C # requires manual writing. Then rewrite the OnCreateMainform method, this method sets the main form of the current application. Next, write the MY class, let him provide a global access point to the MyApplication class. Finally rewrite Main, executing my.application.run () instead of the original Application.Run (), as follows.

namespace WindowsApplication2.Properties {public class MyApplication: WindowsFormsApplicationBase {public MyApplication (): base (AuthenticationMode.Windows) {IsSingleInstance = false; EnableVisualStyles = true; ShutdownStyle = ShutdownMode.AfterMainFormCloses;} protected override void OnCreateMainForm () {// Sepecify the main form of your project MainForm = new WindowsApplication2.Form1 ();}} public static class My {private static MyApplication _Application; private static object syncRoot = new object (); public static MyApplication Application {get {if (_Application == null) { Lock (syncroot) {if (_Application == null) {_application = new myapplication (); } // end lock} // end if return _application;} // end get}}} Modify the main function:

[Stathread] static void main () {// Application.enablevisualStyles (); // Application.enablertlmirroring (); // Application.run (new form1 ()); property.my.Application.run ();}

This way we fully get the object model of My.Application. But some dynamically generated content, such as SplashScreen object, etc., unless you write, otherwise you cannot use. With this approach, you can use My.Application to get many additional benefits, such as getting the event support for My.Application, which is very useful and very useful. However, due to the lack of automatic code support, you cannot guarantee that all behaviors of my.application events are the same as Visual Basic.

Use my.computer in C #

We have to use my.computer. We don't have to force it like Application. Visual Basic's designer put most of the energy supply into the SYSTEM's self-name space, which can be naturally used:

My name in the System name in My.Computer.FileSystemSystem.IO.FileSystemMy.Application.AssemblyInfoSystem.Reflection.AssemblyInfo * My.Computer.KeyBoardSystem.Windows.Forms.KeyBoardMy.Computer.MouseSysetm.Widnows.Forms.Mouse

Note: System.Reflecton.assemblyInfo is just the type of My.Applicaton.AssemblyInfo.

Of course, this is not as rich as Visual Basic, but usually with FileSystem is sufficient, other functions are also very simple to use .NET Framework original approach. It is to be noted that although these classes are dispersed in the various word namespaces of System, all are all in Microsoft.visualBasic.dll, they need to reference it to use.

Use my.resources and my.settings in C #

A good news is that C # also built into these two features, as long as you change my MY to Properties, it can be used as Visual Basic. But my.user, my.forms and my.webservices are really no, to implement their features, need to be fully manually encoded.

转载请注明原文地址:https://www.9cbs.com/read-128293.html

New Post(0)