程式語言 Visual Basic 變數的可視範圍
description
Transcript of 程式語言 Visual Basic 變數的可視範圍
1
程式語言程式語言 Visual BasicVisual Basic變數的可視範圍變數的可視範圍黃瀧輝 老師Long Hwai,Huang
2
壹 .1 資料的種類在 VB 中所使用到的資料包括兩種: 一、常數:不會因程式執行過程而 改變數值。 二、變數:數值可能隨時改變。
3
壹 .2 變數的使用方式 未經宣告即使用的變數 Ex : Dim x as integer ‘ 宣告變數 x y = x + 2 ‘ y 未經宣告變數 強制變數宣告 Option Explicit Ex :如上例會出 現如右錯誤訊息
4
壹 .3 變數的使用方式探討 ⅠFunctionNumber = InputBox(" 請輸入一個數字 ", " 輸入視窗 ")If FunctionNumber < 10 Then
MsgBox FunctionNumber & " < 10 "ElseIf Functionmumber > 10 Then
MsgBox FunctionNumber & " > 10 "ElseIf FunctionNumber = 10 Then
MsgBox FucntionNumber & " = 10 "End If
FunctionNumber = InputBox(" 請輸入一個數字 ", " 輸入視窗 ")If FunctionNumber < 10 Then
MsgBox FunctionNumber & " < 10 "ElseIf FunctionNumber > 10 Then
MsgBox FunctionNumber & " > 10 "ElseIf FunctionNumber = 10 Then
MsgBox FunctionNumber & " = 10 "End If
A
B
5
壹 .4 變數的使用方式探討 Ⅱ 末經宣告即使用的變數,程式寫作的方便性較佳,但往往因為誤打或某些原因造成程式在偵錯上較為困難。 同時在程式的可讀性上也會較差。也較難判別變數在程式執行過程中的「值」變化。
6
壹 .5 變數的生命週期 在程式執行的過程中,變數會佔用某一塊記憶體空間。 變數在程式的執行中,佔有一定的存活期。 變數在程式的執行中,擁有一定的使用範圍 ( 可視範圍 ) 。
7
貳 .1 變數的種類依照變數可使用的範圍及記錄資料的內容,可將變數歸納為: 全域變數 (Global Variable) 區域變數 (Local Variable) 靜態變數 (Static Variable)
8
貳 .2 全域變數 (Global Variable) Ⅰ基本定義 在該表單程式 ( 或模組程式 ) 的全部區域都可以使用,不受限「副程序」範圍限制。 凡撰寫在副程序之前的變數均是。生命週期 起:程式執行到變數宣告處。 結:表單程式 ( 或模組程式 ) 結束時。
9
貳 .3 全域變數 (Global Variable) Ⅱ例子 某一個 Form 的程式碼如下:
Dim x As Integer ‘x 為全域變數Sub SubX()End SubFunction FunX() as IntegerEnd Function
10
貳 .4 全域變數 (Global Variable) Ⅲ優 / 缺點 全域變數可供該表單程式 ( 或模組程式 ) 中的全部副程序來使用,則若某一副程序更改變數值的話,其他使用這個變數值的副程序也會受到影響。 這在程式的偵錯上會較為困難。
11
例子Dim x, y As IntegerSub subx()
x = x + y + 2Print x
End SubSub suby()
y = x - 1Print y
End SubSub Form_Load()
x = 0y = 0subxsuby
End Sub
x y
0 0 0 2 0
2 1
12
參 .1 區域變數 (Local Variable) Ⅰ基本定義 在副程序中宣告使用的變數。 僅限於宣告區域副程序使用。生命週期 起:程式執行到副程序中的變數宣告處。 結:該副程序結束 ( 執行到 End Sub 或 End
Function) 時。
13
參 .2 區域變數 (Local Variable) Ⅱ例子 某一個 Form 的程式碼如下:
Sub SubX()Dim x as integer ‘x 為區域變數End SubFunction FunX() as IntegerDim y as integer ‘y 為區域變數End Function
14
參 .3 區域變數 (Local Variable) Ⅲ優 / 缺點 使用區域變數,則變數的使用範圍較單純,在程式的偵錯上較方便。 不過若單純的區域變數將無法記錄上次在這個區域中的執行結果。
15
例子Sub subx() Dim x As Integer x = x + 2 Print xEnd SubSub suby() Dim x As Integer x = x + 5 Print xEnd SubSub Form_Load() subx suby { subx }End Sub
subx.x suby.x
0 × 2 ×
× 0 × 5 × ×
× ×
16
肆 .1 靜態變數 (Static Variable) Ⅰ基本定義 在 VB 中利用 Static 保留字來宣告的區域變數。 會保留該變數上次執行的內容值。生命週期 起:程式第一次執行到副程序中的變數宣告處。 結:表單程式 ( 或模組程式 ) 結束時。
17
肆 .2 靜態變數 (Static Variable) Ⅱ例子 某一個 Form 的程式碼如下:
Sub SubX()Static x as integer ‘x 為靜態變數End SubFunction FunX() as IntegerDim y as integer ‘y 為區域變數End Function
18
肆 .3 靜態變數 (Static Variable) Ⅲ優 / 缺點 使用靜態變數保有區域變數,使用範圍較單純的優點,在程式的偵錯上較方便。 改正區域變數無法記錄上次在這個區域中執行結果的問題。
19
例子Sub subx() Static x As Integer Dim y As Integer x = x + 2 y = y + 1 Print "x = " & x; " y =" & yEnd SubSub Form_Load() subx subxEnd Sub
x y
× × 0 × 0 0 2 0 2 1 {2} × 2 0
4 1 {4} ×
4 0
20
伍 .1 靜態變數 (Static Variable) Ⅳ 若整個副函式區域中的變數都要宣告成靜態變數的話,則:
Static Sub SubName()Dim x as integer ‘x 為靜態變數Dim y as integer ‘y 為靜態變數…….
End Sub
21
陸 .1 Public / Private 的影響 Ⅰ Public / Private 為 VB 中的保留字,會影響變數的使用範圍。 當有兩個以上的 Form 或是模組時,可用來控制變數的使用範圍。 主要用來修飾表單或是模組的變數、常數、副程式等…。
22
陸 .2 Private & Public
在表單模組中冠上 Private 保留字表示封閉的,其他模組的程式不能使用。《表單模組 A 》 private varA as integer
……..
《表單模組 B 》 varA = 4 ‘Error!! 不能使用其他模組的私有變數。……..
23
陸 .3 Private & Public
在表單模組中冠上 Public 保留字允許其他的表單模組使用,不過需冠上表單名稱。《 FormA 》 public varA as integer
……..
《 FormB 》 FormA.varA = 4 ‘ 必須要冠上表單名稱才能使用。……..
24
陸 .4 未冠上 Private/Public 的情形 變數 Dim X 常數 Const C = 123 程序 Sub SubX() 自訂資 Type NewType料型別 I as Integer S as StringEnd Type
Private X
Private Const C=123
Public Sub SubX()
Public Type NewType
I as Integer
S as String
End Type
例子 相當於
25
陸 .5 Private/Public 的抉擇 變數的使用範圍廣,則偵錯的難度愈高。因此,使用 Public 要較 Private 更難進行程式的偵錯,雖然 Public 在程式寫作上的方便性較佳,不過建議儘量少使用,尤其是變數部分。
26
陸 .6 Public / Private 的影響 Ⅱ Form1
Private A1Dim A2public A3Sub SubX1()Dim A4End SubSub SubX2()Dim A5End Sub
Form2Private B1Public B3Sub SubY1()
Dim B4End SubSub SubY2()
Dim B5End Sub
27
陸 .7 Public / Private 的影響 Ⅲ 使用 Public 會讓變數的使用範圍增廣,同樣的將會增加偵錯的難度。 使用 Public 較容易破壞物件的資料穩藏特性,於物件導向的觀念不符。 利用函式 Public 修飾子來讀取該物件的資料內容。如:
Public Function GetData() as Integer GetData = 欲取出的資料End Function
28
陸 .8 Public / Private 的影響 Ⅳ Form1Private A1Dim A2public A3Public Function GetA1() as in
tegerGetA1 = A1
End SubPublic Sub SetA1(Byval pam)
A1 = pamEnd Sub
Form2Private B1Public B3Sub SubY1()
B1 = Form1.GetA1End SubSub SubY2()
Form1.SetA1(10)End Sub
29
柒 .1 變數宣告的原則 儘量將變數宣告為區域變數。 若要提供給多個程序共用,才宣告成全域變數。 若要記錄程式執行的狀態,則宣告為靜態變數。
30
柒 .2 練習看看Dim x as IntegerSub SubX()
Static y as IntegerDim z as Integerx = y + z + 1y = x + z + 2z = x + y + 3
End SubPrivate Sub Form_Load()
SubXSubX
End Sub
這些部分的 x,y,z 變數值各為何?