C#/vb. NET syntax for comparison

Source: Internet
Author: User
Tags manual writing

recently, because of the work of the relationship, from the use of three years of C # to switch to vb.net. There is no way, originally, the use of language is not a big relationship. But when it was just used, there were some bumps. Mainly is the code writing operation convenient aspect, summarizes several points:

When code is programmed to implement interface

C # in use: Interface, and then press SHIFT+ALT+F10 enter, hehe, all the interface needs to implement the method properties are automatically generated. Of course, this is 2005. 2003 I don't know, I didn't find it anyway. Besides, 2005 is a common kind.
VB using Implements, the cursor positioning to the corresponding interface above, directly enter, Oh, than C # still less than a step to automatically generate the interface needs to implement the method and properties.

Keyword Correspondence Relationship:

C#

vb.net

Using

Imports

This

Me

void

Sub

Base

MyBase

Abstract

MustInherit

Sealed

NotOverridable

Virtual

MustOverride

Switch

Select

Internal

Friend

Static

Shared

<T>

(of T)

Document comments

are based on XML syntax, can describe each type of code and each member of the information, we know that writing XML document annotations The greatest benefit is the ability to obtain IntelliSense support, and combined with a specific xlts to generate beautiful help documents, C # continuous input// The corresponding XML-compliant annotations are automatically generated. This VB does not seem to be, the continuous loss of VB and ", and then to manually write out the characters in the XML format comments out, once the start can not be disconnected, and can not be doped with other code elements, until all the XML tags are closed. <summary> is the recommended tag for VB and is a summary of the description element. In addition to <SUMMARY>, commonly used tags have <remark>, indicating that;<param> represents parameter information;< exception> represents an exception, plus <include> and < Permission> and so on. Of course, it is not necessary to use these recommended tags, but these tags can be used automatically, and can be used by the IDE, so C # can be automatically generated after///. On this point. I compare the clock meaning C # multipoint.

My namespace

"My" is a very good design and an engineering-related namespace in which the content is organized by the IDE's help. This is probably VB better than C # a place, at least to the present I know that C # is not this namespace. Of course, the properties in C # can be implemented in VB My.Resources and My.Settings, as for My.User, My.Forms and My.WebServices really no, to achieve their functions, need to fully hand-coded.
The My namespace consists of six main sections in the current version: My.Application, My.Computer, My.Resources, My.User, My.Forms, and My.WebServices. Enter the My keyword directly to find them, or you can import the My namespace, whose syntax is: the Imports project name. My

Since this is a new feature for me, check out the relevant information to get:
My.Application is an object that is related to the currently running application, many features are the same as the Application object, but My.Application is not just for Windows Form applications. Many features are still available in console applications. The list is described below:

My. Application Members

Function description

ApplicationContext

The context of the application, including the main thread and the main form information

AssemblyInfo

Assembly information, including version, copyright, title, product name, and executable name

Changecurrentculture

Change the application's current culture settings, such as currency and time formats

Changecurrentuiculture

Change the application's current user interface culture settings, such as display language and words

CommandLineArgs

A read-only collection that returns command-line arguments for the current application. These parameters are separated so that you do not have to manually separate the values of the command function as you did originally.

CurrentCulture

Return to current culture settings

CurrentDirectory

Returns the current directory used by the application

CurrentUICulture

Returns the current user interface culture settings

Deployment

Returns the deployment object for the application deployed by the ClickOnce method

DoEvents

Execute all Windows messages stored in Windows Message Queuing

Exit

Exit application

GetEnvironmentVariable

Get the value of an environment variable by the name of the environment variable

IsNetworkDeployed

Returns a value that indicates whether the current application is in a network deployment mode

Log

A logging tool that logs application event logs and exceptions

MainForm

Main form of the current application

OpenForms

A collection of all open forms in the current application, identical to the VB6 Forms collection functionality

Run

Start/Close Application Mode for Visual Basic

SplashScreen

Returns the current application as a splash screen window

The my.computer encapsulates a lot of functionality to access system and hardware information, and is much more convenient to operate than using the. NET framework or Windows APIs directly. including many objects, the use of these objects, previously to write n-multi-code east, now may be one or two lines of code can be done.

Objects in the My.Computer

Function description

Sample code

My.Computer.Audio

Provides the ability to play audio, which can be played from files such as WAV or audio streams, which means that you can use it to easily play audio stored in a resource file or in a database. You can also specify several settings, such as background playback or waiting to end, when playing. Combined with my.resources to use, it seems more convenient.

Simple playback of WAV files

My.Computer.Clipboard

Provides the ability to read and write the Clipboard in a strongly typed way, which is more useful than the Clipboard in Windows.Forms. You can use the Clipboard object to read and write audio, images, text, or even files on my computer directly from the Clipboard to drag and drop information. In addition, projects upgraded by VB6 will now upgrade the previous Clipboard objects directly using the My.Computer.Clipboard object, which will resolve the defect that vb.net cannot upgrade the original Clipboard functionality.

Copy the contents of a text box to the Clipboard

My.Computer.Clock

