VB.Net是一種簡單,現代,面向對象的計算機編程語言,由微軟開發,將.NETFramework和公共語言運行庫的強大功能與作為VisualBasic標志的生產力優勢相結合。
Visual Basic .NET(VB.NET)是在.NET Framework上實現的面向對象的計算機編程語言。 雖然它是經典的Visual Basic語言的演變,但是它不兼容VB6,在任何舊版本編寫的代碼不能在VB.NET下編譯。
像所有其他.NET語言一樣,VB.NET完全支持面向對象的概念。 VB.NET中的一切都是一個對象,包括所有的基本類型(Short,Integer,Long,String,Boolean等)和用戶定義的類型,事件,甚至是程序集。 所有對象從基類Object繼承。
VB.NET是由Microsoft的.NET框架實現的。 因此,它可以完全訪問.Net框架中的所有庫。 也可以在Mono上運行VB.NET程序,Mono是.NET的開源替代軟件,不僅在Windows下,甚至在Linux或Mac OSX上。
以下原因使VB.Net成為廣泛使用的專業語言:
VB.Net有許多強大的編程功能,使世界各地的程序員都很喜歡使用它。 讓我們看一下這些功能:
本教程將教你基本的VB.Net編程,也將帶你學習與VB.Net編程語言相關的各種高級概念。
本教程是為初學者準備的,幫助他們了解基本的VB.Net編程。完成本教程后,您將發現自己在VB.Net編程中達到一個中等水平的專業知識,你可以從自己通過其他方式學習到下一個級別。
在您開始學習本教程之前需要先學習以下知識:
VB.Net編程很大程度基于BASIC和VisualBasic編程語言,所以如果你對這些編程語言有基本的了解然后再學習VB.Net你會發現VB.Net編程語言的樂趣。
VisualBasic.NET2002
2002年VisualBasic.NET問世,此后VisualBasic包含在VisualStudio套裝中。該版本又被稱為VB7.0,是與C#一起在2002年發布的最初始的VB.net版本。C#是一門新設計.Net語言并且語法上和Java有一定的相似性,所以被大力宣傳為微軟對付JAVA的王牌,受到媒體極力關注。然而VB.NET則相反被錯誤地認為僅僅是VB的一個升級(故有時被稱為VB7),它的受關注度也因此較低,當然VB社區以及喜歡Basic系列風格的語言的人還是很關注它的。那些試用過此版VB.NET的人在層層幕布下發現一個強大卻艱深的語言。不過任何一種語言都是有弱點的,當然后面這一點也是.NET語言的共性,那就是運行的計算機上必須裝相應版本的.Netframework。
VisualBasic.NET2003
該版本是和.NETFramework1.1一起發布的。新功能包括對.NETCompactFramework的支持和更好的VB升遷向導。并改進了運行狀況,IDE穩定性(尤其是后臺編譯器),以及運行時RunTime穩定性。另外,VB.NET2003還有一個學院版VisualStudio.NET2003AcademicEdition(VS03AE),它是分發給各國某些學者免費使用的版本。
VisualBasic.NET2005
該版本是VB.Net的重大轉變。但是這個版本的VisualStudio仍然還是面向.NET框架的(版本2.0)。它同時也能開發跨平臺的應用程序,如開發使用微軟操作系統的手機的程序等??傮w來說是一個非常龐大的軟件,甚至包含代碼測試功能。
VisualBasic.NET2008
VisualBasic2008于2008年2月1日發布。通過幾年的發展,它已成為一種專業化的開發語言和環境。用戶可用VisualBasic快速創建Windows程序,現在還可以編寫企業水平的客戶/服務器程序及強大的數據庫應用程序。
VisualStudio2010
VisualStudio2010于2010年4月12日上市,其中包含VisualBasic.NET2010。VisualStudio是微軟公司推出的開發環境。是目前最流行的Windows平臺應用程序開發環境。VisualStudio2010集成開發環境(IDE)的界面被重新設計和組織,變得更加簡單明了。VisualStudio2010同時帶來了NETFramework4.0、MicrosoftVisualStudio2010CTP(CommunityTechnologyPreview–CTP),并且支持開發面向Windows7的應用程序。除了MicrosoftSQLServer,它還支持IBMDB2和Oracle數據庫等。
VisualStudio2012
從該版本開始VB.NET已沒有單獨發售的IDE了,微軟給VisualBasic.NET以及C#開發者的IDE就只有VisualStudio了。該版本增加了modern(原metro)風格的支持等眾多新特性。
VisualStudio2015
增加了大量新特性,提升你的編寫效率。對編譯器和開發環境優化,提高編譯速度,幫你重構代碼和解決錯誤。最明顯的14項改進見參考資料的14TopImprovementsinVisualBasic14。
VB關系
不能簡單認為VB.net是VB的新版本。首先它們一個是完全面向對象的語言,而另一個不是完全面向對象的,其次VB.NET是構建于.Netframework之上的。當然,它們同屬Basic系列語言,又同為微軟所開發,語法上有一定的相似或沿襲是很正常的,但不能因此認為VB.NET是VB的升級版。
對于想從VB轉到VB.NET的開發者來說,如果你只是喜歡Basic系列語言的代碼風格,那么你的遷移會很順利,如果你想沿襲之前VB的編程思想和習慣,你會很苦惱,原因之一就是VB.NET貫穿的是完全面向對象的思想,而VB不是。
微軟為使VB開發者更容易轉到VB.NET,兼容一些VB6函數和庫的用法,但是比不上.NET語言中自帶的可替換的函數和庫來得高效,所以我們應該盡量使用.NET下的新方法。微軟還提供了VB到VB.NET的自動轉換器,雖幾經改進,但仍無法轉換全部代碼,幾乎所有非小型程序都需要人工修改以完成編譯。為了運行優化,大多程序需要大量工作去重構。
爭議與解釋:許多資深的VB程序員抱怨VB.NET,因為VB.NET丟掉了許多不再發行的VB6中使用的大量語言constructs和用戶界面功能,并且對保留下來的東西也改變了語意。其實不應該再把VB和VB.NET做功能上的比較,因為它們除了有些語法相似外,可以說是完全不同的兩種語言。
VisualBasic.NET與VB都代表了Basic系列語言的編碼風格,VisualBasic.NET是這種編碼風格在.NET平臺上的繼承,而不是同系列語言VB的沿襲。
本教程共由3個章節構成,詳細講述了vb.net的基礎使用方法并附帶了大量示例輔助您的學習,讓您輕松掌握VB.Net。
本教程包含:
1、《VB.Net教程-基礎語法》
2、《VB.Net教程-常見對象》
3、《VB.Net教程-面向對象》
4、《VB.Net教程-高級教程》
在本章中,我們將討論可用于創建VB.Net應用程序的工具。
我們已經提到VB.Net是.Net框架的一部分,用于編寫.Net應用程序。 因此,在討論用于運行VB.Net程序的可用工具之前,讓我們先了解VB.Net如何與.Net框架相關。
.NET Framework是一個革命性的平臺,可以幫助你編寫以下類型的應用:
Windows應用程序
Web應用程序
網頁服務
.Net框架應用程序是多平臺應用程序。 該框架的設計方式使其可以從以下任何語言使用:Visual Basic,C#,C ++,Jscript和COBOL等。
.Net框架包含一個巨大的代碼庫,用于客戶端語言(如VB.Net)。 這些語言使用面向對象的方法。
所有這些語言可以訪問框架以及彼此通信。
以下是.Net框架的一些組件:
公共語言運行時(CLR) Common Language Runtime (CLR)
.NET框架類庫 The .Net Framework Class Library
公共語言規范 Common Language Specification
通用類型系統 Common Type System
元數據和組件 Metadata and Assemblies
Windows窗體 Windows Forms
ASP.Net和ASP.Net AJAX
ADO.Net
Windows工作流基礎(WF) Windows Workflow Foundation (WF)
Windows演示基礎 Windows Presentation Foundation
Windows通訊基礎(WCF) Windows Communication Foundation (WCF)
LINQ
有關每個組件的詳細信息,請參閱微軟的文檔。
Microsoft為VB.Net編程提供以下開發工具:
1、Visual Studio 2010(VS)
2、Visual Basic 2010 Express(VBE)
3、可視化Web開發
最后兩個是免費的。 使用這些工具,您可以將各種VB.Net程序從簡單的命令行應用程序寫入到更復雜的應用程序。 Visual Basic Express和Visual Web Developer Express版是Visual Studio的精簡版本,具有相同的外觀和感覺。 它們保留了Visual Studio的大多數功能。 在本教程中,我們使用了Visual Basic 2010 Express和Visual Web Developer(針對Web編程章節)。
它會自動安裝在您的計算機上。 請注意,您需要一個有效的互聯網連接安裝快速版本。
雖然.NET Framework在Windows操作系統上運行,但有一些替代版本可在其他操作系統上運行。 Mono是.NET Framework的開源版本,包括Visual Basic編譯器,可在多種操作系統上運行,包括各種Linux和Mac OS。 最新版本是VB 2012。
Mono的既定目的不僅是能夠跨平臺運行Microsoft .NET應用程序,而且為Linux開發人員提供更好的開發工具。 Mono可以在許多操作系統上運行,包括Android,BSD,iOS,Linux,OS X,Windows,Solaris和UNIX。
在我們學習VB.Net編程語言的基本構建塊之前,讓我們看看一個最小的VB.Net程序結構,以便我們可以將它作為未來的章節的參考。
一個VB.Net程序主要由以下幾部分組成:
命名空間聲明 Namespace declaration
一個類或模塊 A class or module
一個或多個程序 One or more procedures
變量 Variables
主過程 The Main procedure
語句和表達式 Statements & Expressions
注釋 Comments
讓我們看一個簡單的代碼,打印單詞“Hello World”:
Imports System
Module Module1
'This program will display Hello World
Sub Main()
console.writeline("Hello World")
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
Hello, World!
讓我們來看看上面的程序中的各個部分:
程序Imports System的第一行用于在程序中包括系統命名空間。The first line of the program Imports System is used to include the System namespace in the program.
下一行有一個Module聲明,模塊Module1。 VB.Net是完全面向對象的,所以每個程序必須包含一個類的模塊,該類包含您的程序使用的數據和過程。The next line has aModuledeclaration, the moduleModule1. VB.Net is completely object oriented, so every program must contain a module of a class that contains the data and procedures that your program uses.
類或模塊通常將包含多個過程。 過程包含可執行代碼,或者換句話說,它們定義了類的行為。 過程可以是以下任何一種:
功能Function
子Sub
運算符Operator
獲取Get
組Set
AddHandler
RemoveHandler
的RaiseEvent
下一行(’這個程序)將被編譯器忽略,并且已經在程序中添加了額外的注釋。
下一行定義了Main過程,它是所有VB.Net程序的入口點。 Main過程說明了模塊或類在執行時將做什么。
Main過程使用語句指定其行為
Console.WriteLine(“Hello World”的)
WriteLine是在System命名空間中定義的Console類的一個方法。此語句會導致消息”Hello,World!”在屏幕上顯示。
最后一行Console.ReadKey()是用于VS.NET用戶的。 這將阻止屏幕從Visual Studio .NET啟動時快速運行和關閉。
如果您使用Visual Studio.Net IDE,請執行以下步驟:
啟動Visual Studio。Start Visual Studio.
在菜單欄,選擇文件,新建,項目。On the menu bar, choose File, New, Project.
從模板中選擇Visual Basic。Choose Visual Basic from templates
選擇控制臺應用程序。Choose Console Application.
使用瀏覽按鈕指定項目的名稱和位置,然后選擇確定按鈕。Specify a name and location for your project using the Browse button, and then choose the OK button.
新項目顯示在解決方案資源管理器中。The new project appears in Solution Explorer.
在代碼編輯器中編寫代碼。Write code in the Code Editor.
單擊運行按鈕或F5鍵運行項目。 將出現一個包含行Hello World的命令提示符窗口。Click the Run button or the F5 key to run the project. A Command Prompt window appears that contains the line Hello World.
您可以使用命令行而不是Visual Studio IDE編譯VB.Net程序:
打開文本編輯器,并添加上述代碼。Open a text editor and add the above mentioned code.
將文件另存為helloworld.vb。Save the file as helloworld.vb
打開命令提示符工具并轉到保存文件的目錄。Open the command prompt tool and go to the directory where you saved the file.
類型VBC helloworld.vb,然后按回車編譯代碼。Type vbc helloworld.vb and press enter to compile your code.
如果在你的代碼中沒有錯誤命令提示符下會帶你到下一行,并會產生HelloWorld.exe的可執行文件。If there are no errors in your code the command prompt will take you to the next line and would generate helloworld.exe executable file.
接下來,輸入的HelloWorld來執行你的程序。Next, type helloworld to execute your program.
您將可以看到“Hello World”字樣在屏幕上。You will be able to see “Hello World” printed on the screen.
VB.Net是一種面向對象的編程語言。 在面向對象編程方法中,程序由通過動作相互交互的各種對象組成。 對象可能采取的動作稱為方法。 相同類型的對象被認為具有相同的類型,或者更經常地被稱為在同一類中。
當我們考慮VB.Net程序時,它可以定義為通過調用對方的方法進行通信的對象的集合?,F在讓我們簡單地看看類,對象,方法和實例變量是什么意思。
Object對象-對象具有狀態和行為。 示例:狗有狀態 – 顏色,名稱,品種以及行為 – 搖擺,吠叫,吃飯等。對象是類的實例。Objects have states and behaviors. Example: A dog has states – color, name, breed as well as behaviors – wagging, barking, eating, etc. An object is an instance of a class.
Class類-類可以被定義為描述其類型的對象支持的行為/狀態的模板/藍圖。A class can be defined as a template/blueprint that describes the behaviors/states that objects of its type support.
Methods方法-方法基本上是一種行為。 一個類可以包含許多方法。 它是在寫邏輯,操縱數據和執行所有動作的方法中。A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
實例變量-每個對象都有其唯一的實例變量集。 對象的狀態由分配給這些實例變量的值創建。Each object has its unique set of instance variables. An object’s state is created by the values assigned to these instance variables.
例如,讓我們考慮一個Rectangle對象。 它具有長度和寬度等屬性。 根據設計,它可能需要接受這些屬性的值,計算面積和顯示細節的方式。
讓我們看一個Rectangle類的實現,并在我們的觀察的基礎上討論VB.Net基本語法:
Imports System
Public Class Rectangle
Private length As Double
Private width As Double
'Public methods
Public Sub AcceptDetails()
length = 4.5
width = 3.5
End Sub
Public Function GetArea() As Double
GetArea = length * width
End Function
Public Sub Display()
Console.WriteLine("Length: {0}", length)
Console.WriteLine("Width: {0}", width)
Console.WriteLine("Area: {0}", GetArea())
End Sub
Shared Sub Main()
Dim r As New Rectangle()
r.Acceptdetails()
r.Display()
Console.ReadLine()
End Sub
End Class
當上述代碼被編譯和執行時,它產生以下結果:
Length: 4.5
Width: 3.5
Area: 15.75
在上一章中,我們創建了一個包含代碼的Visual Basic模塊。 Sub Main表示VB.Net程序的入口點。 這里,我們使用包含代碼和數據的類。 您使用類來創建對象。 例如,在代碼中,r是一個Rectangle對象。
對象是類的一個實例:
Dim r As New Rectangle()
類可以具有可以從外部類訪問的成員,如果指定的話。 數據成員稱為字段,過程成員稱為方法。
可以在不創建類的對象的情況下調用共享方法或靜態方法。 通過類的一個對象調用實例方法:
Shared Sub Main()
Dim r As New Rectangle()
r.Acceptdetails()
r.Display()
Console.ReadLine()
End Sub
標識符是用于標識類,變量,函數或任何其他用戶定義項的名稱。 在VB.Net中命名類的基本規則如下:
名稱必須以字母開頭,后跟一個字母,數字(0 – 9)或下劃線。 標識符中的第一個字符不能是數字。A name must begin with a letter that could be followed by a sequence of letters, digits (0 – 9) or underscore. The first character in an identifier cannot be a digit.
它不能包含任何嵌入的空格或符號是怎樣的? – +! @#%^&*()[] {}。 ; :“’/和\。但是,可以使用下劃線(_)。It must not contain any embedded space or symbol like ? – +! @ # % ^ & * ( ) [ ] { } . ; : ” ‘ / and \. However, an underscore ( _ ) can be used.
它不應該是保留關鍵字。It should not be a reserved keyword.
下表列出了VB.Net保留的關鍵字:
AddHandler | AddressOf | Alias | And | AndAlso | As | Boolean |
ByRef | Byte | ByVal | Call | Case | Catch | CBool |
CByte | CChar | CDate | CDec | CDbl | Char | CInt |
Class | CLng | CObj | Const | Continue | CSByte | CShort |
CSng | CStr | CType | CUInt | CULng | CUShort | Date |
Decimal | Declare | Default | Delegate | Dim | DirectCast | Do |
Double | Each | Else | ElseIf | End | End If | Enum |
Erase | Error | Event | Exit | False | Finally | For |
Friend | Function | Get | GetType |
GetXML Namespace |
Global | GoTo |
Handles | If | Implements | Imports | In | Inherits | Integer |
Interface | Is | IsNot | Let | Lib | Like | Long |
Loop | Me | Mod | Module | MustInherit | MustOverride | MyBase |
MyClass | Namespace | Narrowing | New | Next | Not | Nothing |
Not Inheritable |
Not Overridable |
Object | Of | On | Operator | Option |
Optional | Or | OrElse | Overloads | Overridable | Overrides | ParamArray |
Partial | Private | Property | Protected | Public | RaiseEvent | ReadOnly |
ReDim | REM |
Remove Handler |
Resume | Return | SByte | Select |
Set | Shadows | Shared | Short | Single | Static | Step |
Stop | String | Structure | Sub | SyncLock | Then | Throw |
To | True | Try | TryCast | TypeOf | UInteger | While |
Widening | With | WithEvents | WriteOnly | Xor |
數據類型指用于聲明不同類型的變量或函數的擴展系統。 變量的類型確定它在存儲中占用多少空間以及如何解釋存儲的位模式。
VB.Net提供了多種數據類型。下表顯示的所有數據類型可用的:
數據類型 | 存儲分配 | 值范圍 |
---|---|---|
Boolean | 取決于實施平臺 | 真或假 |
Byte | 1個字節 | 0到255(無符號) |
Char | 2個字節 | 0?65535(無符號) |
Date | 8個字節 | 00:00:00(午夜),時間為0001年12月31日11時31分至晚上11:59:59 |
Decimal | 16字節 | 0至+/- 79,228,162,514,264,337,593,543,950,335(+/- 7.9 … E + 28),沒有小數點; 0到+/- 7.9228162514264337593543950335,其中小數點右邊有28個位 |
Double | 8個字節 |
-1.79769313486231570E + 308至-4.94065645841246544E-324,對于負值 4.94065645841246544E-324至1.79769313486231570E + 308,對于正值 |
Integer | 4個字節 | -2,147,483,648至2,147,483,647(有符號) |
Long | 8個字節 | -9,223,372,036,854,775,808至9,223,372,036,854,775,807(簽字) |
Object |
在32位平臺上的4個字節 在64位平臺8字節 |
任何類型都可以存儲在Object類型的變量中 |
SByte | 1個字節 | -128至127(簽字) |
Short | 2個字節 | -32,768至32,767(簽字) |
Single | 4個字節 |
-3.4028235E + 38至-1.401298E-45為負值; 1.401298E-45至3.4028235E + 38正值 |
String | 取決于實施平臺 | 0到大約20億個Unicode字符 |
UInteger | 4個字節 | 0至4294967295(無符號) |
ULONG | 8個字節 | 0至18,446,744,073,709,551,615(簽名) |
User-Defined | 取決于實施平臺 | 結構的每個成員具有由其數據類型確定的范圍并且獨立于其他成員的范圍 |
UShort | 2個字節 | 0至65,535(無符號) |
下面的示例演示使用的一些類型︰
Module DataTypes
Sub Main()
Dim b As Byte
Dim n As Integer
Dim si As Single
Dim d As Double
Dim da As Date
Dim c As Char
Dim s As String
Dim bl As Boolean
b = 1
n = 1234567
si = 0.12345678901234566
d = 0.12345678901234566
da = Today
c = "U"c
s = "Me"
If ScriptEngine = "VB" Then
bl = True
Else
bl = False
End If
If bl Then
'the oath taking
Console.Write(c & " and," & s & vbCrLf)
Console.WriteLine("declaring on the day of: {0}", da)
Console.WriteLine("We will learn VB.Net seriously")
Console.WriteLine("Lets see what happens to the floating point variables:")
Console.WriteLine("The Single: {0}, The Double: {1}", si, d)
End If
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
U and, Me
declaring on the day of: 12/4/2012 12:00:00 PM
We will learn VB.Net seriously
Lets see what happens to the floating point variables:
The Single:0.1234568, The Double: 0.123456789012346
VB.Net提供以下內聯類型轉換函數:
SN | 功能和說明 |
---|---|
1 |
CBool(表達式) 將表達式轉換為布爾數據類型。 |
2 |
CByte(表達式) 將表達式轉換為字節數據類型。 |
3 |
CChar(表達式) 將表達式轉換為Char數據類型。 |
4 |
CDate(表達式) 將表達式轉換為Date數據類型 |
5 |
CDbl(表達式) 將表達式轉換為雙精度數據類型。 |
6 |
CDec(表達式) 將表達式轉換為十進制數據類型。 |
7 |
CInT(表達式) 將表達式轉換為整數數據類型。 |
8 |
CLng函數(表達式) 將表達式轉換為長數據類型。 |
9 |
CObj(表達式) 將表達式轉換為對象類型。 |
10 |
CSByte(表達式) 將表達式轉換為SByte數據類型。 |
11 |
CShort(表達式) 將表達式轉換為短數據類型。 |
12 |
CSng函數(表達式) 將表達式轉換為單一數據類型。 |
13 |
CStr的(表達式) 將表達式轉換為字符串數據類型。 |
14 |
CUInt(表達式) 將表達式轉換為UInt數據類型。 |
15 |
CULng(表達式) 將表達式轉換為ULng數據類型。 |
16 |
CUShort(表達式) 將表達式轉換為UShort數據類型。 |
下面的例子演示了其中的一些功能:
Module DataTypes
Sub Main()
Dim n As Integer
Dim da As Date
Dim bl As Boolean = True
n = 1234567
da = Today
Console.WriteLine(bl)
Console.WriteLine(CSByte(bl))
Console.WriteLine(CStr(bl))
Console.WriteLine(CStr(da))
Console.WriteLine(CChar(CChar(CStr(n))))
Console.WriteLine(CChar(CStr(da)))
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
True
-1
True
12/4/2012
1
1
一個變量只是一個給我們的程序可以操作的存儲區域的名字。 VB.Net中的每個變量都有一個特定的類型,它決定了變量內存的大小和布局; 可以存儲在該存儲器內的值的范圍; 以及可以應用于該變量的一組操作。
我們已經討論了各種數據類型。 VB.Net中提供的基本值類型可以分為:
類型 | 示例 |
---|---|
Integral types | SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char |
Floating point types | Single and Double |
Decimal types | Decimal |
Boolean types | True or False values, as assigned |
Date types | Date |
VB.Net還允許定義其他值類型的變量,如枚舉和類的變量的引用類型。 我們將在后續章節討論日期類型和類。
Dim語句用于一個或多個變量的變量聲明和存儲分配。 Dim語句用于模塊,類,結構,過程或塊級別。
VB.Net中變量聲明的語法是:
[ < attributelist> ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]
[ ReadOnly ] Dim [ WithEvents ] variablelist
1、attributelist是適用于變量的屬性列表。 可選的。
2、accessmodifier定義變量的訪問級別,它具有值 – Public,Protected,Friend,Protected Friend和Private。 可選的。
3、Shared共享聲明一個共享變量,它不與類或結構的任何特定實例相關聯,而是可用于類或結構的所有實例。 可選的。
4、Shadows陰影表示變量在基類中重新聲明和隱藏一個同名的元素或一組重載的元素。 可選的。
5、Static表示變量將保留其值,即使在聲明它的過程終止之后。 可選的。
6、ReadOnly表示變量可以讀取,但不能寫入。 可選的。
7、WithEvents指定該變量用于響應分配給變量的實例引發的事件。 可選的。
8、Variablelist提供了聲明的變量列表。
變量列表中的每個變量具有以下語法和部分:
variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
1、variablename:是變量的名稱
2、boundslist:可選。 它提供了數組變量的每個維度的邊界列表。
3、New:可選。 當Dim語句運行時,它創建一個類的新實例。
4、datatype:如果Option Strict為On,則為必需。 它指定變量的數據類型。
5、initializer:如果未指定New,則為可選。 創建時評估并分配給變量的表達式。
一些有效的變量聲明及其定義如下所示:
Dim StudentID As Integer
Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date
變量被初始化(賦值)一個等號,然后是一個常量表達式。 初始化的一般形式是:
variable_name = value;
例如,
Dim pi As Double
pi = 3.14159
您可以在聲明時初始化變量,如下所示:
Dim StudentID As Integer = 100
Dim StudentName As String = "Bill Smith"
嘗試下面的例子,它使用各種類型的變量:
Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c = a + b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
a = 10, b = 20, c = 30
System命名空間中的控制臺類提供了一個函數ReadLine,用于接受來自用戶的輸入并將其存儲到變量中。 例如,
Dim message As String
message = Console.ReadLine
下面的例子說明:
Module variablesNdataypes
Sub Main()
Dim message As String
Console.Write("Enter message: ")
message = Console.ReadLine
Console.WriteLine()
Console.WriteLine("Your Message: {0}", message)
Console.ReadLine()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果(假設用戶輸入的Hello World):
Enter message: Hello World
Your Message: Hello World
有兩種表達式:
變量是左值,因此可能出現在作業的左側。 數字文字是右值,因此可能不會分配,不能出現在左側。 以下是有效的語句:
Dim g As Integer = 20
但以下并不是有效的語句,并會生成編譯時的錯誤:
20 = g
constants常數指的是程序在執行過程中可能不會改變的固定值。 這些固定值也稱為文字。
常量可以是任何基本數據類型,如整數常量,浮點常量,字符常量或字符串常量。 還有枚舉常量。
常量被視為常規變量,除了它們的值不能在它們的定義之后被修改。
enumeration枚舉是一組命名的整數常量。
在VB.Net中,常量是使用Const語句聲明的。 Const語句用于模塊,類,結構,過程或塊級別,以用于替換文字值。
Const語句的語法是:
[ < attributelist> ] [ accessmodifier ] [ Shadows ]
Const constantlist
1、attributelist:指定應用于常量的屬性列表; 您可以提供多個屬性,以逗號分隔。 可選的。
2、accessmodifier:指定哪些代碼可以訪問這些常量。 可選的。 值可以是:Public, Protected, Friend, Protected Friend, or Private.
3、Shadows:這使常量隱藏基類中相同名稱的編程元素。 可選的。
4、Constantlist:給出聲明的常量的名稱列表。 必填。
其中,每個常量名都有以下語法和部分:
constantname [ As datatype ] = initializer
1、constantname常量名:指定常量的名稱
2、data type 數據類型:指定常量的數據類型
3、initializer 初始值設定:指定分配給常量的值
例如,
'The following statements declare constants.'
Const maxval As Long = 4999
Public Const message As String = "HELLO"
Private Const piValue As Double = 3.1415
以下示例演示了常量值的聲明和使用:
Module constantsNenum
Sub Main()
Const PI = 3.14149
Dim radius, area As Single
radius = 7
area = PI * radius * radius
Console.WriteLine("Area = " & Str(area))
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
Area = 153.933
VB.Net提供以下打印和顯示常量:
Constant | 描述 |
---|---|
vbCrLf | 回車/換行字符組合。 |
vbCr | 回車字符。 |
vbLf | 換行字符。 |
vbNewLine | 換行字符。 |
vbNullChar | 空字符。 |
vbNullString | 不等于零長度字符串(“”); 用于調用外部過程。 |
vbObjectError | 錯誤號。用戶定義的錯誤號應大于此值。例如: Err.Raise(數字)= vbObjectError + 1000 |
vbTab | 標簽字符。 |
vbBack | 退格字符。 |
使用Enum語句聲明枚舉類型。 Enum語句聲明一個枚舉并定義其成員的值。 Enum語句可以在模塊,類,結構,過程或塊級別使用。
Enum語句的語法如下:
[ < attributelist > ] [ accessmodifier ] [ Shadows ]
Enum enumerationname [ As datatype ]
memberlist
End Enum
1、attributelist:指應用于變量的屬性列表。 可選的。
2、asscessmodifier:指定哪些代碼可以訪問這些枚舉。 可選的。 值可以是:公共,受保護,朋友或私人。
3、Shadows:這使枚舉隱藏基類中相同名稱的編程元素。 可選的。
4、enumerationname:枚舉的名稱。 必填
5、datatype:指定枚舉的數據類型及其所有成員。
6、memberlist:指定在此語句中聲明的成員常數的列表。 必填。
成員列表中的每個成員具有以下語法和部分:
[< attribute list>] member name [ = initializer ]
name名稱:指定成員的名稱。必填。
initializer初始化:分配給枚舉成員的值??蛇x的。
例如,
Enum Colors
red = 1
orange = 2
yellow = 3
green = 4
azure = 5
blue = 6
violet = 7
End Enum
以下示例演示了Enum變量顏色的聲明和使用:
Module constantsNenum
Enum Colors
red = 1
orange = 2
yellow = 3
green = 4
azure = 5
blue = 6
violet = 7
End Enum
Sub Main()
Console.WriteLine("The Color Red is : " & Colors.red)
Console.WriteLine("The Color Yellow is : " & Colors.yellow)
Console.WriteLine("The Color Blue is : " & Colors.blue)
Console.WriteLine("The Color Green is : " & Colors.green)
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
The Color Red is: 1
The Color Yellow is: 3
The Color Blue is: 6
The Color Green is: 4
修飾符是通過任何編程元素添加的關鍵字,以便特別強調編程元素將如何在程序中執行或將被訪問
例如,訪問修飾符:Public, Private, Protected, Friend, Protected Friend等,指示編程元素的訪問級別,如變量,常量,枚舉或類。
下表提供了VB.Net修飾符的完整列表:
S.N | 修飾符 | 描述 |
---|---|---|
1 | Ansi | 指定Visual Basic應該將所有字符串編組到美國國家標準協會(ANSI)值,而不考慮正在聲明的外部過程的名稱。 |
2 | Assembly | 指定源文件開頭的屬性適用于整個程序集。 |
3 | Async | 表示它修改的方法或lambda表達式是異步的。 這樣的方法被稱為異步方法。 異步方法的調用者可以恢復其工作,而不必等待異步方法完成。 |
4 | Auto | 在外部過程的調用期間,十進制中的chchetetmodifierpart提供用于編組字符串的字符集信息。 它還會影響Visual Basic如何在外部文件中搜索外部過程名稱。 Auto修飾符指定Visual Basic應根據.NET Framework規則編組字符串。 |
5 | ByRef | 指定參數通過引用傳遞,即被調用過程可以更改調用代碼中參數下面的變量的值。 它在下列語境下使用:
|
6 | BYVAL | 指定傳遞參數時,調用過程或屬性不能更改調用代碼中參數下面的變量的值。 它在下列語境下使用:
|
7 | Default | 標識屬性作為它的類、結構或接口的默認屬性。 |
8 | Friend |
指定一個或多個聲明的編程元素可以從包含其聲明的程序集中訪問,而不僅僅是聲明它們的組件。 Friendaccess通常是應用程序編程元素的首選級別,Friend是接口,模塊,類或結構的默認訪問級別。 |
9 | In | 它用于通用接口和代理。 |
10 | Iterator | 指定函數或Get訪問器是迭代器。 Aniterator對集合執行自定義迭代。 |
11 | Key | Key關鍵字使您能夠為匿名類型的屬性指定行為。 |
12 | Module | 指定源文件開頭的屬性適用于當前裝配模塊。 它與Module語句不同。 |
13 | MustInherit | 指定一個類只能用來作為基類,并且你不能直接創建一個對象。 |
14 | MustOverride | 指定屬性或過程未在此類中實現,必須在導出類中重寫,然后才能使用。 |
15 | Narrowing | 表示轉換運算符(CType)將類或結構轉換為可能不能保存原始類或結構的某些可能值的類型。 |
16 | NotInheritable | 指定類不能用作基類。 |
17 | NotOverridable | 指定不能在派生類中重寫屬性或過程。 |
18 | Optional | 指定當程序被調用的過程參數可以被省略。 |
19 | Out | 對于通用類型參數,Out關鍵字指定類型是協變的。 |
20 | Overloads | 指定屬性或過程重新聲明具有相同名稱的一個或多個現有屬性或過程。 |
21 | Overridable | 指定屬性或過程可以由派生類中具有相同名稱的屬性或過程覆蓋。 |
22 | Overrides | 指定屬性或過程覆蓋從基類繼承的命名相同的屬性或過程。 |
23 | ParamArray |
ParamArray允許您將任意數量的參數傳遞給過程。 ParamArray參數始終使用ByVal聲明。 |
24 | Partial | 表示類或結構聲明是類或結構的部分定義。 |
25 | Private | 指定一個或多個聲明的編程元素只能在其聲明上下文中訪問,包括來自任何包含的類型。 |
26 | Protected | 指定一個或多個聲明的編程元素只能從其自己的類或派生類中訪問。 |
27 | Public | 指定一個或多個聲明的編程元素沒有訪問限制。 |
28 | ReadOnly | 指定可以讀取但不寫入變量或屬性。 |
29 | Shadows | 指定聲明的編程元素在基類中重新聲明和隱藏相同命名的元素或一組重載的元素。 |
30 | Shared | 指定一個或多個聲明的編程元素與類或結構(而不是類或結構的特定實例)關聯。 |
31 | Static | 指定一個或多個已聲明的局部變量將繼續存在,并在聲明它們的過程終止后保留其最新值。 |
32 | Unicode | 指定Visual Basic應將所有字符串編組為Unicode值,而不考慮正在聲明的外部過程的名稱。 |
33 | Widening | 表示轉換運算符(CType)將類或結構轉換為可以保存原始類或結構的所有可能值的類型。 |
34 | WithEvents | 指定一個或多個聲明的成員變量引用可以引發事件的類的實例。 |
35 | WriteOnly | 指定可以寫入但不讀取屬性。 |
statement聲明是Visual Basic程序中的完整指令。 它可以包含關鍵字,運算符,變量,字面值,常量和表達式。
語句可以分類為:
1、Declaration statements聲明語句– 這些語句是您命名變量,常量或過程的語句,也可以指定數據類型。
2、Executable statements可執行語句– 這些是啟動動作的語句。 這些語句可以調用方法或函數,通過代碼塊循環或分支,或者將值或表達式賦值給變量或常量。 在最后一種情況下,它被稱為Assignment語句。
聲明語句用于命名和定義過程、變量、屬性、數組和常量。當你聲明的編程元素時,您還可以定義其數據類型、訪問級別和范圍。
您可以聲明的編程元素包括變量、常數、枚舉、類、結構、模塊、接口、程序、過程參數、函數返回值、外部過程的引用、運算符、屬性、事件和委托。
以下是在VB.Net中的聲明語句︰
S.N | 聲明和說明 | 示例 |
---|---|---|
1 |
Dim Statement Dim語句 Declares and allocates storage space for one or more variables. 聲明和分配一個或多個變量的存儲空間。 |
|
2 | Const Statement Const語句 Declares and defines one or more constants. 聲明和定義一個或多個常量。 |
|
3 | Enum Statement 枚舉語句 Declares an enumeration and defines the values of its members. 聲明一個枚舉并定義其成員的值。 |
|
4 |
Class Statement 類語句 Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises. 聲明類的名稱,并引入該類包含的變量,屬性,事件和過程的定義。 |
|
5 |
Structure Statement 結構聲明 Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises. 聲明結構的名稱,并引入結構所包含的變量,屬性,事件和過程的定義。 |
|
6 |
Module Statement 模塊語句 Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises. 聲明模塊的名稱,并介紹模塊包含的變量,屬性,事件和過程的定義。 |
|
7 |
Interface Statement 接口語句 |
|
8 |
Function Statement 函數語句 Declares the name, parameters, and code that define a Function procedure. 聲明定義函數過程的名稱,參數和代碼。 |
|
9 |
Sub Statement 子語句 Declares the name, parameters, and code that define a Sub procedure. 聲明定義Sub過程的名稱,參數和代碼。 |
|
10 |
Declare Statement 聲明語句 Declares a reference to a procedure implemented in an external file. 聲明對在外部文件中實現的過程的引用。 |
|
11 |
Operator Statement 運算符聲明 Declares the operator symbol, operands, and code that define an operator procedure on a class or structure. 聲明的運算符符號、操作數和在類或結構定義一個運算符過程的代碼。 |
|
12 |
Property Statement 屬性聲明 Declares the name of a property, and the property procedures used to store and retrieve the value of the property. 聲明屬性的名稱,以及用于存儲和檢索屬性值的屬性過程。 |
|
13 |
Event Statement 事件聲明 Declares a user-defined event. 聲明用戶定義的事件。 |
|
14 |
Delegate Statement 委托聲明 Used to declare a delegate. 用于聲明一個委托。 |
|
示例
以下示例演示了一個決策語句:
Module decisions
Sub Main()
'local variable definition '
Dim a As Integer = 10
' check the boolean condition using if statement '
If (a < 20) Then
' if condition is true then print the following '
Console.WriteLine("a is less than 20")
End If
Console.WriteLine("value of a is : {0}", a)
Console.ReadLine()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
a is less than 20;
value of a is : 10
VB.Net編譯器指令給編譯器指令,以便在實際編譯開始之前預處理信息。
所有這些指令以#開頭,并且在一行上的指令之前只能出現空格字符。 這些指令不是語句。
VB.Net編譯器沒有單獨的預處理器; 然而,指令被處理,就像有一個。 在VB.Net中,編譯器指令用于幫助條件編譯。 與C和C ++指令不同,它們不用于創建宏。
VB.Net提供了以下一組編譯器指令:
The #Const 指令
The #ExternalSource 指令
The #If…Then…#Else指令
The #Region 指令
該指令定義條件編譯常量。語法這個指令是:
#Const constname = expression
constname:指定常量的名稱。必要。
expression:它是文字或其他條件編譯器常量,或包含任何或所有算術或邏輯運算符(除了Is)的組合。
例如,
#Const state = "WEST BENGAL"
示例
以下代碼演示了偽指令的使用:
Module mydirectives
#Const age = True
Sub Main()
#If age Then
Console.WriteLine("You are welcome to the Robotics Club")
#End If
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
You are welcome to the Robotics Club
此指令用來指示特定行的源代碼和源的外部文本之間的映射。它使用僅由編譯器和調試器并不影響代碼編譯。
此指令允許包括從外部代碼文件外部的代碼到一個源代碼文件。
此指令的語法是︰
#ExternalSource( StringLiteral , IntLiteral )
[ LogicalLine ]
#End ExternalSource
#ExternalSource偽指令的參數是外部文件的路徑,第一行的行號和發生錯誤的行。
示例
以下代碼演示了偽指令的使用:
Module mydirectives
Public Class ExternalSourceTester
Sub TestExternalSource()
#ExternalSource("c:vbprogsdirectives.vb", 5)
Console.WriteLine("This is External Code. ")
#End ExternalSource
End Sub
End Class
Sub Main()
Dim t As New ExternalSourceTester()
t.TestExternalSource()
Console.WriteLine("In Main.")
Console.ReadKey()
End Sub
當上述代碼被編譯和執行時,它產生了以下結果:
This is External Code.
In Main.
此偽指令有條件地編譯所選的Visual Basic代碼塊。
此偽指令的語法是:
#If expression Then
statements
[ #ElseIf expression Then
[ statements ]
...
#ElseIf expression Then
[ statements ] ]
[ #Else
[ statements ] ]
#End If
例如,
#Const TargetOS = "Linux"
#If TargetOS = "Windows 7" Then
' Windows 7 specific code
#ElseIf TargetOS = "WinXP" Then
' Windows XP specific code
#Else
' Code for other OS
#End if
示例
下面的代碼演示一個假設使用的指令:
Module mydirectives
#Const classCode = 8
Sub Main()
#If classCode = 7 Then
Console.WriteLine("Exam Questions for Class VII")
#ElseIf classCode = 8 Then
Console.WriteLine("Exam Questions for Class VIII")
#Else
Console.WriteLine("Exam Questions for Higher Classes")
#End If
Console.ReadKey()
End Sub
End Module
當上述代碼被編譯和執行時,它產生了以下結果:
Exam Questions for Class VIII
此偽指令有助于在Visual Basic文件中折疊和隱藏代碼段。
此偽指令的語法是:
#Region "identifier_string"
#End Region
例如,
#Region "StatsFunctions"
' Insert code for the Statistical functions here.
#End Region
運算符是一個符號,通知編譯器執行特定的數學或邏輯操作。 VB.Net豐富的內置運算符,并提供以下類型的常用運算符:
算術運算符
比較運算符
邏輯/位運算符
位移位運算符
賦值運算符
其他運算符
本教程將介紹最常用的運算符。
下表顯示了VB.Net支持的所有算術運算符。 假設變量A保持2,變量B保持7,則:
運算符 | 描述 | 例 |
---|---|---|
^ |
Raises one operand to the power of anothe 將一個操作數提升為另一個的權力 |
B^A will give 49 |
+ |
Adds two operand 添加兩個操作數s |
A + B will give 9 |
– | Subtracts second operand from the first 從第一個操作數中減去第二個操作數 |
A – B will give -5 |
* |
Multiplies both operands 將兩個操作數相乘 |
A * B will give 14 |
/ | Divides one operand by another and returns a floating point result 將一個操作數除以另一個操作數,并返回一個浮點結果 |
B / A will give 3.5 |
\ | Divides one operand by another and returns an integer result 將一個操作數除以另一個操作數,并返回一個整數結果 |
B \ A will give 3 |
MOD | Modulus Operator and remainder of after an integer pision 模數運算符和整數除法后的余數 |
B MOD A will give 1 |
下表顯示了VB.Net支持的所有比較運算符。 假設變量A保持10,變量B保持20,則:
運算符 | 描述 | 例 |
---|---|---|
= | Checks if the values of two operands are equal or not; if yes, then condition becomes true. 檢查兩個操作數的值是否相等; 如果是,則條件變為真。 |
(A = B)是不正確的。 |
<> |
Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true. 檢查兩個操作數的值是否相等; 如果值不相等,則條件為真。 |
(AB)為真。 |
> | Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. 檢查左操作數的值是否大于右操作數的值; 如果是,則條件變為真。 |
(A> B)是不正確的。 |
< | Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true. 檢查左操作數的值是否小于右操作數的值; 如果是,則條件變為真。 |
(A <B)為真。 |
> = | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true. 檢查左操作數的值是否大于或等于右操作數的值; 如果是,則條件變為真。 |
(A> = B)是不正確的。 |
<= | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true. 檢查左操作數的值是否小于或等于右操作數的值; 如果是,則條件變為真。 |
(A <= B)為真。 |
除了上述情況外,VB.Net提供了三個比較運算符,我們將在以后的章節中使用; 然而,我們在這里給出一個簡短的描述。
1、Is運算符– 它比較兩個對象引用變量,并確定兩個對象引用是否引用相同的對象,而不執行值比較。 如果object1和object2都引用完全相同的對象實例,則result為True; 否則,result為False。
2、IsNot運算符– 它還比較兩個對象引用變量,并確定兩個對象引用是否引用不同的對象。 如果object1和object2都引用完全相同的對象實例,則result為False; 否則,result為True。
3、Like運算符– 它將字符串與模式進行比較。
下表顯示了VB.Net支持的所有邏輯運算符。 假設變量A保持布爾值True,變量B保持布爾值False,則:
運算符 | 描述 | 例 |
---|---|---|
And | It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. 它是邏輯以及按位AND運算符。 如果兩個操作數都為真,則條件為真。 此運算符不執行短路,即,它評估兩個表達式。 |
(A和B)為假。 |
Or | It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. 它是邏輯以及按位或運算符。 如果兩個操作數中的任何一個為真,則條件為真。 此運算符不執行短路,即,它評估兩個表達式。 |
(A或B)為真。 |
Not | It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. 它是邏輯以及按位非運算符。 用于反轉其操作數的邏輯狀態。 如果條件為真,則邏輯非運算符將為假。 |
沒有(A和B)為真。 |
Xor | It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator. 它是邏輯以及按位邏輯異或運算符。 如果兩個表達式都為True或兩個表達式都為False,則返回True; 否則返回False。 該運算符不會執行短路,它總是評估這兩個表達式,并且沒有該運算符的短路對應。 |
異或B為真。 |
AndAlso | It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. 它是邏輯AND運算符。它僅適用于布爾型數據。它執行短路。 |
(A AndAlso運算B)為假。 |
OrElse | It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. 它是邏輯或運算符。 它只適用于布爾數據。 它執行短路。 |
(A OrElse運算B)為真。 |
IsFalse | It determines whether an expression is False. 它確定表達式是否為假。 |
|
IsTrue | It determines whether an expression is True. 它確定表達式是否為真。 |
我們已經討論了按位運算符。 位移運算符對二進制值執行移位操作。 在進入位移運算符之前,讓我們來了解位操作。
按位運算符處理位并執行逐位操作。 &,|和^的真值表如下:
p | q | p&Q | p | q | p ^ Q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
假設A = 60; 和B = 13; 現在的二進制格式,他們將如下:
A = 0011 1100
B = 0000 1101
—————–
A&B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
?A = 1100 0011
我們已經看到VB.Net支持的Bitwise運算符是And,Or,Xor和Not。 位移位算子分別是用于左移和右移的>>和<<。
假設變量A保持60,變量B保持13,則:
運算符 | 描述 | 示例 |
---|---|---|
And | Bitwise AND Operator copies a bit to the result if it exists in both operands. 如果兩個操作數都存在,則按位AND運算符將一個位復制到結果。 |
(A AND B) will give 12, which is 0000 1100 |
Or | Binary OR Operator copies a bit if it exists in either operand. 二進制OR運算符復制一個位,如果它存在于任一操作數。 |
(A Or B) will give 61, which is 0011 1101 |
Xor | Binary XOR Operator copies the bit if it is set in one operand but not both. 二進制XOR運算符復制該位,如果它在一個操作數中設置,但不是兩個操作數。 |
(A Xor B) will give 49, which is 0011 0001 |
Not | Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. 二進制補碼運算符是一元的,具有“翻轉”位的效果。 |
(Not A ) will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. 二進制左移位運算符。 左操作數值向左移動由右操作數指定的位數。 |
A << 2 will give 240, which is 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. 二進制右移運算符。 左操作數值向右移動由右操作數指定的位數。 |
A >> 2 will give 15, which is 0000 1111 |
運算符 | 描述 | 例 |
---|---|---|
= |
Simple assignment operator, Assigns values from right side operands to left side operand 簡單賦值操作符,將值從右側操作數分配給左側操作數 |
C = A + B A + B將賦值為C |
+ = | Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand 添加AND賦值運算符,向左操作數添加右操作數,并將結果賦值給左操作數 |
C + = A等于C = C + A |
– = | Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand 減法AND賦值運算符,它從左操作數中減去右操作數,并將結果賦值給左操作數 |
? – = A等于C = C – 一個 |
* = | Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand 乘法AND賦值運算符,它將右操作數與左操作數相乘,并將結果賦值給左操作數 |
C * = A等于C = C * A |
/ = | Divide AND assignment operator, It pides left operand with the right operand and assigns the result to left operand (floating point pision) 除法AND賦值運算符,它用右操作數劃分左操作數,并將結果分配給左操作數(浮點除法) |
C / = A等于C = C / A |
\= | Divide AND assignment operator, It pides left operand with the right operand and assigns the result to left operand (Integer pision) 除法AND賦值運算符,它用右操作數劃分左操作數,并將結果分配給左操作數(整數除法) |
? = A等于C = C A |
^ = | Exponentiation and assignment operator. It raises the left operand to the power of the right operand and assigns the result to left operand. 指數和賦值運算符。 它將左操作數提升為右操作數的冪,并將結果分配給左操作數。 |
C ^ = A等于C = C ^ A |
<< = | Left shift AND assignment operator 左移AND賦值運算符 |
C語言的<< = 2是同C = C << 2 |
>> = | Right shift AND assignment operator 右移AND賦值運算符 |
C >> = 2 >> 2同C = C |
&= | Concatenates a String expression to a String variable or property and assigns the result to the variable or property. 將String表達式連接到String變量或屬性,并將結果分配給變量或屬性。 |
STR1&= STR2賽車是一樣的 STR1 = STR1與STR2 |
有很少其他重要的操作系統支持VB.Net。
運算符 | 描述 | 例 |
---|---|---|
AddressOf | Returns the address of a procedure. 返回過程的地址。 |
|
Await | It is applied to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes. 它應用于異步方法或lambda表達式中的操作數,以暫停該方法的執行,直到等待的任務完成。 |
|
GetType | It returns a Type object for the specified type. The Type object provides information about the type such as its properties, methods, and events. 它返回指定類型的Type對象。 Type對象提供有關類型的信息,例如其屬性,方法和事件。 |
|
Function Expression | It declares the parameters and code that define a function lambda expression. 它聲明定義函數lambda表達式的參數和代碼。 |
|
If | It uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments. 它使用短路評估有條件地返回兩個值之一。 可以使用三個參數或兩個參數調用If運算符。 |
|
運算符優先級確定表達式中的術語分組。 這會影響表達式的計算方式。 某些運算符比其他運算符具有更高的優先級; 例如,乘法運算符的優先級高于加法運算符:
例如,x = 7 + 3 * 2; 這里,x被分配13,而不是20,因為operator *具有比+高的優先級,所以它首先乘以3 * 2,然后加到7。
這里,具有最高優先級的運算符出現在表的頂部,具有最低優先級的運算符出現在底部。 在表達式中,將首先計算較高優先級運算符。
運算符 | 優先級 |
---|---|
Await | 最高 |
Exponentiation (^) | |
Unary identity and negation (+, -) | |
Multiplication and floating-point pision (*, /) | |
Integer pision (\) | |
Modulus arithmetic (Mod) | |
Addition and subtraction (+, -) | |
Arithmetic bit shift (<<, >>) | |
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf…Is) | |
Negation (Not) | |
Conjunction (And, AndAlso) | |
Inclusive disjunction (Or, OrElse) | |
Exclusive disjunction (Xor) | 最低 |
決策結構需要程序員指定一個或多個條件進行評估或測試程序和語句或語句的執行,如果確定的條件為真,并選擇,如果確定的條件為假,則執行其它語句。
以下是決策的典型結構發現在大多數編程語言的一般形式︰
VB.Net提供以下類型的決策語句。 單擊以下鏈接以檢查其詳細信息。
聲明 | 描述 |
---|---|
If … Then statement |
AnIf…Then statementconsists of a boolean expression followed by one or more statements. 一個If…Then語句由一個布爾表達式后跟一個或多個語句組成。 |
If…Then…Else statement |
AnIf…Then statementcan be followed by an optionalElse statement, which executes when the boolean expression is false. 一個If…Then語句后面可以是一個可選的Else語句,當布爾表達式為假時執行。 |
nested If statements |
You can use oneIforElse ifstatement inside anotherIforElse ifstatement(s). 您可以在一個if或else if語句中使用If或Else if語句。 |
Select Case statement |
You can use oneIforElse ifstatement inside anotherIforElse ifstatement(s). Select Case語句允許根據值列表測試變量的相等性。 |
nested Select Case statements |
You can use oneselect casestatement inside anotherselect casestatement(s). 您可以使用一個selectcase語句中使用一個selectcase語句。 |
可能有一種情況,當你需要執行一段代碼幾次。 一般來說,語句是按順序執行的:函數中的第一個語句首先執行,然后是第二個語句,依此類推。
編程語言提供允許更復雜的執行路徑的各種控制結構。
循環語句允許我們多次執行一個語句或一組語句,以下是大多數編程語言中循環語句的一般形式:
VB.Net提供以下類型的循環來處理循環需求。 單擊以下鏈接以檢查其詳細信息。
循環類型 | 描述 |
---|---|
Do Loop |
It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True. It could be terminated at any time with the Exit Do statement. 它重復包含的語句塊內布爾條件為True或直到條件變為True。 它可以隨時使用Exit Do語句終止。 |
For…Next |
It repeats a group of statements a specified number of times and a loop index counts the number of loop iterations as the loop executes. 它重復指定次數的一組語句,循環索引計算循環執行時的循環迭代數。 |
For Each…Next |
It repeats a group of statements for each element in a collection. This loop is used for accessing and manipulating all elements in an array or a VB.Net collection. 它為集合中的每個元素重復一組語句。 這個循環用于訪問和操作數組或VB.Net集合中的所有元素。 |
While… End While |
It executes a series of statements as long as a given condition is True. 只要給定條件為True,它就執行一系列語句。 |
With… End With |
It is not exactly a looping construct. It executes a series of statements that repeatedly refer to a single object or structure. 它不是一個循環結構。 它執行一系列重復引用單個對象或結構的語句。 |
Nested loops |
You can use one or more loops inside any another While, For or Do loop. 您可以在任何其他While,For或Do循環中使用一個或多個循環。 |
循環控制語句從其正常序列改變執行。 當執行離開作用域時,在該作用域中創建的所有自動對象都將被銷毀。
VB.Net提供以下控制語句。 單擊以下鏈接以檢查其詳細信息。
控制語句 | 描述 |
---|---|
Exit statement |
Terminates thelooporselect casestatement and transfers execution to the statement immediately following the loop or select case. |
Continue statement |
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.導致循環跳過其本身的其余部分,并在重復之前立即重新測試其狀態。 |
GoTo statement |
Transfers control to the labeled statement. Though it is not advised to use GoTo statement in your program. 將控制轉移到帶標簽的語句。 雖然不建議在程序中使用GoTo語句。 |
未完待續,下一章節,つづく
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
18135鏡頭是什么意思?鏡頭18135mm是指相機中的一個數字,其中它的18表示這款相機的最短焦距為18 mm,最長焦距為135mm,而在這兩個數字之間,它的變焦是連續可變的。鏡頭在影視方面有兩個手指。一種是指膠片相機和放映機用來產生影像的光學元件,由多個鏡頭組成。鏡頭18-135什么意思?18-135代表鏡頭焦距,表示短焦距18mm,長焦距135 mm..換句話說,這個鏡頭最近只能拉回18mm...
旅行用英文怎么說?在英語中,旅行是:旅行,旅程,旅行,旅游,航行。1.旅行:在……旅行;在旅行;四處旅游;走來走去;旅行。常指出國或去很遠的地方,不強調具體地點。比如:1)瑞安博士周游世界為他的書收集素材。瑞安博士周游世界為他的書收集材料。2)去環游世界,環游世界。我們旅行了800多公里。我們的行程已達800多公里。2、旅程:旅行,行程單;距離;旅行時間;旅行。一般來說,時間和距離都很長的陸地旅行...
5g手機打游戲怎么樣?在5g手機上玩游戲,如果用wifi,和在4g手機上是一樣的。如果用流量的話,如果你的手機正好充滿了5g信號,那么在這種情況下,你的5g網絡玩游戲肯定比4g網絡好,因為5g信號下延遲更低,網速更快,所以體驗更好。5個G 的手游?像《和平精英》和《英雄聯盟》手游這樣的游戲,內存都有5個G。即使玩古典記憶,游戲的動畫也很好看,上手體驗也很好。這些游戲,目前網上呼聲很高,能排到前五...