Use VB. NET to speed up code development

Source: Internet
Author: User
Tags try catch

When I was at school, I wroteCodeThey all use C # And get used to C # code habits. After work, they gradually begin to adopt VB because of their work needs. net Development Project, gradually like VB. net, now I will list some VB.. Net to accelerate code development.

I. Smart sensing

do. many developers of net know that as Microsoft. net in the two major languages VB. net and C #, VB. net is much better than C # in Intelligent Sensing.

1. Auto-completion. if you enter VB. net keyword. When you press enter, Vs will automatically help you complete the remaining code.

for example, if you press enter after entering try, VB. NET will automatically generate it.

 try catch ex as exception end try 

in this way, the Program personnel can directly write the business logic Code (VB. net keyword will have a similar effect, you can try it yourself)

of course, snippets can also be used in C #, but the snippets of C # is much less than VB.

VB. NET

C #

2. Automatic Code formatting

In VB. if you press enter in. net, the current document will be automatically formatted. You do not need to manually press Ctrl + k in C, CTRL + f (although a piece of code written in C # will be automatically formatted, the effect is far inferior to that of VB.. Net), this automatic formatting reduces a lot of work in the code writing process, and if you encounter a person who does not like to format the code, VB. net can automatically help with formatting, which makes it easier for later users to read the code.

 

3. Smart sensing

In. net3.5, with the addition of LINQ, we often write such code, such:

VB. NET

C #

 

We can clearly see that VB. net can automatically infer the type of the LINQ variable from the LINQ statement, but C # does not. This kind of prompt is very useful when we are writing complicated LINQ statements, in addition, when traversing objects such as foreach, VB. net can automatically infer the type of the variable, but C # is not required to manually specify the type of the variable.

At the same time, in VB. net, there is another advantage is not case sensitive, for example: we are doing ASP. NET development in some cases, because there is no smart perception prompt, it is often necessary to rely on memory to write code, such as writing JavaScript on the page:

VaR TXT = Document. getelementbyid ("<% = text1.clientid %> ");

To obtain the Client ID of the text box. text1.clientid must be written in C #; otherwise, an error is returned, while VB. NET can be written as text1.clientid.

For example, in the C # compiling process, we will write using system; to reference the namespace. However, if you write using system directly. then press ". "The Smart prompt will not have any prompts. It should be the first letter of the system in lower case. You must delete the original system and write the correct first letter in upper case of the system, this kind of smart sensing cannot be prompted normally in C #.(Of course it has been well optimized in vs2010)But VB. NET won't have such a problem. Every step of your writing can be correctly identified regardless of the size of your writing.

Of course, this kind of intelligent perception is reflected in many aspects of VB. NET.

4. namespace Import

In C # Development, I am suffering from importing namespaces. Whenever I create a class, I may need to import the namespace multiple times, in such a project, hundreds of classes have to be repeatedly imported into many namespaces. While VB. NET solves this problem very well. When you create a new VB. NET class or module, you will find this class very clean:

Public class aend class

Only the class is defined, and there are not several necessary namespaces imported by default like C #, such as system and system. text, etc. This is because VB.. Net project has been referenced by you. For winform projects, you can view the project property references. For Asp.net projects, you can view them on the web. config:

<Pages enablesessionstate = "true"> <namespaces> <clear/> <add namespace = "system"/> <add namespace = "system. collections "/> <add namespace =" system. collections. generic "/> <add namespace =" system. collections. specialized "/> <add namespace =" system. configuration "/> <add namespace =" system. text "/> <add namespace =" system. text. regularexpressions "/> <add namespace =" system. LINQ "/> <add namespace =" system. XML. LINQ "/> <add namespace =" system. web "/> <add namespace =" system. web. caching "/> <add namespace =" system. web. sessionstate "/> <add namespace =" system. web. security "/> <add namespace =" system. web. profile "/> <add namespace =" system. web. ui "/> <add namespace =" system. web. UI. webcontrols "/> <add namespace =" system. web. UI. webcontrols. webparts "/> <add namespace =" system. web. UI. htmlcontrols "/> </namespace> <pages>

You can also reference namespaces shared by projects here.

 

Ii. Feature syntax, comparison with C #

After introducing some smart sensing features of VB. NET, I will compare the feature Syntax of VB. NET with C #.

1. End of code

VB. net has the biggest benefit. By default, VB. net line feed indicates the end of a line of code, and does not need to write ";" like C #. To some extent, it can slightly improve the writing speed of some code.

2. Anonymous type

At the beginning of. net3.0, C # introduced the VaR keyword, which makes it easier for programmers to define variables. The corresponding VB. NET keyword also has dim.

To create an anonymous type in C:

VaR people = new {name = "Hangzhou", age = 22 };

VB. NET:

Dim people = new with {. Name = "Hangzhou",. Age = 22}

 

3. xml

In VB. when net9.0 is released, VB. net's XML writing method makes programmers shine at the moment. In the program, it can also be as simple as writing XML, and XML operations can be performed in programming form. Now we can compare VB. net and C # Writing XML

VB. NET:

 
Dim xml = <shortles> <people> <Name> keywords </Name> <age> 22 </age> </People> </shortles>

C #:

 
VaR xml = new xelement ("Les", new xelement ("people", new xelement ("name", "users"), new xelement ("Age ", 22 )));
 

We can see that the VB. Net Writing Method fully implements the XML structure, which can be understood at a glance, and C # can be seen only by good formatting.

Let's look at the XML operation example of VB. NET in msdn:

 'place imports statements at the top of your program. imports 
   
     module sample1 sub sampletransform () 'create test by using a global XML namespace prefix. dim contact =_< ns: Contact> 
    
      Patrick Hines 
     
    
      206-555-0144 
     
    
      425-555-0145 
      dim phonetypes =_< phonetypes> <% = from phone in contact. 
    
     _select 
     
       <% = phone. @ ns: Type %> 
      _ %>  console. writeline (phonetypes) end subend module 
    
   
The result of this Code is to query and output all phone types of the contact. For details about the principles, refer to msdn, which has been detailed in msdn.
 
 

4. Optional parameters and optional Real Parameters

In the existing VB. NET, the optional parameters are available. The optional real parameters are the two features, and C # is added in 4.0:

VB. NET:

Optional parameters:

 
Public sub Suba (byval A as string, optional byval B as integer = 0) A = B. tostring () Suba ("Hangzhou") Suba ("Hangzhou", 2) end sub

Optional parameters:

 
Suba (A: = "Hangzhou", B: = 2)

C #:

Optional parameters:

 
Public void a (string a, int B = 0)
 
{
 
A ("success ");
A ("success", 2 );
}
 
Optional parameters:
 
A (A: "Hangzhou", B: 2 );

5. Syntax readability

Comparing the keywords of C # and VB. NET, I think the syntax of VB. NET is closer to the natural language of human beings, and it is more readable. You can compare VB in msdn. net and C # keywords, you will find VB. although the keyword of. NET is too long, it is more understandable. For more information, see VB. net keyword and C # keyword

Iii. Limitations of VB. NET

After talking about so many advantages of VB. NET, I am talking about the shortcomings of VB. NET, and I personally think these shortcomings are also quite uncomfortable:

1. Code coloring

C # code coloring is very comfortable when you look at it. Various Types and keywords are distinguished by different colors, allowing you to know what it is, and VB. net, the whole code has three colors (excluding the color of XML programming), the blue of the keyword, the black of the common code and the red of the string.

VB. NET:

C #:

You can see it by comparison!

 

 

2. code structure

In C #, "{}" is used to indicate the beginning and end of the code. It looks very concise, while VB. net, for example, a method starts with sub and ends with end sub. It can also be accepted when the code is too large (Fortunately, VB. net Automatic formatting is very good)

VB. net also has a hard injury: # region code folding. in C #, # region can appear anywhere, and any code that needs to be folded can be folded in the method body, it can also be in addition to the method, while VB. net does not work, # region must be outside the method, only the method can be folded, not the code in the method, so if the code in a method is relatively long, in this way, you cannot perform segment collapse.

 

Iv. VB. NET 10.0

VB. net10.0 has been released, and the 10 changes made this time are huge. It integrates the advantages of C # And many 3.0, which is exciting.

1. Automatic attributes

before 10.0, VB. NET was difficult to write attributes, for example:

 private newpropertyvalue as string public property newproperty () as string get return newpropertyvalue end get set (byval value as string) newpropertyvalue = value end set end property 

although snippets can be used to reduce input, it is still quite troublesome, but automatic attributes are introduced in 10.0. Just like C #3.0, one line is required:

 Public Property A as string =" "

2. Implicit line feed

Write VB. net has a relatively painful thing, because VB. net indicates the end of a line of code with a carriage return, but sometimes the code is too long to be written in several lines for easy reading. "_" is required at the end of each line to indicate line feed, in this way, sometimes an error is reported when you forget to write the code, but you can cancel the damn "_" in 10.0 to directly wrap the line. In this way, you do not need to consider the line feed factor when writing the code.

3. Local methods and functions

. In net3.0, lambda expressions are introduced. C # can be expressed by "=>", while VB. net is represented by function (x) Code, which greatly limits VB. net Lambda expression, because function (x) indicates that a function must have a return value, so that action <t> in VB.. Net cannot be used through lambda expressions. In 10.0, local methods and functions are added:

Dim action = sub () end subdim func = function () end Function

 

In this way, the lambda Expression Function of VB. NET is truly perfect.

This article is now complete, although it only introduces some VB. net, there are many other basic features of VB. NET features are not mentioned. For example, my keyword, implicit conversion, CINT (cxx) conversion, Microsoft. visual Basic namespace and so on. We still need to study it in daily use. I hope you will really like Visual Basic.. net.

if this article is difficult, please forgive me because I am a newbie.

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.