Gets the time of the tool, which can directly get the local time, the time zone and the number of milliseconds from then to the beginning of the millisecond count.

My.Computer.FileSystem

Fully improve the complexity of file operations. The FileSystem object provides an easy-to-understand way of doing things. The method of copying files in a FileSystem object not only requires specifying the destination path, but also helps you establish levels that do not exist in the target directory. It also provides copydirectory features that can replicate the entire directory! This is what is currently lacking in the. NET framework. At the same time filesystem can also provide the ability to search the parent, subdirectory or root directory, very thoughtful.

Put a file into the Recycle Bin under an animated demo

Text file contents can be read in one line of code


Of course, most of the functions of filesystem objects can also be accomplished through the System.IO.FileSystem class, which is similar to C # or better for C #.

My.Computer.Info

Get the total number of native physical or virtual memory, the remaining amount, the operating system name, the current user name, the native installation culture settings, and so on, all of which make it easy to use the Info object, which allows you to know your application's system at your fingertips.

My.Computer.Keyboard

and My.Computer.Mouse

Quickly get information about the user's keyboard, such as CAPS LOCK, digital keypad lock, etc., and whether the mouse has several keys, whether it is equipped with a scroll wheel, etc. If you want your application to be the most thoughtful user, then this information is indispensable

Gets the user's left and right mouse button function Exchange



My.Computer.Network

The most commonly used network task is to ping an address or detect if the network is connected by just one line of code.

Example of downloading a file

My.Computer.Port

Provides a line of code to open the local serial port function, but also immediately binding an event to monitor the serial port changes. Now the serial port programming is surprisingly simple, no longer need mscomm control.

My.Computer.Printers

Be able to traverse all the printers installed on this machine and find out the default printer. You can start printing by drawing the same actions to the default printer. This will remind you of the convenient and concise print operation of the VB6 era. The following example prints an ellipse on the default printer. When upgrading a project from VB6, the original printer object will be automatically upgraded to the relevant action in My.Computer.Printers, and the upgraded user can be more assured.

Print an ellipse on the default printer

My.Computer.Registry

It's much simpler than that version in the Microsoft.Win32 space, he provides strong-type path support, and it's very easy to read and write to the registry.

How to tell if a key value exists

My.Computer.Screen

Gets the visual range of the screen, the number of pixels, and so on. Stronger than the VB6 screen object, it now supports two displays.

My.Resources is not a class library, but is the only child namespace in the My namespace. is a strongly typed encapsulation of project resources, making the use of resources very simple.

My.User is the smallest member of the My namespace and is related to the Thread.CurrentPrincipal property. Simply provide the user name and role information, if you want to get the current login user name, only need to enter the My.User.Identity on the line.

My.Forms feeling is to give VB6.0 users back a previous form programming mode, because My.Forms is in the My namespace, but using it does not require input my.forms. And My.Forms maintains a default instance for each form in the project, which is implemented much like the singleton pattern-each form has a default instance and a global access point that is accessible through the form's class name. Suppose you have two forms--form1 and Form2,form1 are startup forms, and now you want to display Form2 in code, just form2.show. To modify the text of a textbox in the Form1 in Form2, you only need to do this: Form1.textBox1.Text = "Hello". The function of my.forms is to solve the best mode of form exchange, and not to waste memory, because it is only established when the form is first accessed.

The my.webservices principle is the same as my.forms. WebService there should be a consistent access point for the project globally, so VB2005 will instead create an instance of the proxy class and maintain it in my.webservices, which you can access at any time.

Extending the capabilities of the My namespace

Putting a class or module directly into my namespace is a convenient way to do so simply by defining the class or module in the My namespace, for example:

The simplest way

Then you can directly use My.tools to access the custom module, this is to make the function static, but all the methods are static is not the best method, so the recommended practice is: first we want to define a custom class, can be placed anywhere, without putting into the My namespace, This prevents the class name from appearing directly after the My keyword. Then, under the My namespace, define a module with HideModuleNameAttribute, the name can be random, and finally set the properties that access the custom class instance in the module.

The partial keyword can be used to extend the functionality of My.Application or My.Computer. The corresponding classes are MyApplication and MyComputer respectively.

Use My.Application in C # to use My.Application, you must inherit System.Windows.Forms.WindowsFormsApplicationBase (This class is located in Microsoft.VisualBasic.dll). After inheritance we do a few things, first write the constructor initializes the MyApplication class, in Visual Basic these settings are IDE-generated, and C # requires manual writing. Then override the Oncreatemainform method, which sets the main form of the current application. Next write the My class, giving him a global access point to the MyApplication class. Finally rewrite main, in which the implementation of My.Application.Run () instead of the original Application.Run (), as follows.

C # using My.appliation

Modify the main function

[STAThread]
static void Main ()
{
Application.enablevisualstyles ();
Application.enablertlmirroring ();
Application.Run (New Form1 ());
Properties.My.Application.Run ();
}




Finally, put your own little bit of code for the event machine to paste in this, is your own note, and another with a pen and casual too much, see this is a VB VS C # Anyway, want to place in this also not tasted:

VB Event Machine system

C # event Machine system

C#/vb. NET syntax for comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.