- 最後登錄
- 2024-5-25
- 在線時間
- 7171 小時
- 註冊時間
- 2010-5-4
- 閱讀權限
- 95
- 精華
- 0
- UID
- 7964333
- 帖子
- 155
- 積分
- 1287 點
- 潛水值
- 47925 米
| 本帖最後由 tryit244178 於 2016-12-5 11:00 PM 編輯
想要漂亮的原始碼,除了有意義的變數宣告外
提供一下我個人寫程式的小技巧
讓有興趣的人參考參考
=====================================
模組化你的程式碼!
看看下面的程式碼…
- Sub TestA()
- Text1.Text = "TestA"
- Button1.Enabled = False
- i = 1
- End Sub
- Sub TestB()
- Text1.Text = "TestB"
- Button1.Enabled = True
- i = 1
- End Sub
- .
- .
- .
複製代碼
仔細一看,內容都是類似的!
寫個副程式,把它們包裝起來吧!
- Sub Test(ByVal strValue As String, ByVal blnValue As Boolean)
- Text1.Text = strValue
- Button1.Enabled = blnValue
- i = 1
- End Sub
複製代碼
=====================================
用函數來傳回布林值!
臨時想不出例子,總之很好用!!
- Function CheckState() As Boolean
- [程序]
- Return True
- End Fucntion
- If CheckState = True Then
- [程序]
- End If
複製代碼
=====================================
假設有個整數變數 i ,它會記錄執行後的結果
我們會用它記錄的結果來執行程序
- If i = 1 Then
- [程序]
- End If
- If i = 2 Then
- [程序]
- End If
- .
- .
- .
複製代碼
或是
- If i = 1 Then
- [程序]
- ElseIf i = 2 Then
- [程序]
- ElseIf i = 3 Then
- .
- .
- .
- End If
複製代碼
比起用If一行一行判斷,改用Select Case吧!
- Select Case i
- Case 1
- [程序]
- Case 2
- [程序]
- .
- .
- .
- End Select
複製代碼
=====================================
寫個從1加到10的程式碼
- Dim intCount As Integer = 0
- For i As Integer = 1 To 10
- intCount += i
- Next
複製代碼
有個自已呼叫自已的方法,叫做遞迴!(這個有好有壞,但很華麗XD)
- Function Count(ByVal intValue As Integer) As Integer
- If intValue = 0 Then Exit Function
- Return intValue + Count(intValue - 1)
- End Function
- '傳回55
- Debug.Print(Count(10))
複製代碼
=====================================
其他的還沒想到,歡迎補充
... |
|