VB.net new features, routines (translations)

Source: Internet
Author: User
Tags define get integer numeric new features tostring visual studio advantage
7. Introduction vb.net
VB.net or VB7 has many of the same syntax as the old version of Visual Basic, but in many ways it is a completely new language. And the previous version of VB is not the same, VB7 is completely object-oriented, some ordinary operation of the implementation because of this difference and there are some differences. For this reason, instead of compiling it as a compiler to compile your already written program, it is better to have VB. NET as a language for developing. NET applications. Due to VB. NET format is still immature, so in this article when we talk about vb.net, we will use VB7 to name vb.net. (We will continue VB.net's name in the title), however, in this chapter we will see the advantages of VB7. In the next chapter, we'll see how simple it is to customize design patterns to build useful object-oriented VB programs.
Vb. NET syntax is different
The main differences in this version are that those subroutines and class methods must be prefixed with parentheses. We can write this in VB6:
Dim MyCol as New Collection
Mycol.add "Mine"
However, in VB7, you must add parentheses to the parameters:
Dim MyCol as New ArrayList
Mycol.add ("Mine")
Another significant difference, for most people, is an improvement, and the parameter passing process defaults to the transfer of values instead of the address. In other words, you can manipulate a variable in a subroutine without having to worry about changing the value of the variable in the program that calls it. In other words, the "ByVal" that was previously to be corrected is now the default. In fact, in most cases, the development environment automatically inserts a value parameter from your program into the calling subroutine, and if you want to change the value in the program, you can still declare it to be a parameter, fixed to the address.
Four other VB6 keywords have also been removed or significantly changed: Set, Variant, Wend, EndIf. In fact, the development environment simply removes the verb set at the beginning of any line where you use it.


From now on, the Dim declaration allows you to list several variables that belong to the same type in a single declaration at the same time.
Dim x, y as Integer
You can no longer list different types of variables in the same single statement.
Dim X as Integer, Y as single ' illegal in VB7 (illegal in VB7)
You must now list them in a different line.
Dim X as Integer ' legal in both VB6 and VB7
Dim Y as Single
The VB7 compiler will mark many of the VB6 on the construction usage as incorrect. However, it simply ignores the additional "as" declarations in the same dim declaration line. In addition, the character function InStr, left and right have been replaced by the character class method indexOf and substring, making it more versatile. Note Those methods that use 0 as the index of characters.
VB6 VB7
Instr (S, ",") S.indexof (",")
Left (S, 2) s.substring (0,2)
Right (s, 4) s.substring (S.length () 4)
Improved function syntax
In VB, when you return a value from a function, you need to provide the name of the function
Public Function Squarit (x as Single)
Squarit = x * x
End Function
In VB7, this restriction is eventually canceled, and you can simply return the value of the function as you would in other languages:
Public Function Squarit (x as Single)
return x * x
End Function
This makes the function easier to define and use.
The objects in the vb.net
In VB7, anything is treated as an object. When in VB6, you can create a class instance and use it as an object. objects can be manipulated through the contained data and methods. In VB7, all the variables are really like this. For example, characters are also objects, and they have this method:
Substring
toLowerCase
toUpperCase
IndexOf
Insert
ToSingle
ToInt32
A bit further, integers, single and Double are objects, and they have methods
Dim S as String
Dim x as Single
x = 12.3
s = x.tostring
In fact, you can even treat constants as objects.
Dim Snum as String
Snum = 9.toString
Note that now the conversion of character and numeric types can use object methods, and do not need to use the Val and STR functions as before. If you want to format a number as a character, each numeric type has a formatting method: Dim S As StringDim x as single
x = 12.34
s = Single.format (x, "##.000")
Vb. NET in the number
All numbers with no decimal points are assumed to be Int32 types, and all decimal points are assumed to be of double type. If you want to define a single variable as a decimal, you must follow a "F" (floating point) on the back of the number.
Dim time as Single = 123F
Generally speaking, VB. NET compiler requires a variable declaration to prevent most of the unknown type conversions (except the converted to a wider type). You can always convert integers to single and double types. However, if you want to convert single to Integer, you have to make a clear point.
Dim k as Integer = time. ToInt16
Properties of VB6 and vb.net
Visual Basic provides a structure called attributes, similar to the GetXXX and Setxxx methods in other languages. In VB6, you can specify a property by name, and define get and let methods. These two methods allow you to assign a value to a private variable and allow you to obtain its value.
Property Get Fred () as Stringfred = Frednameend Property
Property Lets Fred (S as String) Fredname = SEnd Property You can, of course, make the data available in the pre-and post-processing or convert it into the form you want.
In VB7, these attributes are grouped into a single program:
Property Fred () as Stringgetreturn frednameend getsetfredname = ValueEnd SetEnd Property
Note these key values. We will use it in VB7 to be passed the value of the property. So if we write this
abc.fred = "Dog"
Then, when the set code in the property is executed, the value of the character variable will contain "dog".
In both systems, this effect is used to provide a simple interface that can extract data from a class or store data in a class without knowing how exactly it is stored. You can use the properties of these shared assignment declarations. If "Fred" is an attribute of a boy class, then you can write a statement like this:
Dim Kid as Boy
kid.fred = "smart"
And then
Dim Brain As String
Brain = kid.fred
In general, a property system provides an alternate syntax that allows you to write getfred and setfred functions effortlessly. When the syntax is not the same, there is no obvious advantage in addition to local VB objects than get and set methods. In this book, we will not use this attribute too much, as they are not a significant advantage in writing object-oriented program code.
Shorthand Equality grammar
VB7 uses the shorthand equality syntax that we can find in c,c++,c# and Java. It allows you to write a variable name two times without adding, subtraction, multiplying, and using a constant.
i + 5 ' add 5 to I
I-= 8 ' Subtract 8 Form I
I *= 4 ' Multiply I by 4
You can use this approach to reduce the code, but it does have the same effect as the code you wrote in the old way.
i = i + 5
i = i-8
i = i * 4
This same application can also be used for division, but you hardly see this usage because they are less readable.
Managing languages and Memory recycling
Both vb.net and C # are administrative languages. They have two important implications. First, they are compiled into an intermediate low-level language, a common language runtime (CLR) is used to execute compiled code, and perhaps compile earlier. Therefore, not only VB7 but also C # share the Run-time library, they are largely like two sides of a coin, two aspects of a language system. Their difference is that VB7 is more like visual Basic, and it's easier to learn and use. C # is more like C + + and Java, and programmers with these language experiences are more inclined to use C #.
The implication of another management language is memory recycling. The memory recycle language will automatically release unused memory so that you don't have to care about the problem. When the memory recycle system discovers a variable, an array, the object is no longer in use, it releases the memory to the system. You don't need to be nervous about not having enough memory to allocate memory without releasing it in time. Of course, a lot of memory-consuming code is still possible, but in most cases you don't have to worry about allocating and freeing memory.
Vb. NET in the class
Class is a very important part of VB7. Almost every important program contains one or more classes. The distinction between classes and forms in VB7 has disappeared, and many programs will all consist of classes. Since everything is a class, the number of names of class objects will be uncontrollable. They are grouped into different functional library files, and you have to be very specific about where they are when using the library.
At a deeper level, these libraries are separate dynamic link libraries. However, you just need to use their base name to refer to them, and then you can use all of the functions in these libraries.
Imports system.collections ' Use Collection namespace classes
Theoretically, each library has a different namespace, and each namespace contains a separate group of class and method names, and when you reference the namespace, the compiler will recognize the class function or method you want to use. But after you have referenced multiple namespaces that contain the same class and method names, you will be told that there is a conflict when you try to use a function or method that has the same name in more than one namespace.
The most common namespace is the system internal namespace, and you do not need to reference these default namespaces. It contains a number of basic classes and methods that will be used in VB7, such as applications, arrays, consoles, exceptions, objects and bytes, logic, single-precision and double-precision numbers, characters, and other standard objects. In the simplest VB7 program we can print "Hello VB World" on the console without the need to create a window:
Imports System ' This one is optional
' Simple VB Hello World Program '
Public Class Cmain
Shared Sub Main () ' entry point is main
' Write text to the console
Console.WriteLine ("Hello VB World")
End Sub
End Class
A program simply prints "Hello VB World" on the Command (DOS) window. The entrance to any program must be a main function, in a module, it must be declared as shared. The former module type in VB7 has only a type of modules. The program can write this:
' Simple VB Hello World Program '
Public Module Cmain
Sub Main () ' entry point is Main
' Write text to the console
Console.WriteLine ("Hello VB World")
End Sub
End Module
Except that a module has a common (shared) method, the main function does not have to be declared as shared, and everything is the same. The modules in VB7 are similar to the previous modules, and they are all generated by VB with a. bas type of file. They have an advantage that the method and variable constants that are declared in the module can be used throughout the program. However, unlike classes, they do not provide a way to hide information and algorithms, which are no longer described and used below.
In VB6, a class name usually declares a filename, although you can modify the properties of the class name. In VB7, the keyword of a class allows you to do so without considering the filename, in fact, you must declare a class name for each class. The default extension for a class in VB6 is. frm, the default extension of the. CLS form. You can use any extension in VB7, but the default is. vb.

Build a VB7 Application
Let's start with building a simple console application: It's a window-less application that he runs only on a command-line basis. Open the Visual Studio.NET program and select file| New Project. Select the console program from the selection box as shown in Figure 7-1:


Figure 7-1 Select a console application in the selection box for the new project
This will generate a module that already has the main procedure, now add the following code:
Module Cmain
Sub Main ()
' Write text to the console
Console.WriteLine ("Hello VB World")
End Sub
End Module
You can press F5 to compile and execute the program. If you want to change the name of the main module of the program, as we do, from Module1 to Cmain, you will have to change the module name in the Startup module option. To do so, click the project name in the Solution Explorer floating surface and select the Properties option from the pop-up menu, and the property page for the project in Figure 7-2 will appear:

You can choose the correct new name in the Pull-down menu. When you press F5 to compile the program, a DOS will appear and print the message "Hello VB World" and then exit.
You can also delete the module by inserting a class instead.
Public Class Cmain
Shared Sub Main ()
Console.WriteLine ("Hello classy VB World")
End Sub
End Class
This can be compiled and run in the same way.




Figure 7-2 Property page for Project

Write the simplest window program in vb.net
It's easy to write a program that has a form frame. In fact, you can use Windows Designer to create most applications. To do this, open Visual Studio.NET, select file| New project, then select Windows Application. The default file name is WindowsApplication1, but you can change the name before you close the New dialog box. This creates a simple project that initializes the Form1.vb form. You can then use the toolbox to add controls to the form as you would in VB6.
In Figure 7-3, the form design has a simple form that contains two labels, a text box, and a button. You can place controls on a form and double-click Controls to enter code design. On this simple form, we click the "Say Hello" button, and he copies the contents of the text box to the blank label we named Lbhi, and then empties the text box.
Protected Sub Sayhello_click (ByVal sender as Object, _
ByVal e as System.EventArgs)
Lbhi. Text () = Txhi. Text
Txhi. Text = ""
End Sub
Code generation may be a little different. Note that the click Action passes in a Receive object (Sender) and an event object (e) so that you can ask for further information. The Run-time program is shown in Figure 7-4.




Figure 7-3 Visual Studio.NET Form Design




Figure 7-4 The effect of the simple Hello program's pressing the Say Hello button
Although we only need to write two lines of code to the above subroutine, it is meaningful to look at the rest of the code in the program and the differences in the previous one. First we saw that several classes of libraries were introduced to the program so that the entire program could use them:
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
The most important thing is to winforms this class library, which is the whole. NET in common use. You can use VB Form Design in C # (C-SHARP) and C + +.
Inherited
Next, we have to look at the best change in VB-7: inheritance.
Public Class Helloform
Inherits System.WinForms.Form
The form we are building now is a subclass of the form class, not just an instance of a class, as in previous versions of VB. This has a lot of meaning. You can create visual objects by overloading the properties to make them non-functional. We'll look at some examples in brief.



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.