VB.net 基礎知識----循環,類

一. 常用變量類型,FCL和VB的對應關係

二. .net 框架

三. For 循環 (已知被循環的所有數)

Dim nums() As Integer = {10,25,30,45,50,65}
For index As Integer = 0 To nums.Length - 1 Step 2
    Dim value As Integer = nums(index)
    Console.WriteLine(value)
Next
'The result is 10 30 50

Dim nums() As Integer = {10,25,30,45,50,65}
For index As Integer = nums.Length - 1 To 0 Step -1
    Dim value As Integer = nums(index)
    Console.WriteLine(value)
Next
'The result is 65 50 45 30 25 10

Continue For

Exit For

四. While 循環(被循環的數不確定,但是已知條件)
Dim nums() As Integer = {10,25,30,45,50,65}
Dim index As Integer = 0
Dim value As Integer = nums(index)
While value < 50
    If value Mod 2 = 0 Then
        Continue While
    End If
    Console.WriteLine(value)
    index += 1
    value = nums(index)
End While
'The result is 

Dim nums() As Integer = {10,25,30,45,50,65}
Dim index As Integer = 0
Dim value As Integer = nums(index)
While value < 50
    If value Mod 2 = 0 Then
        Exit While
    End If
    Console.WriteLine(value)
    index += 1
    value = nums(index)
End While
'The result is 

五. For Each 循環
Dim nums() As Double = {10.5,20.5,30.5,40.5,50.5}
'For index As Integer = 0 To nums.Length - 1 
    'Dim value As Double = nums(index)
    'Console.WriteLine(value)
'Next
For Each value As Double In nums 
    Console.WriteLine(value)
Next
'The result is 10.5 20.5 30.5 40.5 50.5

六. Overload
Overloading 重載
表示同一個類中可以有多個名稱相同的方法,但這些方法的參數列表各不相同(即參數個數、類型或順序不同)

Override 重寫

FCL Framework類庫
CLS Common Language Specification公共語言規範
CLR Common Language Runtime公共語言運行庫

七. Exception handling

Try
    ShowNumbers(nums)
Catch ex1 As DivideByZeroException
    Console.WriteLine(ex1.Message)
Catch ex2 As ArithmeticException
    Console.WriteLine(ex2.Message)
Catch ex3 As Exception
    Console.WriteLine(ex3.Message)    
End Try

'Throwing exception
Sub BuyDrinks(age As Integer)
    If age <= 21 Then
        Throw New ArgumentException("Must be 21 or older")
    End If
End Sub

八. 類
class: variables/state + procedures/behavior
state: fields + properties
behavior: methods (subrountines + functions)
constructors is a special methods that run when an object is created

fields: public + private
private often exposed using properties

區別:Dim emp As New Employee()       Dim emp As New Employee

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章