Summary of VB. NET syntax

Source: Internet
Author: User
To learn how to walk, first learn to import namespaces.
Imports System

1. (1) define a variable and initialize it.
Dim strText As String = ""
Or
Dim strText As String
StrText = ""
(2) instantiate a class or object
Dim openFileDialog1 As New OpenFileDialog ()
Dim clsClass As New ClsClass ()
(3) declare an array
Dim fileNames As String () = New String (3 ){}
FileNames (0) = "Zhang San"
FileNames (1) = "Li Si"
FileNames (2) = "Wang Wu"
(4) declare a generic type
Dim list As New List (Of T )()

Ii. Exception Handling.
Try
Catch ex As Exception
Throw New Exception (ex. Message, ex)
End Try

Iii. Condition judgment statement
If... Then
...
ElseIf... Then
...
Else
...
End if
Iv. Loop array and set
For I As Integer = 0 To openFileDialog1.FileNames. Length-1
// Exit
Next

For Each item In List
// Exit
Next

V. Process and Function
Public Sub function name (ByVal/ByRef parameter name As Integer) as type

End Sub
Public Function Name (ByVal/ByRef parameter name As Integer) as type
'Vb6. 0: function writing with return values
Dimstr1 As String = "111"
'Result = str1
'You can also use the key value Return in vb.net to Return.
Dimstr2 As String = "222"
Return str2
End Function
6. modules in vb.net are generally used only to define global variables and public methods.
Class ClassName
Public shared Function fnXXX () as string' shared indicates that the Static method is equivalent to the Static keyword in C.
...
End Function
End Class
Similar
Use ClassName. fnXXX () directly. You can also call fnXXX () directly.
Module ModuleName
Public Function fnXXX () as string
...
End Function
End Module
Call: ModuleName. fnXXX (). You can also call fnXXX () directly.
Bytes -------------------------------------------------------------------------------------------
There can be direct methods in the Module. These methods are automatically static (or shared-by-VB.. NET), these methods can be called directly, so the Module cannot be instantiated, and there is no need to instantiate
The module cannot inherit or implement interfaces.
There can be classes in the module, but in fact this class does not need to be referenced by the module as a prefix. So the module is a bit virtual.
A module can be organized into a namespace. Because a module is actually virtual, its method can also be called directly in an outer namespace.




7. Use the same event for multiple identical events.
VB. NET
Private Sub rdbFeedChannelAny_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles rdbFeedChannel1.Click, rdbFeedChannel2.Click, rdbFeedChannel3.Click

Exit Sub

C #
Designer
This. radioButton1.CheckedChanged + = new System. EventHandler (radioButton_CheckedChanged );
This. radioButton2.CheckedChanged + = new System. EventHandler (radioButton_CheckedChanged );

Code;
Private void radioButton_CheckedChanged (object sender, EventArgs e)
{
If (this. radioButton1.Checked)
{

}
}
8. Use the Overloads keyword to implement overload functions.
Class1
Overloads Public Sub New () // constructor.
......
......
End sub
Overloads Public Sub New (s as string)
......
......
End sub
End Class
IX,
Withevents is used to declare an object. The object declared with withevents has its own event. When RaiseEvent exists in this object, it is received externally.

Handles is used to receive the withevents object event. For example, if your object has an event A, and the RaiseEvent A object is executed, the SUB of the caller's handles object. A will be executed.

 

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.